Skip to content

Commit 8faa479

Browse files
committed
Merge pull request #26 from pjohnmeyer/fixUnitTestTimeConstraintExemptForFixtures
Fix unit test time constraint exempt for fixtures; closes #9.
2 parents 0f5e457 + 79172d0 commit 8faa479

File tree

8 files changed

+50
-19
lines changed

8 files changed

+50
-19
lines changed

src/Test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ TestList& Test::GetTestList()
2121
Test::Test(char const* testName, char const* suiteName, char const* filename, int lineNumber)
2222
: m_details(testName, suiteName, filename, lineNumber)
2323
, m_nextTest(0)
24-
, m_timeConstraintExempt(false)
2524
, m_isMockTest(false)
2625
{
2726
}

src/Test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class UNITTEST_LINKAGE Test
1717

1818
TestDetails const m_details;
1919
Test* m_nextTest;
20-
mutable bool m_timeConstraintExempt;
20+
2121
mutable bool m_isMockTest;
2222

2323
static TestList& GetTestList();

src/TestDetails.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ TestDetails::TestDetails(char const* testName_, char const* suiteName_, char con
77
, testName(testName_)
88
, filename(filename_)
99
, lineNumber(lineNumber_)
10+
, timeConstraintExempt(false)
1011
{
1112
}
1213

@@ -15,6 +16,7 @@ TestDetails::TestDetails(const TestDetails& details, int lineNumber_)
1516
, testName(details.testName)
1617
, filename(details.filename)
1718
, lineNumber(lineNumber_)
19+
, timeConstraintExempt(details.timeConstraintExempt)
1820
{
1921
}
2022

src/TestDetails.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class UNITTEST_LINKAGE TestDetails
1515
char const* const testName;
1616
char const* const filename;
1717
int const lineNumber;
18+
mutable bool timeConstraintExempt;
1819

1920
TestDetails(TestDetails const&); // Why is it public? --> http://gcc.gnu.org/bugs.html#cxx_rvalbind
2021
private:

src/TestRunner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void TestRunner::RunTest(TestResults* const result, Test* const curTest, int con
6767
curTest->Run();
6868

6969
double const testTimeInMs = testTimer.GetTimeInMs();
70-
if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_timeConstraintExempt)
70+
if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_details.timeConstraintExempt)
7171
{
7272
MemoryOutStream stream;
7373
stream << "Global time constraint failed. Expected under " << maxTestTimeInMs <<

src/TimeConstraint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class UNITTEST_LINKAGE TimeConstraint
2929

3030
#define UNITTEST_TIME_CONSTRAINT_EXEMPT() \
3131
UNITTEST_MULTILINE_MACRO_BEGIN \
32-
m_timeConstraintExempt = true; \
32+
m_details.timeConstraintExempt = true; \
3333
UNITTEST_MULTILINE_MACRO_END
3434

3535
}

src/tests/TestTestRunner.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,24 +207,30 @@ TEST_FIXTURE(TestRunnerFixture, SlowTestHasCorrectFailureInformation)
207207
CHECK(strstr(reporter.lastFailedMessage, "3ms"));
208208
}
209209

210-
TEST_FIXTURE(TestRunnerFixture, SlowTestWithTimeExemptionPasses)
210+
211+
namespace SlowTestHelper
211212
{
212-
class SlowExemptedTest : public Test
213-
{
214-
public:
215-
SlowExemptedTest() : Test("slowexempted", "", 0) {}
216-
virtual void RunImpl() const
217-
{
218-
UNITTEST_TIME_CONSTRAINT_EXEMPT();
219-
TimeHelpers::SleepMs(20);
220-
}
221-
};
213+
TestRunnerFixture testRunnerFixture;
222214

223-
SlowExemptedTest test;
224-
list.Add(&test);
215+
TEST_EX(SlowExemptedTest, testRunnerFixture.list)
216+
{
217+
UNITTEST_TIME_CONSTRAINT_EXEMPT();
218+
TimeHelpers::SleepMs(20);
219+
}
220+
221+
class Fixture {};
222+
223+
TEST_FIXTURE_EX(Fixture, SlowExemptedTest, testRunnerFixture.list)
224+
{
225+
UNITTEST_TIME_CONSTRAINT_EXEMPT();
226+
TimeHelpers::SleepMs(20);
227+
}
228+
}
225229

226-
runner.RunTestsIf(list, NULL, True(), 3);
227-
CHECK_EQUAL(0, reporter.testFailedCount);
230+
TEST(SlowTestsWithTimeExemptionPass)
231+
{
232+
SlowTestHelper::testRunnerFixture.runner.RunTestsIf(SlowTestHelper::testRunnerFixture.list, NULL, True(), 3);
233+
CHECK_EQUAL(0, SlowTestHelper::testRunnerFixture.reporter.testFailedCount);
228234
}
229235

230236
struct TestSuiteFixture

src/tests/TestTimeConstraintMacro.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,27 @@ TEST(TimeConstraintMacroComparesAgainstPreciseActual)
6262
CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroComparesAgainstPreciseActual"));
6363
}
6464

65+
struct EmptyFixture {};
66+
67+
TEST_FIXTURE(EmptyFixture, TimeConstraintMacroWorksInFixtures)
68+
{
69+
int testLine = 0;
70+
RecordingReporter reporter;
71+
72+
{
73+
UnitTest::TestResults testResults(&reporter);
74+
ScopedCurrentTest scopedResults(testResults);
75+
76+
UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__;
77+
UnitTest::TimeHelpers::SleepMs(20);
78+
}
79+
80+
using namespace std;
81+
82+
CHECK_EQUAL(1, reporter.testFailedCount);
83+
CHECK(strstr(reporter.lastFailedFile, __FILE__));
84+
CHECK_EQUAL(testLine, reporter.lastFailedLine);
85+
CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroWorksInFixtures"));
86+
}
87+
6588
}

0 commit comments

Comments
 (0)