Unit Testing 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Unit and Regression Testing

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

Executing Unit Tests


• Tedious to run all tests by hand - should
be automated
• Have hierarchy of tests for a system
– Test suite contains unit tests for a module
– Test suite contains smaller suites for
subsystems
– Entire test suite contains all top-level tests

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?

Benefits of Unit Testing


• Facilitates change - automatically see if
new changes break compilation, tests
• Helps integration testing - layered test
suites actually integrate the system
• Implicit documentation - see how the
code is actually used
• Enforces interface/implementation split

Limitations of Unit Testing


• Gives illusion of integration testing -
doesn’t really test the system
• May not help with performance issues -
automated tests run when you’re not
watching
• Requires disciplined approach to testing
- write tests for all cases, maintain tests
with software

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

Reducing Risk of Regression


• Complete test suite repetition
• Automated test suite execution (unit
tests)
• Partial test automation based on impact
analysis
• Customer/user testing (i.e., betas)

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

You might also like