Unit Testing With CppUnit
Unit Testing With CppUnit
Software Testing
Fundamentals (1)
z What is software testing?
z The process of operating a system or component under
specified conditions, observing or recording the results,
and making an evaluation of some aspect of system or
component. (IEEE Definition)
z Why do we test?
z To ensure the quality and satisfaction of the product
z To gain the confidence in the correctness of the product
z Testing vs. debugging
z Testing is to show that a program has bugs
z Debugging is to locate and correct the error or
misconception that cause the program failures
2
Software Testing
Fundamentals (2)
z Software Testing Techniques
z White-box testing
z The tester has access to the details of the
program under test and performs the testing
according to such details.
z Examples: path testing, branch testing
z Black-box testing
z Black-box testing treats the program under test as
a “black box.” No knowledge about the
implementation is assumed.
z Examples: equivalent partitioning, boundary value
analysis
3
Software Testing
Fundamentals (3)
z Testing should begin “in the small” and progress
toward testing “in the large”
z Unit testing
z Concentrates on each unit (i.e., component) of the software
z Integrated testing
z Building a system from its components and testing the
resultant system for detecting component interaction
problems
z System testing
z Concern with testing an increment to be delivered or entire
system
z Acceptance testing
z Performed by the clients/users to confirm that the product
meets the business requirements
4
Unit Testing
z Unit Testing
z Is normally considered as an adjunct to the coding step
z Focuses verification effort on the smallest unit of software
design – the software component or module
z Using the component-level design description as a guide
z Provide a release criterion for a programming task
z Unit Testing in the OO Context
z Smallest testable unit is the encapsulated class
z Conducting class testing (i.e., unit testing)
z Methods within the class are tested
z The state behavior of the class is examined
What is xUnit?
z An automated unit test framework
z Provides the Driver for unit(s)
z Provides automatic test runs
z Provides automatic result checks
z Available for multiple languages:
z JUnit (from Kent Beck (XP) and Erich Gamma(Gang of Four))
z cppUnit
z httpUnit
z NUnit
z …
14
The Goals of JUnit
z To write a framework within which we have some
glimmer of hope that developers will actually write
tests.
z The framework has to use familiar tools, so there is little
new to learn.
z The second goal of testing is creating tests that
retain their value over time.
z Someone other than the original author has to be able to
execute the tests and interpret the results.
z Creating a setup or fixture is expensive
z A framework has to enable reusing fixtures to run different
tests.
15
16
The Patterns Used in JUnit
17
18
How to Use JUnit (2)
z Suite management
z A test suite is a collection of test cases that are intended to be
used to show that a program under test has some specified set of
behaviors
z Write a static suite() containing all the test???() in the fixture
class
z Define a main() method that runs the TestCase in batch mode
z Error vs. Failures
z Error: unanticipated problem like an
ArrayIndexOutOfBoundsException
z Failure: is anticipated and can be checked with assertions
19
21
22
JUnit FAQ: Best Practices
z How often should I run my tests?
z Run all your unit tests as often as possible
z Ideally every time the code is changed.
z Make sure all your unit tests always run at 100%.
z Frequent testing gives you confidence that your changes
didn't break anything.
z For larger systems, you may just run specific test suites
that are relevant to the code you're working on.
z Run all the tests of the a system at least once per day (or
night).
23
void runTest() {
CPPUNIT_ASSERT( Complex (10, 1) == Complex (10, 1) );
CPPUNIT_ASSERT( !(Complex (1, 1) == Complex (2, 2)) );
}
};
26
CppUnit Cookbook (2)
z Fixture - a known set of objects served as a base for a set of test cases
z To add new tests
z Add member variables for each part of the fixture
z Override setUp() to initialize the variables
z Override tearDown() to release any resources allocated in setUp()
32
Unit testing and
Test-driven Development (TDD)
z How to write JUnit/CPPUnit tests? Do it TDD!
z Workflow:
1. Think about functionality to be implemented &
scenarios in which the unit to be tested will play a role.
2. Create a stub of the unit you want to implement.
3. Write a test for each scenario and/or use of the unit.
4. Make the unit fail the tests (nothing is implemented
yet!).
5. Develop the unit until it passes every test.
z Encountered new scenario/use?
z make a test before implementing it!
33
35