Skip to content

Fix unit test time constraint exempt for fixtures #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/TestDetails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ TestDetails::TestDetails(char const* testName_, char const* suiteName_, char con
, testName(testName_)
, filename(filename_)
, lineNumber(lineNumber_)
, timeConstraintExempt(false)
{
}

Expand All @@ -15,6 +16,7 @@ TestDetails::TestDetails(const TestDetails& details, int lineNumber_)
, testName(details.testName)
, filename(details.filename)
, lineNumber(lineNumber_)
, timeConstraintExempt(details.timeConstraintExempt)
{
}

Expand Down
1 change: 1 addition & 0 deletions src/TestDetails.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/TestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<
Expand Down
2 changes: 1 addition & 1 deletion src/TimeConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Expand Down
36 changes: 21 additions & 15 deletions src/tests/TestTestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/tests/TestTimeConstraintMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

}