Cpp unit


Published on

Presentation was given as part of a workshop conducted for developers to help them explain unit testing and CPPUnit framework

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Cpp unit

  1. 1. Dev Unit Testing:CppUnit WorkshopMudabbir Warsi (SHAMU04)December 29,2010
  2. 2. Agenda• Introduction to Unit Testing• Introduction to CppUnit• Important aspects of CppUnit • Writing Test Cases • Launching Tests • Collecting and Reporting Test Results• CppUnit Workshop • Demo • How to write unit tests for existing code? • Integration with the Build process• Introduction to Stubs and Mock objects• Q&A 2 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  3. 3. Introduction• Traditional testing methods • Stepping through a debugger • Littering code with stream out put calls• What is a “Unit Test”? “A Unit Test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested”.• Why Unit Testing? • Speed up the development – Write tests once, use tests to find errors many times • Less time spent on debugging – Incorrect changes discovered immediately • Tests as documentation. Provides a working specification of the functional code • Gain Confidence in your code• Excuses for NOT (Unit) testing? • It takes too much time to write the tests • I am being paid to write code, not to write tests • I feel guilty about putting QA staff out of work 3 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  4. 4. Introduction to CppUnit• Trivia • Kent Beck published the first unit test framework for SmallTalk in 1999 • CppUnit is a member of xUnit family (SUnit, JUnit, NUnit, pyUnit, vbUnit etc) • CppUnit started as a port of JUnit to C++ by Michael Feathers • Motto of CppUnit: “Testing: Early, Often and Automated”• Important Classes of xUnit Framework • TestCase : Represents a single test case • TestSuite : A collection of Test Cases • TestRunner : A Launcher of Test Suites • TestResult : Collects errors/failures that occur during a test • TestListener : Listener of events that occur during a test 4 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  5. 5. Writing Test Cases• Class “Test” represents one individual test. All test cases should be a sub-class of Test. Its an Abstract base class. Test• Class “TestFixture” provides a common run(TestResult*):void environment OR common resources for a set of TestFixture test cases. TestLeaf Setup() TearDown()• Class “TestLeaf” is a base class for single test case. A test that is not composed of other test cases. TestCase run(TestResult*):void runTest() : void• Class “TestCase” is the parent of all Unit Tests. All unit tests should inherit from this class and should MyTestCase override the runTest() method runTest() : void 5 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  6. 6. Writing Test Cases….Contd• A Test Suite is a container that collects several tests and runs them as a single unit.• Class “TestComposite” represents a Test 1..* base class for collection of test cases. run(TestResult*):void Subclass this to implement a custom Test Suite. TestComposite• Class “TestSuite” represents a run(TestResult*) : void collection of Test Cases that are run as a single unit. TestSuite MyTestSuite addTest(Test*) : void addTest(Test*) : void 1 doGetChildTestAt(int) : Test* doGetChildTestAt(int) : Test* vector<Test*> suite deque<Test*> suite 6 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  7. 7. Launching Tests with TestRunner• Class “TestRunner” executes the test cases and provides statistical information regarding the outcome of the test execution.• CppUnit provides different test runners. • TestRunner : Generic test runner • TextTestRunner : A text mode test runner • MFCTestRunner : GUI based test runner • QTTestRunner : GUI based test runner for QT framework• Unlike other elements of the framework, CppUnit does not provide any interface for creating custom Test Runners.• Important method of TestRunner Class TestRunner::run(TestResult&); // Runs the test using the specified controller 7 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  8. 8. Collecting and Reporting Test Results• Collecting Results with TestResult A TestResult collects the results of executing a collection of test cases. It counts the tests run and collects the failures and errors so that the framework can report them.• Observing Results with TestListeners TestListeners provides the ability for the Framework users to be notified about the different events that occur while execution of a test case. Eg: Start of a test case, end of a test case, start of test suite, End of a test suite• Printing Results with Outputters Outputters print the summary of the Test Results in various formats (XML Format, Text Format etc). It can be extended to print result summary in any custom format. Eg: TextOutputter, XmlOutputter, CompilerOutputter 8 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  9. 9. CppUnit Workshop• DEMO• How to write unit tests for existing code?• Integration with the Build process 9 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  10. 10. Introduction to Stubs and MOCK objects• Stub is a piece of code that replaces the real code, in order to isolate the calling code from the real implementation. • Advantages of STUBS • Replace complex behavior with a simple one • Allows for coarse-grained Isolation • Disadvantages of STUBS • Complex to write • Difficult to debug and maintain• A Mock object is an object created to stand in for an object that the code being tested will be collaborating with. • Advantages of MOCK objects • Easier to write and maintain. • Allows for fine-grained Isolation • Disadvantages of MOCK objects • There may be many objects to Mock • Forces to re write some of the code under test. 10 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  11. 11. Thank You !!