CppUnit using introduction
Upcoming SlideShare
Loading in...5
×
 

CppUnit using introduction

on

  • 15,065 views

Introduction to using CppUnit unit-testing framework.

Introduction to using CppUnit unit-testing framework.

Statistics

Views

Total Views
15,065
Views on SlideShare
15,005
Embed Views
60

Actions

Likes
2
Downloads
242
Comments
1

5 Embeds 60

http://www.slideshare.net 55
http://pinterest.com 2
http://www.docshut.com 1
http://www.linkedin.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • well done! easy to understand.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

CppUnit using introduction CppUnit using introduction Presentation Transcript

  • CppUnit CppUnit usage  Workshop © 2007­2008 Iurii Kiyan
  • 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
  • CppUnit Introduction ● JUnit port of Michael Feathers ● Jerome Lacoste provided port for Unix/Solaris © 2007­2008 Iurii Kiyan
  • CppUnit Base Classes © 2007­2008 Iurii Kiyan
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • CppUnit References 1. http://cppunit.sourceforge.net/ 2. http://sourceforge.net/projects/cppunit © 2007­2008 Iurii Kiyan