Testing - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Testing

Description:

Testing the code as the user would see it (black box) CppUnit ... int main( int argc, char* argv[] ) CPPUNIT_NS::TextUi::TestRunner runner; runner.addTest ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 21
Provided by: Mich1010
Category:
Tags: argv | testing

less

Transcript and Presenter's Notes

Title: Testing


1
Testing
  • Unit Testing
  • Testing of a unit of code, usually a class
  • Integration Testing
  • Testing of a module of code (e.g. a package)
  • Application Testing
  • Testing the code as the user would see it (black
    box)

2
CppUnit
  • Automated regression testing framework
  • Part of XUnit family
  • JUnit
  • VBUnit
  • NUnit
  • Many others

3
CppUnit Testing
  • Each class is tested while developed
  • Create tests first
  • Start with simplest that works
  • Incrementally add code while testing
  • Tests serve as benchmark
  • Optimize and refactor without worry

4
Testcase Design
  • You cant test a program completely
  • Equivalence classes
  • They all test the same thing
  • If one test catches a bug, the other probably
    will too
  • If one test doesnt catch a bug, the others
    probably wont either

5
Equivalence Classes
From Testing Computer Software. Kaner, Falk,
and Nguyen. 1999.
6
Common Software Errors
  • Error Handling
  • Error detection/prevention/recovery
  • Boundary-Related Errors
  • Numeric boundaries
  • Off-by-one errors
  • Calculation Errors
  • Overflow and underflow
  • Truncation and round-off errors
  • Incorrect conversions

7
Common Software Errors
  • Initial and Later States
  • Failure to initialize data
  • Failure to initialize loop-control item
  • Failure to initialize pointer
  • Load Conditions
  • Required resource not available
  • No large memory areas available
  • Source, Version, and ID Control
  • Old bugs reappear
  • Manufactured disks contain wrong code or data

8
Creating a Test
  • Subclass the TestCase class
  • Implement test functions
  • Run the test using a TestRunner
  • Group multiple TestCases using TestSuite
  • All of this is made much easier with CppUnit
    Helper Macros

9
Example (header file)
  • include ltcppunit/extensions/HelperMacros.hgt
  • class MyTestCase public CPPUNIT_NSTestFixtur
    e CPPUNIT_TEST_SUITE( MyTestCase )
    CPPUNIT_TEST( testOne ) CPPUNIT_TEST( testTwo
    ) CPPUNIT_TEST_SUITE_END()protected void
    testOne() void testTwo()

10
Example (implementation file)
  • CPPUNIT_TEST_SUITE_REGISTRATION(MyTestCase)voi
    d MyTestCasetestOne() // test code goes
    herevoid MytestCasetestTwo() // test
    code goes here

11
Example (driver)
  • include ltcppunit/ui/text/TestRunner.hgtinclude
    ltcppunit/extensions/TestFactoryRegistry.hgtint
    main( int argc, char argv )
    CPPUNIT_NSTextUiTestRunner runner
    runner.addTest( CPPUNIT_NSTestFactoryRegistry
    getRegistry().makeTest() ) return
    runner.run() ? 0 1

12
Checking Results
  • CPPUNIT_ASSERT(condition)
  • CPPUNIT_ASSERT_MESSAGE(message, condition)
  • CPPUNIT_ASSERT_EQUAL(expected, actual)
  • CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected,
    actual)
  • CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual,
    delta)
  • CPPUNIT_FAIL(message)

13
Checking Results Example
  • void testEquality() Money a(1, 50) Money
    b(1, 50) CPPUNIT_ASSERT(a b)

14
Test Fixture
  • setUp()
  • Initialize member variables common to all tests
  • Called once before each test
  • tearDown()
  • Release resources
  • Called once after each test

15
  • class ShoppingCartTest() public
    ShoppingCart m_cart Product
    m_book void setUp() m_cart new
    ShoppingCart() m_book new Product(Unit
    Test) m_cart-gtAddItem(book) void
    tearDown() delete cart delete book
    void testAddItem() void
    testRemoveItem()

16
Missing Resource
  • Implementation requires a resource that is not
    available
  • Create stub that returns anticipated results
  • Class TextFilepublic bool
    m_isFileAvailable bool OpenFile(const char
    filename) return m_isFileAvailable

17
Handling New Bugs
  • If a bug is found
  • Add it to the test suite
  • Then correct bug

18
UCS Configuration
  • .cshrc
  • setenv LD_LIBRARY_PATH /pkgs2/cppunit-1.10.2/lib
  • .bashrc
  • export LD_LIBRARY_PATH/pkgs2/cppunit-1.10.2/lib

19
Resources
  • http//cppunit.sourceforge.net/doc/1.8.0/cppunit_c
    ookbook.html
  • /pkgs2/cppunit-1.10.2/include/cppunit
  • TestAssert.h (and others)
  • /pkgs2/cppunit-1.10.2/include/cppunit/extensions
  • HelperMacros.h (and others)

20
Sample Class for Testing
  • class Moneypublic // Creates new
    initialized Money object // dollars range 0,
    inf) // cents - range 0, 99 Money(int
    dollars, int cents) bool operator(const
    Money rhs) stdstring ToString() const
Write a Comment
User Comments (0)
About PowerShow.com