Created
March 9, 2016 08:03
-
-
Save mrafferson/988177d525052b763b0d to your computer and use it in GitHub Desktop.
Testing Suite with SCons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cppunit/extensions/HelperMacros.h> | |
#include "MyTestClass.h" | |
class FixtureTest : public CPPUNIT_NS::TestFixture | |
{ | |
CPPUNIT_TEST_SUITE( FixtureTest ); | |
CPPUNIT_TEST( Test1 ); | |
CPPUNIT_TEST( Test2 ); | |
CPPUNIT_TEST( Test3 ); | |
CPPUNIT_TEST_SUITE_END(); | |
protected: | |
float someValue; | |
std::string str; | |
MyTestClass myObject; | |
public: | |
void setUp(); | |
protected: | |
void Test1(); | |
void Test2(); | |
void Test3(); | |
}; | |
CPPUNIT_TEST_SUITE_REGISTRATION( FixtureTest ); | |
void FixtureTest::setUp() | |
{ | |
someValue = 2.0; | |
str = "Hello"; | |
} | |
void FixtureTest::Test1() | |
{ | |
CPPUNIT_ASSERT_DOUBLES_EQUAL( someValue, 2.0f, 0.005f ); | |
someValue = 0; | |
//System exceptions cause CppUnit to stop dead on its tracks | |
//myObject.UseBadPointer(); | |
// A regular exception works nicely though | |
myObject.ThrowException(); | |
} | |
void FixtureTest::Test2() | |
{ | |
CPPUNIT_ASSERT_DOUBLES_EQUAL( someValue, 2.0f, 0.005f ); | |
CPPUNIT_ASSERT_EQUAL (str, std::string("Hello")); | |
} | |
void FixtureTest::Test3() | |
{ | |
// This also causes it to stop completely | |
//myObject.DivideByZero(); | |
// Unfortunately, it looks like the framework creates 3 instances of MyTestClass | |
// right at the beginning instead of doing it on demand for each test. We would | |
// have to do it dynamically in the setup/teardown steps ourselves. | |
// CPPUNIT_ASSERT_EQUAL (1, myObject.s_currentInstances); | |
// CPPUNIT_ASSERT_EQUAL (3, myObject.s_instancesCreated); | |
// CPPUNIT_ASSERT_EQUAL (1, myObject.s_maxSimultaneousInstances); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cppunit/BriefTestProgressListener.h> | |
#include <cppunit/CompilerOutputter.h> | |
#include <cppunit/extensions/TestFactoryRegistry.h> | |
#include <cppunit/TestResult.h> | |
#include <cppunit/TestResultCollector.h> | |
#include <cppunit/TestRunner.h> | |
int | |
main( int argc, char* argv[] ) | |
{ | |
// Create the event manager and test controller | |
CPPUNIT_NS::TestResult controller; | |
// Add a listener that colllects test result | |
CPPUNIT_NS::TestResultCollector result; | |
controller.addListener( &result ); | |
// Add a listener that print dots as test run. | |
//CPPUNIT_NS::BriefTestProgressListener progress; | |
//controller.addListener( &progress ); | |
//controller.push | |
// Add the top suite to the test runner | |
CPPUNIT_NS::TestRunner runner; | |
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() ); | |
runner.run( controller ); | |
// Print test in a compiler compatible format. | |
CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr ); | |
outputter.write(); | |
return result.wasSuccessful() ? 0 : 1; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MyTestClass.h" | |
#include <algorithm> | |
#include <stdexcept> | |
int MyTestClass::s_currentInstances; | |
int MyTestClass::s_instancesCreated; | |
int MyTestClass::s_maxSimultaneousInstances; | |
MyTestClass::MyTestClass() | |
{ | |
++s_currentInstances; | |
++s_instancesCreated; | |
s_maxSimultaneousInstances = std::max(s_maxSimultaneousInstances, s_currentInstances); | |
} | |
MyTestClass::~MyTestClass() | |
{ | |
--s_currentInstances; | |
} | |
bool MyTestClass::UseBadPointer() const | |
{ | |
int * p = 0; | |
*p = 32; | |
} | |
bool MyTestClass::DivideByZero() const | |
{ | |
int i = 1, j = 0; | |
i = i / j; | |
} | |
void MyTestClass::ThrowException() const | |
{ | |
throw(std::bad_alloc()); | |
} | |
int main(int argc, char const *argv[]) { | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MYTESTCLASS_H | |
#define MYTESTCLASS_H | |
class MyTestClass | |
{ | |
public: | |
MyTestClass(); | |
~MyTestClass(); | |
bool UseBadPointer() const; | |
bool DivideByZero() const; | |
void ThrowException() const; | |
static int s_currentInstances; | |
static int s_instancesCreated; | |
static int s_maxSimultaneousInstances; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
env = Environment( CXX="clang++" ) | |
flags = [ | |
"-std=c++11", | |
"-Wall", | |
"-O3", | |
] | |
libs = [ "-lcppunit" ] | |
test_sources = [ | |
"main.cpp", | |
"simplest_test.cpp", | |
"fixture_test.cpp", | |
] | |
main_sources = [ | |
"MyTestClass.cpp", | |
] | |
env.Program( target="program", source=main_sources, CXXFLAGS=flags, LIBS=libs ) | |
program = env.Program( target="test", source=test_sources, CXXFLAGS=flags, LIBS=libs ) | |
test_alias = Alias( "test", [program], program[0].path ) | |
AlwaysBuild( test_alias ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cppunit/extensions/HelperMacros.h> | |
class SimplestCase : public CPPUNIT_NS::TestFixture | |
{ | |
CPPUNIT_TEST_SUITE( SimplestCase ); | |
CPPUNIT_TEST( MyTest ); | |
CPPUNIT_TEST_SUITE_END(); | |
protected: | |
void MyTest(); | |
}; | |
CPPUNIT_TEST_SUITE_REGISTRATION( SimplestCase ); | |
void SimplestCase::MyTest() | |
{ | |
float fnum = 2.00001f; | |
CPPUNIT_ASSERT_DOUBLES_EQUAL( fnum, 2.0f, 0.0005 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment