0% found this document useful (0 votes)
55 views8 pages

Performing Unit Testing: 1 Test-Driven Development

Exercise on Unit Testing

Uploaded by

Ricardo Sanz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views8 pages

Performing Unit Testing: 1 Test-Driven Development

Exercise on Unit Testing

Uploaded by

Ricardo Sanz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Performing Unit Testing

Software Engineering MII


Ricardo Sanz
This is an exercise in software testing. Software testing is one of the processes used to improve the quality of developed software. In some cases it is
used to prove functionality as required by users. In other cases it is used to
discover defects by developers.
Software testing, depending on the testing method employed, can be implemented at any time in the development process. Testing is a basic process
not just for final delivery but also for continuous discovery of code problems
during software construction.
The purpose of this exercise is the construction of a unit test for a C++ class.
Unit testing is the starting test process upon which the whole test framework
is built.

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].

What is Unit Testing?

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 .

A C++ Class to test

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 .

R e d i s t r i b u t i o n and use i n s o u r c e and b i n a r y forms , with or without


m o d i f i c a t i o n , a r e p e r m i t t e d provided t h a t t h e f o l l o w i n g c o n d i t i o n s are
// met :
//
//
* R e d i s t r i b u t i o n s o f s o u r c e code must r e t a i n t h e above copyright
// n o t i c e , t h i s l i s t o f c o n d i t i o n s and t h e f o l l o w i n g d i s c l a i m e r .
//
* R e d i s t r i b u t i o n s i n b i n a r y form must reproduce t h e above
// c o p y r i g h t n o t i c e , t h i s l i s t o f c o n d i t i o n s and t h e f o l l o w i n g disclaimer
// i n t h e documentation and/or o t h e r m a t e r i a l s provided with t h e
// d i s t r i b u t i o n .
//
* N e i t h e r t h e name o f Google I n c . nor t h e names o f i t s
// c o n t r i b u t o r s may be used t o endorse or promote products derived from
// t h i s s o f t w a r e without s p e c i f i c p r i o r w r i t t e n permission .
//
// THIS SOFTWARE I S PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// AS I S AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR

22

23

24
25

26

27

28

29

// A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT


// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL ,
// SPECIAL , EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
// DATA, OR PROFITS ; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY , WHETHER IN CONTRACT, STRICT LIABILITY , OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN I F ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

30
31
32
33

// A sample program demonstrating using Google C++ t e s t i n g framework .


//
// Author : wan@google . com ( Zhanyong Wan)

34
35
36
37
38
39

// This sample shows how t o w r i t e a simple u n i t t e s t f o r a f u n c t i o n ,


// using Google C++ t e s t i n g framework .
//
// Writing a u n i t t e s t using Google C++ t e s t i n g framework i s easy as 123:

40
41
42
43
44
45

// Step 1 . I n c l u d e n e c e s s a r y header f i l e s such t h a t t h e s t u f f your


// t e s t l o g i c needs i s d e c l a r e d .
//
// Don ' t f o r g e t g t e s t . h , which d e c l a r e s t h e t e s t i n g framework .

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

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

Step 2 . Use t h e TEST macro t o d e f i n e your t e s t s .


TEST has two parameters : t h e t e s t c a s e name and t h e t e s t name .
A f t e r using t h e macro , you should d e f i n e your t e s t l o g i c between a
p a i r o f b r a c e s . You can use a bunch o f macros t o i n d i c a t e t h e
s u c c e s s or f a i l u r e o f a t e s t . EXPECT TRUE and EXPECT EQ a r e
examples o f such macros . For a complete l i s t , s e e g t e s t . h .
<T e c h n i c a l D e t a i l s >
In Google Test , t e s t s a r e grouped i n t o t e s t c a s e s . This i s how we
keep t e s t code organized . You should put l o g i c a l l y r e l a t e d t e s t s
i n t o t h e same t e s t c a s e .
The t e s t c a s e name and t h e t e s t name should both be v a l i d C++
i d e n t i f i e r s . And you should not use underscore ( ) i n t h e names .
Google T e s t g u a r a n t e e s t h a t each t e s t you d e f i n e i s run e x a c t l y
once , but i t makes no guarantee on t h e order t h e t e s t s a r e

71
72
73
74

// executed . T h e r e fo r e , you should w r i t e your t e s t s i n such a way


// t h a t t h e i r r e s u l t s don ' t depend on t h e i r order .
//
// </T e c h n i c a l D e t a i l s >

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

//
//
//
//
//
//
//
//
//
//
//

Step 3 . C a l l RUN ALL TESTS ( ) i n main ( ) .


We do t h i s by l i n k i n g i n s r c / g t e s t m a i n . c c f i l e , which c o n s i s t s o f
a main ( ) f u n c t i o n which c a l l s RUN ALL TESTS ( ) f o r us .
This runs a l l t h e t e s t s you ' ve defined , p r i n t s t h e r e s u l t , and
r e t u r n s 0 i f s u c c e s s f u l , or 1 o t h e r w i s e .
Did you n o t i c e t h a t we didn ' t r e g i s t e r t h e t e s t s ? The
RUN ALL TESTS ( ) macro m a g i c a l l y knows about a l l t h e t e s t s we
defined . Isn ' t t h i s convenient ?

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

Beck, K. Test-Driven Development by Example, Addison Wesley - Vaseem,


2003

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

GTest for CDT

1. Download gtest and install it as described in the readme.txt (using cmake


and make in linux)
2. Go to YourProject- Properties- C/C++ Build- Settings- GCC C++
Compiler- Includes- Include paths and add the include folder in gtest
3. Go to YourProject- Properties- C/C++ Build- Settings- GCC C++
Linker- Libraries, add the gtest folder as search path and add libraries gtest
and pthread
(4. If you have tests in the same project as sources exclude tests from release
build)
5. Go to Run- Run Configurations... and Create a new C/C++ Unit run
configuration
6. Set project to your project and C/C++ Application to your Application
in main tab. Set Tests Runner to Google Test Runner in C/C++ Testing tab.
(7. Error notifications may stick around in the eclipse gui, if this is the case
re-indexing the project might help)

You might also like