PB4 WhyUnitTesting
PB4 WhyUnitTesting
Contents
Introduction............................................................. 1
What is a unit test? ................................................. 1
Definition of an ideal unit test............................. 1
A pragmatic approach ........................................ 2
Code coverage................................................... 3
Benefits of unit tests ............................................... 3
Problems and limitations ........................................ 4
Enable unit testing .................................................. 5
Success stories ...................................................... 6
References ............................................................. 7
Appendix ................................................................ 8
Introduction
Even if software is developed since quite a
while software development organizations still
struggle to create software in reliable quality. The
numbers of bugs of a piece of code delivered from
a programmer to others is still high. Agile methods
have been put into practice more and more in the
last decade. Extreme programming (XP)
introduced in [Beck04] is a specific agile approach
with a strong emphasis on testing. It puts testing at
the foundation of development with every
programmer writing (unit) tests as they write their
production code.
In the second chapter we define an ideal unit
test as well as a corresponding pragmatic
approach. Since the question of code coverage
and its goals is asked repeatedly, we also give a
recommendation on this topic. The benefits of unit
tests, like shifting from defect detection to defect
prevention, are covered in the third chapter,
whereas the following fourth chapter depicts
problems and limitations coming along with unit
tests. We close with highlighting some topics to be
tackled to enable unit testing at SAP. Last but not
least you will find brief success stories on unit
testing outside SAP. The appendix contains
some more definitions of unit tests in literature.
2 Grey box test combines advantages of white box and black box tests. It is coded by
a developer BEFORE implementation of the specification.
3 A test fixture is all the things we need to have in place in order to run a test and
expect a particular outcome.
It is easy to implement
It runs quickly
6 Despite its name, TDD is not a testing technique, but rather a development and
design technique. Further reading in general: [Meszaro08b] and [Erdogamus05]; on
empirical evidence on TDD: [Siniaalto06] and [nagappan08]
7 In xUnit, a test fixture is all the things we need to have in place in order to run a
test and expect a particular outcome. Some people call this the test context. Some
variants of xUnit keep the concept of the test context separate from the Testcase Class
test.
that creates it; JUnit and its direct ports fall into this camp. Setting up the test fixture is
SAP AG 2009
It is easy to read/understand
Code coverage
Usually unit testing is biased to looking at code
as it is implemented; therefore its thoroughness is
usually measured by code coverage (aka test
coverage). Code coverage measurement simply
determines those statements in a body of code
that have been executed through a test run and
those which have not; it is a method to assure the
quality of your set of tests (avoiding unit test
atrophy during development cycle) and to identify
gaps in your test suite, but not (!) the quality of
your product. This does not mean that a
component with high coverage is in a good shape
from quality perspective. It rather highlights parts
of your coding without tests.
Code coverage could be used for minimizing
and finding untested code.
Code coverage enables you to detect coding
what is probably not feature or requirement
relevant, since the effort for writing tests is for most
developers still too high to write tests for coding
nobody asks for. It also could be useful for identify
potential dead coding.
Contra-productive usage of the measurement
results is to define general KPI's of high coverage,
e.g. 80%. This leads to not implementing
meaningful tests if they do not lead to increased
coverage.
Usually the question is raised what coverage
goals to choose. There are no scientifically proven
papers out recommending any number to be set.
The goals for legacy coding and newly written
code might differ; it probably does not make sense
the first phase of the Four-Phase Test. For meanings of the term test fixture in other
contexts, please see test fixture (disambiguation). [Meszaro08]
SAP AG 2009
8 http://www.rtca.org/
SAP AG 2009
Makes Development Faster. On a class-byclass basis, testing slows you down. It takes time
to think about and produce good tests. But as time
goes on, your overall velocity increases because
you are not constantly worrying about breaking
existing code as you add new features.
We have also found that with good tests, we
can ignore internal implementation details during
the first iteration. Provided that we get the public
API right, we can improve internal design and
algorithms later, again without fear of breaking
something. We have used this specifically for
things like sorting algorithms. In the first iteration,
we do something quick and dirty, like a bubble
sort. We can always come back later and replace it
with
a
better
algorithm,
if
necessary.
([BurkeCoyner03]). Besides that, time used for
debugging will be reduced.
Enables low level testing. Unit tests,
especially white box tests, allow you to test internal
conditions that are not easily reached by external
inputs, whether all error cases are correctly
detected and if all special cases work correctly.
Ease higher level testing. Even higher level
tests like integration or scenario tests benefit from
the safety net unit tests provide since each unit
works in separation. Therefore integration tests
can focus on the cooperation of individual parts.
Easy to maintain, easy to run, but hard to
develop for non-trivial code. In comparison to
higher level tests like UI based QTP tests, unit
tests have a very low effort for maintenance
whereas currently it is difficult to keep a UI based
QTP test stable. Unit test also do not require
properly configured, heavyweight systems; instead
they simply can be executed fast and reliable in
very simple test landscapes.
Can I run all the unit tests Ive written at the push
of a button?
9 http://sourceforge.net/projects/mockobjects
10 http://easymock.org/
11 http://www.jmock.org/
SAP AG 2009
12
or
Success stories
Finally, this section gives you an overview of
known success stories in respect of unit testing
also combined with the TDD approach.
IBM / North Carolina State University. A
development team in IBM transitioned from an
adhoc to a TDD unit testing practice []. Through
the introduction of this practice a relatively
inexperienced team realized about 40% reduction
16
in FVT detected defect density of new/changed
code []. Additionally, the suite of automated unit
test cases created via TDD became a reusable
and extendable asset that will continue to improve
12 http://www.picocontainer.org/
13 http://code.google.com/p/google-guice/
14 http://www.dbunit.org/
15 http://ejb3unit.sourceforge.net/
16 Note: functional verification test
SAP AG 2009
References
[Beck04]
SAP AG 2009
osherove_meapch1.pdf, 2009-0406
[RTCA/DO-178B] RTCA/DO-178B, Software
Considerations in Airborne
Systems and Equipment
Certification,
http://www.do178site.com/do178b_
structural_coverage.php, 2009-0415
Implementation of business object
[SAP09]
Purchase Order of the proxy
application Enterprise
Procurement Model (EPM), SAP,
2009,
\\production.wdf.sap.corp\depot\inf
o\NW_E_TI\Projects\Test Tools
Automation NW
Strategy\020_Code_Coverage\020
_Workpackages\MD30\_ALL\100_
CaseStudy_UnitTestImplementatio
n_EPM_PO.ppt, 2009-04-14
[Siniaalto06]
Maria Siniaalto. Agile Deliverable
D.2.7, Test driven
development: empirical body of
evidence,
Juergen Staader. Attention ABAP
[Staader05]
Developers! Boost the quality of
Your Programs by Writing
Automated Unit Tests with ABAP
Unit, SAP Professional Journal,
2005
[Williams03]
Laurie Williams, E. Michael
Maximilien, Mladen Vouk.TestDriven Development as a DefectReduction Practice, Proceedings of
the 14th International Symposium
on Software Reliability
Engineering, 2003, from
http://portal.acm.org/citation.cfm?id
=952364, 2009-04-16
Appendix
The following section only contains some
additional definitions which have been removed
from the paper.
The ANSI/IEEE standard for unit testing
[IEEE87] defines the term test unit as A set of
one or more computer program modules together
with associated control data, (for example, tables),
usage procedures, and operating procedures that
satisfy the following conditions:
[IEEE87]
[Meszaro08]
[Oracle04]
[Osherove08]
[SAP07]