Unit Testing 1
Unit Testing 1
Unit Testing
• Bottom-up approach to testing
• Can incorporate black and glass box
test design
• Automate test execution, report
generation
• Generate confidence that your code
isn’t broken
Software Units
• Smallest testable part of an application
• Procedural - function or procedure
• Object oriented - member function or
method
1
Unit Tests
• Each unit test tests executes a test case
• Units are tested independently of each
other
• Multiple test cases per unit (one per
equivalence class)
• May be difficult to test in isolation.
Test Suite
• A collection of closely related unit tests
– Unit tests for all members of a class
– Unit tests for all functions in a module
– Unit test for all functions related to specific
activity
• A Collection of other test suites
2
Test Fixture
• Code that executes tests (or suites)
– Setup
• Create mock (external) objects
• Create test data
• Create object to be tested
– Execute test
– Teardown
• Delete everything, free resources, close
connections
Structured Fixtures
http://www.codeproject.com/csharp/autp1.asp#What%20Is%20A%20Unit%20Test2
Test Harness
• Software environment for executing
tests
– Executes tests, similar to fixture
– Records test results
– Generates test reports
3
Test Hierarchy
• Unit tests of functions (methods)
• Suites of unit tests cover an entire
modules (classes)
• Suites of class test suites cover entire
subsystems (libraries, components)
• Suits of component tests cover entire
system
• Same as integration testing?
4
Unit Testing Frameworks
• C++
– Boost.Testing library
– CPPUnit
– CxxUnit
• Java
– Junit
• .NET Languages (C#, VB.NET, etc.)
– NUnit
Regression Testing
• During maintenance, add code, cause
bugs
• Code that did work, but doesn’t after
maintenance is called a regression.
• Requires use of version control system
to help track versions and changes
Types of Regressions
• Local - change introduces a new bug
• Unmasked - changes unmask previous
bugs
• Remote - changing one part breaks
another
5
Alternative Classification
• New feature regression - changes break
new code in version
• Existing feature regression - changes
break old code in version
Testing Frameworks
• Common testing frameworks (xUnit)
– Testing tools - assertions, checks,
exceptions, etc.
– Runner - controls execution of tests, traps
exceptions, signals
– Reporting - accumulates error messages,
generates test report data
6
Using Frameworks
• Implement per-class test suite as a fixture
• Unit tests are member functions of fixture
• Setup/Teardown are virtual member functions
(or ctor/dtor).
• Register member functions as tests with the
fixture
• Register the fixture with the test “runner”
Executing Tests
• Two options:
– Execute each fixture as its own program
– Register related of fixtures with a top-level
runner, execute that
• Result is always a (large) number of
executable programs
Executing Tests
• Still need programs to:
– Execute fixtures, suites as programs
– Collect output
– Generate system-wide test results
• Other types of tests
– Compilation/Link tests
– Expected failure tests
7
Testing and the Build System
• Testware is software, needs to be built
• Same basic dependency problems as
building software
– code -> exec -> results -> reports
• Most real build systems provide testing
support