Performing Unit Testing: 1 Test-Driven Development
Performing Unit Testing: 1 Test-Driven Development
Test-driven development
Traditionally, tests are built and passed after the coding process has been completed -according to requirements- but this is not always the case. In the agile
approaches the test effort is an activity strongly imbricated with the coding. In
particular, the test-first approach uses tests as the vehicle for guarantee because
they simultaneously capture the requirement and the proof of fulfilment.
This exercise focus on this strongly imbricated testing approach. Test-driven
development (TDD) is a software development process that uses the test-first
approach. TDD iterates a short development cycle that starts capturing the
requirements with tests:
1. First the developer writes an automated test case that defines a requirement
(desired improvement or new function),
2. then he verifies that the software passes the test (obviously this initially
fails),
3. if unsuccessful then produces the minimum amount of code to address
the issue and goes back to 2,
4. if successful refactors the new code to acceptable standards and ends the
development cycle.
1
In the words of Kent Beck, TDD encourages simple designs and inspires
confidence [Beck 2003].
Unit testing is a software testing process in which the smallest testable parts
of an application, called units, are individually and independently checked
for proper operation. Unit testing can be done manually -developers use it
continuously- but it is often automated, especially to support automated regression tests1 .
In this exercise we will use a xUnit testing framework to implement a testfirst iteration. xUnit is the collective name for several unit testing frameworks
that derive their structure and functionality from the pure object-oriented Smalltalks
SUnit. SUnit pure object-oriented style enable the easy migration to other more
used languages like Java and C++. The names of many of these frameworks are
usually a variation on SUnit, usually substituting the S for the first letter
(or letters) in the name of their intended language (JUnit for Java, RUnit
for R etc.). But sometimes they are not. In this exercise we will focus on C++
and will use the Google gtest C++ Testing Framework2 .
https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings
Text before . . .
f o r ( i n t i = 0 ; i<i t e r a t i o n s ; i ++)
{
do something
}
Text after it . . .
#include<stdio.h>
#include<iostream>
// A comment
int main(void)
{
printf("Hello World\n");
return 0;
}
#include<stdio.h>
#include<iostream>
1 Regression testing is a type of software testing that seeks to uncover new software bugs, or
regressions, in existing functional and non-functional areas of a system after changes such as enhancements, patches or configuration changes, have been made to them [Wikipedia].
2 Other examples are CppUnit or the Boost testing framework.
// A comment
int main(void)
{
printf("Hello World\n");
return 0;
}
1
2
3
4
5
6
7
8
#include<stdio . h>
#include<iostream>
// A comment
i n t main ( void )
{
printf ( Hello World\n ) ;
return 0;
}
#include<stdio . h>
#include<iostream>
// A comment
i n t main ( void )
{
printf ( Hello World\n ) ;
return 0;
}
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
//
//
//
//
Copyright 2 0 0 5 , Google I n c .
All r i g h t s reserved .
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# i n c l u d e <l i m i t s . h>
# i n c l u d e sample1 . h
# i n c l u d e g t e s t / g t e s t . h
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
71
72
73
74
75
76
77
// T e s t s F a c t o r i a l ( ) .
78
79
80
81
82
83
84
85
// T e s t s f a c t o r i a l o f n e g a t i v e numbers .
TEST ( FactorialTest , Negative ) {
// This t e s t i s named Negative , and belongs t o t h e F a c t o r i a l T e s t
// t e s t c a s e .
EXPECT_EQ ( 1 , Factorial( 5) ) ;
EXPECT_EQ ( 1 , Factorial( 1) ) ;
EXPECT_GT ( Factorial( 10) , 0 ) ;
86
//
//
//
//
//
//
//
//
//
//
//
//
//
//
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<T e c h n i c a l D e t a i l s >
EXPECT EQ ( expected , a c t u a l ) i s t h e same as
EXPECT TRUE ( ( expected ) == ( a c t u a l ) )
e x c e p t t h a t i t w i l l p r i n t both t h e expected value and t h e a c t u a l
value when t h e a s s e r t i o n f a i l s . This i s very h e l p f u l f o r
debugging . T h e r e f o r e i n t h i s c a s e EXPECT EQ i s p r e f e r r e d .
On t h e o t h e r hand , EXPECT TRUE a c c e p t s any Boolean e x p r e s s i o n ,
and i s thus more g e n e r a l .
</T e c h n i c a l D e t a i l s >
102
103
104
105
106
// T e s t s f a c t o r i a l o f 0 .
TEST ( FactorialTest , Zero ) {
EXPECT_EQ ( 1 , Factorial ( 0 ) ) ;
}
107
108
109
110
111
112
113
114
// T e s t s f a c t o r i a l o f p o s i t i v e numbers .
TEST ( FactorialTest , Positive ) {
EXPECT_EQ ( 1 , Factorial ( 1 ) ) ;
EXPECT_EQ ( 2 , Factorial ( 2 ) ) ;
EXPECT_EQ ( 6 , Factorial ( 3 ) ) ;
EXPECT_EQ ( 4 0 3 2 0 , Factorial ( 8 ) ) ;
}
115
116
117
// T e s t s IsPrime ( )
118
119
120
121
// T e s t s n e g a t i v e in pu t .
TEST ( IsPrimeTest , Negative ) {
// This t e s t belongs t o t h e I s P r i m e T e s t t e s t c a s e .
122
EXPECT_FALSE ( IsPrime( 1) ) ;
EXPECT_FALSE ( IsPrime( 2) ) ;
EXPECT_FALSE ( IsPrime ( INT_MIN ) ) ;
123
124
125
126
127
128
129
130
131
132
133
134
// T e s t s some t r i v i a l c a s e s .
TEST ( IsPrimeTest , Trivial ) {
EXPECT_FALSE ( IsPrime ( 0 ) ) ;
EXPECT_FALSE ( IsPrime ( 1 ) ) ;
EXPECT_TRUE ( IsPrime ( 2 ) ) ;
EXPECT_TRUE ( IsPrime ( 3 ) ) ;
}
135
136
137
138
139
140
141
142
// T e s t s p o s i t i v e in pu t .
TEST ( IsPrimeTest , Positive ) {
EXPECT_FALSE ( IsPrime ( 4 ) ) ;
EXPECT_TRUE ( IsPrime ( 5 ) ) ;
EXPECT_FALSE ( IsPrime ( 6 ) ) ;
EXPECT_TRUE ( IsPrime ( 2 3 ) ) ;
}
143
144
145
146
147
148
149
150
151
152
153
154
//
//
//
//
//
//
//
//
//
//
//
The Task
As was said at the beginning, the purpose of this exercise is the construction of
a unit test for a C++ class.
Steps to follow:
1. Read
2. Learn
3. Create a class
4. Prepare a single PDF with the source -class and test- and any explanation
that you consider relevant to understand them.
5. Upload to Moodle.
You can download gtest using the following link:
https://github.com/google/googletest/archive/master.zip
References
Some links
https://en.wikipedia.org/wiki/Portal:Software_testing
http://feelings-erased.blogspot.com.es/2012/07/eclipse-juno-has-landed-with-unit.
https://github.com/google/googletest/
https://en.wikipedia.org/wiki/Google_Test
http://www.ibm.com/developerworks/aix/library/au-googletestingframework.html
http://www.yolinux.com/TUTORIALS/Cpp-GoogleTest.html
http://stackoverflow.com/questions/3951808/using-googletest-in-eclipse-how
https://github.com/xgsa/cdt-tests-runner/wiki/Tutorial
https://sourceforge.net/projects/cppunit
http://www.boost.org