+ All Categories
Home > Technology > CppUnit using introduction

CppUnit using introduction

Date post: 18-Jun-2015
Category:
Upload: iuriikiyan
View: 10,935 times
Download: 0 times
Share this document with a friend
Description:
Introduction to using CppUnit unit-testing framework.
Popular Tags:
13
CppUnit © 2007-2008 Iurii Kiyan CppUnit usage Workshop
Transcript
Page 1: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

CppUnit usage Workshop

Page 2: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

Overview● Introduction● Using CppUnit framework classes for unit testing

● TestCase● TestRunner● TestFixture● TestSuite● Helper Macros

● Integration CppUnit in build process● References

Page 3: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

Introduction

●JUnit port of Michael Feathers●Jerome Lacoste provided port for Unix/Solaris

Page 4: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

Base Classes

Page 5: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

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)) );    }};

Page 6: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

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( "", true );

Page 7: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

Test Fixture

Specialized class which allows to:1. have called special methods before and after every test2. 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 */ }};

Page 8: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

SuiteSpecialized 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>("test1",                                                                                 &MyTest::test1 ) );suite.addTest( new CppUnit::TestCaller<MyTest>("testAddition",                                                                                  &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.

Page 9: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

TestRunnerTool 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;}

Page 10: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

Helper MacrosMinimize 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() { /* ... */ }

};

Page 11: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

TestFactoryRegistryAllows 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;

};

Page 12: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

Integration in build processint main( int argc, char **argv){

CppUnit::TextUi::TestRunner runner;CppUnit::TestFactoryRegistry &registry =

 CppUnit::TestFactoryRegistry::getRegistry();runner.addTest( registry.makeTest() );bool wasSuccessful = runner.run( "", false );return !wasSuccessful;

};

Page 13: CppUnit using introduction

CppUnit

© 2007­2008 Iurii Kiyan

References

1. http://cppunit.sourceforge.net/2. http://sourceforge.net/projects/cppunit


Recommended