From 3a5205f7030cb826fa49d827ed9d5f5fb535097f Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Sun, 3 Feb 2013 01:14:24 -0600 Subject: [PATCH 1/2] Added test verifying that UNITTEST_TIME_CONSTRAINT works in TEST_FIXTURE tests. --- src/tests/TestTimeConstraintMacro.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/tests/TestTimeConstraintMacro.cpp b/src/tests/TestTimeConstraintMacro.cpp index bee7dcf..ebb5388 100644 --- a/src/tests/TestTimeConstraintMacro.cpp +++ b/src/tests/TestTimeConstraintMacro.cpp @@ -62,4 +62,27 @@ TEST(TimeConstraintMacroComparesAgainstPreciseActual) CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroComparesAgainstPreciseActual")); } +struct EmptyFixture {}; + +TEST_FIXTURE(EmptyFixture, TimeConstraintMacroWorksInFixtures) +{ + int testLine = 0; + RecordingReporter reporter; + + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__; + UnitTest::TimeHelpers::SleepMs(20); + } + + using namespace std; + + CHECK_EQUAL(1, reporter.testFailedCount); + CHECK(strstr(reporter.lastFailedFile, __FILE__)); + CHECK_EQUAL(testLine, reporter.lastFailedLine); + CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroWorksInFixtures")); +} + } From 79172d0139f979e6993430155b2bdb688613271d Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Sun, 3 Feb 2013 01:38:16 -0600 Subject: [PATCH 2/2] Refactored SlowTestWithTimeExemptionPass into two tests, defined in the SlowTestHelper namespace. These tests are actually run by new test SlowTestsWithTimeExemptionsPass. --- src/Test.cpp | 1 - src/Test.h | 2 +- src/TestDetails.cpp | 2 ++ src/TestDetails.h | 1 + src/TestRunner.cpp | 2 +- src/TimeConstraint.h | 2 +- src/tests/TestTestRunner.cpp | 36 +++++++++++++++++++++--------------- 7 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/Test.cpp b/src/Test.cpp index 352ba7e..6a270db 100644 --- a/src/Test.cpp +++ b/src/Test.cpp @@ -21,7 +21,6 @@ TestList& Test::GetTestList() Test::Test(char const* testName, char const* suiteName, char const* filename, int lineNumber) : m_details(testName, suiteName, filename, lineNumber) , m_nextTest(0) - , m_timeConstraintExempt(false) , m_isMockTest(false) { } diff --git a/src/Test.h b/src/Test.h index f51d8e8..bf7c4a3 100644 --- a/src/Test.h +++ b/src/Test.h @@ -17,7 +17,7 @@ class UNITTEST_LINKAGE Test TestDetails const m_details; Test* m_nextTest; - mutable bool m_timeConstraintExempt; + mutable bool m_isMockTest; static TestList& GetTestList(); diff --git a/src/TestDetails.cpp b/src/TestDetails.cpp index a13a168..3af0446 100644 --- a/src/TestDetails.cpp +++ b/src/TestDetails.cpp @@ -7,6 +7,7 @@ TestDetails::TestDetails(char const* testName_, char const* suiteName_, char con , testName(testName_) , filename(filename_) , lineNumber(lineNumber_) + , timeConstraintExempt(false) { } @@ -15,6 +16,7 @@ TestDetails::TestDetails(const TestDetails& details, int lineNumber_) , testName(details.testName) , filename(details.filename) , lineNumber(lineNumber_) + , timeConstraintExempt(details.timeConstraintExempt) { } diff --git a/src/TestDetails.h b/src/TestDetails.h index 50b630c..eb7d596 100644 --- a/src/TestDetails.h +++ b/src/TestDetails.h @@ -15,6 +15,7 @@ class UNITTEST_LINKAGE TestDetails char const* const testName; char const* const filename; int const lineNumber; + mutable bool timeConstraintExempt; TestDetails(TestDetails const&); // Why is it public? --> http://gcc.gnu.org/bugs.html#cxx_rvalbind private: diff --git a/src/TestRunner.cpp b/src/TestRunner.cpp index 10f47e4..a8151e6 100644 --- a/src/TestRunner.cpp +++ b/src/TestRunner.cpp @@ -67,7 +67,7 @@ void TestRunner::RunTest(TestResults* const result, Test* const curTest, int con curTest->Run(); double const testTimeInMs = testTimer.GetTimeInMs(); - if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_timeConstraintExempt) + if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_details.timeConstraintExempt) { MemoryOutStream stream; stream << "Global time constraint failed. Expected under " << maxTestTimeInMs << diff --git a/src/TimeConstraint.h b/src/TimeConstraint.h index a73057c..8c06913 100644 --- a/src/TimeConstraint.h +++ b/src/TimeConstraint.h @@ -29,7 +29,7 @@ class UNITTEST_LINKAGE TimeConstraint #define UNITTEST_TIME_CONSTRAINT_EXEMPT() \ UNITTEST_MULTILINE_MACRO_BEGIN \ - m_timeConstraintExempt = true; \ + m_details.timeConstraintExempt = true; \ UNITTEST_MULTILINE_MACRO_END } diff --git a/src/tests/TestTestRunner.cpp b/src/tests/TestTestRunner.cpp index b607ec6..5b2d65a 100644 --- a/src/tests/TestTestRunner.cpp +++ b/src/tests/TestTestRunner.cpp @@ -207,24 +207,30 @@ TEST_FIXTURE(TestRunnerFixture, SlowTestHasCorrectFailureInformation) CHECK(strstr(reporter.lastFailedMessage, "3ms")); } -TEST_FIXTURE(TestRunnerFixture, SlowTestWithTimeExemptionPasses) + +namespace SlowTestHelper { - class SlowExemptedTest : public Test - { - public: - SlowExemptedTest() : Test("slowexempted", "", 0) {} - virtual void RunImpl() const - { - UNITTEST_TIME_CONSTRAINT_EXEMPT(); - TimeHelpers::SleepMs(20); - } - }; + TestRunnerFixture testRunnerFixture; - SlowExemptedTest test; - list.Add(&test); + TEST_EX(SlowExemptedTest, testRunnerFixture.list) + { + UNITTEST_TIME_CONSTRAINT_EXEMPT(); + TimeHelpers::SleepMs(20); + } + + class Fixture {}; + + TEST_FIXTURE_EX(Fixture, SlowExemptedTest, testRunnerFixture.list) + { + UNITTEST_TIME_CONSTRAINT_EXEMPT(); + TimeHelpers::SleepMs(20); + } +} - runner.RunTestsIf(list, NULL, True(), 3); - CHECK_EQUAL(0, reporter.testFailedCount); +TEST(SlowTestsWithTimeExemptionPass) +{ + SlowTestHelper::testRunnerFixture.runner.RunTestsIf(SlowTestHelper::testRunnerFixture.list, NULL, True(), 3); + CHECK_EQUAL(0, SlowTestHelper::testRunnerFixture.reporter.testFailedCount); } struct TestSuiteFixture