CppUnit using introduction

  • 10,785 views
Uploaded on

Introduction to using CppUnit unit-testing framework.

Introduction to using CppUnit unit-testing framework.

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • well done! easy to understand.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
10,785
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
243
Comments
1
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. CppUnit CppUnit usage  Workshop © 2007­2008 Iurii Kiyan
  • 2. CppUnit Overview ● Introduction ● Using CppUnit framework classes for unit testing ● TestCase ● TestRunner ● TestFixture ● TestSuite ● Helper Macros ● Integration CppUnit in build process ● References © 2007­2008 Iurii Kiyan
  • 3. CppUnit Introduction ● JUnit port of Michael Feathers ● Jerome Lacoste provided port for Unix/Solaris © 2007­2008 Iurii Kiyan
  • 4. CppUnit Base Classes © 2007­2008 Iurii Kiyan
  • 5. CppUnit Simplest unit­test #include <cppunit/TestCase.h> class MyTest : public CppUnit::TestCase  {    public:       MyTest( )  : CppUnit::TestCase( “MyTest” ) {}         void runTest()       {         CPPUNIT_ASSERT( Complex (10, 1) == Complex (10, 1) );         CPPUNIT_ASSERT( !(Complex (1, 1) == Complex (2, 2)) );     } }; © 2007­2008 Iurii Kiyan
  • 6. CppUnit Simplest unit­test running #include <cppunit/TestCase.h> #include <cppunit/TextTestRunner.h> class MyTest : public CppUnit::TestCase { /* ... */ } MyTest t; CppUnit::TextTestRunner r; r.addTest(&t); r.run( quot;quot;, true ); © 2007­2008 Iurii Kiyan
  • 7. CppUnit Test Fixture Specialized class which allows to: 1. have called special methods before and after every test 2. have several tests defined in the same class as public methods with  signature: void method() class SomeTest : public CppUnit::TestFixture  {   public:     void virtual setUp(){ /* initialization of required members */  }     void virtual tearDown(){ /* cleanup of required members */ } }; © 2007­2008 Iurii Kiyan
  • 8. CppUnit Suite Specialized class to group tests and run them as a single unit (collection  for classes which implements Test interface). CppUnit::TestSuite suite; suite.addTest(new CppUnit::TestCaller<MyTest>(quot;test1quot;,                                                                                  &MyTest::test1 ) ); suite.addTest( new CppUnit::TestCaller<MyTest>(quot;testAdditionquot;,                                                                                   &MyTest::test2 ) ); CppUnit::TestResult result; suite.run( &result ); Usefull to have specialized static method in test classes “Suite suite()”  which contains code for Suite creation. © 2007­2008 Iurii Kiyan
  • 9. CppUnit TestRunner Tool to run tests and display test results. int main( int argc, char **argv) {   CppUnit::TextUi::TestRunner runner;   runner.addTest( MyTests1::suite() );   runner.addTest( MyTests2::suite() );   runner.run();   return 0; } © 2007­2008 Iurii Kiyan
  • 10. CppUnit Helper Macros Minimize coding errors by hiding tests creation into macros. #include <cppunit/extensions/HelperMacros.h> class SampleTest : public CppUnit::TestFixture   { CPPUNIT_TEST_SUITE( SampleTest ); CPPUNIT_TEST( test1 ); CPPUNIT_TEST_SUITE_END(); public: void setUp()  { /*...*/ } void tearDown()  { /* ... */ } void test1() { /* ... */ } }; © 2007­2008 Iurii Kiyan
  • 11. CppUnit TestFactoryRegistry Allows to simplify running all test (creates collection of test classes). #include <cppunit/extensions/HelperMacros.h> CPPUNIT_TEST_SUITE_REGISTRATION( MyTest );//for every testset int main( int argc, char **argv) { CppUnit::TextUi::TestRunner runner; CppUnit::TestFactoryRegistry &registry =  CppUnit::TestFactoryRegistry::getRegistry(); runner.addTest( registry.makeTest() ); runner.run(); return 0; }; © 2007­2008 Iurii Kiyan
  • 12. CppUnit Integration in build process int main( int argc, char **argv) { CppUnit::TextUi::TestRunner runner; CppUnit::TestFactoryRegistry &registry =  CppUnit::TestFactoryRegistry::getRegistry(); runner.addTest( registry.makeTest() ); bool wasSuccessful = runner.run( quot;quot;, false ); return !wasSuccessful; }; © 2007­2008 Iurii Kiyan
  • 13. CppUnit References 1. http://cppunit.sourceforge.net/ 2. http://sourceforge.net/projects/cppunit © 2007­2008 Iurii Kiyan