2.5 the Unit Test Framework (UTF)
Total Page:16
File Type:pdf, Size:1020Kb
Masaryk University Faculty of Informatics C++ Unit Testing Frameworks Diploma Thesis Brno, January 2010 Miroslav Sedlák i Statement I declare that this thesis is my original work of authorship which I developed individually. I quote properly full reference to all sources I used while developing. ...…………..……… Miroslav Sedlák Acknowledgements I am grateful to RNDr. Jan Bouda, Ph.D. for his inspiration and productive discussion. I would like to thank my family and girlfriend for the encouragement they provided. Last, but not least, I would like to thank my friends Ing.Michal Bella who was very supportive in verifying the architecture of the extension of Unit Test Framework and Mgr. Irena Zigmanová who helped me with proofreading of this thesis. ii Abstract The aim of this work is to gather, clearly present and test the use of existing UTFs (CppUnit, CppUnitLite, CppUTest, CxxTest and other) and supporting tools for software project development in the programming language C++. We will compare the advantages and disadvantages of UTFs and theirs tools. Another challenge is to design effective solutions for testing by using one of the UTFs in dependence on the result of analysis. Keywords C++, Unit Test (UT), Test Framework (TF), Unit Test Framework (UTF), Test Driven Development (TDD), Refactoring, xUnit, Standard Template Library (STL), CppUnit, CppUnitLite, CppUTest Run-Time Type Information (RTTI), Graphical User Interface (GNU). iii Contents 1 Introduction ..................................................................................................................................... 2 2 The Software Testing ...................................................................................................................... 5 2.1 Types of Testing .................................................................................................................... 6 2.2 The Test Environment (TE) ................................................................................................... 8 2.3 The Unit Test (UT) ................................................................................................................ 9 2.4 The Test Framework (TF) .................................................................................................... 10 2.5 The Unit Test Framework (UTF) ......................................................................................... 10 2.6 The Test Driven Development (TDD) ................................................................................. 13 3 The Comparison of the Unit Test Frameworks ............................................................................. 16 3.1 The xUnit ............................................................................................................................. 17 3.2 The CppUnit ......................................................................................................................... 19 3.3 The CppUnitLite .................................................................................................................. 26 3.4 The CppUTest ...................................................................................................................... 28 3.5 The CxxTest ......................................................................................................................... 30 3.6 The Conclusion of Analysis ................................................................................................. 33 4 Programs for UTF ......................................................................................................................... 34 4.1 CppUnit Base Programs ....................................................................................................... 34 5 The Unit Test Envinronment (UTE) .............................................................................................. 38 5.1 The Framework Design ........................................................................................................ 39 5.2 Implementation of Extension CppUnit ................................................................................ 44 5.3 Implementation of UTE ....................................................................................................... 47 6 Conclusion ..................................................................................................................................... 55 7 Bibliography .................................................................................................................................. 57 8 Appendix ....................................................................................................................................... 58 8.1 Abbreviations ....................................................................................................................... 58 8.2 Glossary ............................................................................................................................... 58 8.3 Source code .......................................................................................................................... 60 1 1 Introduction Today, large amount of software is developed for various purposes. Quality of software source code is measured by various factors, where the most important one is how the product fits customer’s requirements. Ensuring that it is so and that the customer is able to use the final product efficiently can be achieved through continuous testing, debugging and other techniques (see http://en.wikipedia.org/wiki/Software_quality, where different views of software quality are presented), that are out of scope of my thesis. Important phase of detection and localisation of a problem is testing, which increases the quality of developed software. The main purpose of testing is to reduce the risk of an error. There are many possibilities how to use testing during the development phase of an application. The most common is the possibility to create tests to verify functionality when software is developed. If tests have not revealed an error, we may assume that the application has no errors. However, the occurrence of errors should rather be expected, since it is known that no large-scale application is perfect. The process of testing can not verify everything, so we try to verify those parts of the system whose failure would affect the execution of the application. According to Kernigham, the most important rule of testing is to do testing as described in the book: The Practice of Programming [1]. The test can be created on different levels. This thesis is aimed at the first level of testing (Unit Testing), which is performed by developer of an application. The cornerstone for the first level of testing is development of tests and their automation for smallest units. Issues that affect source the code quality include ease of maintenance, testing, debugging, fixing, modification and portability. This involves usage of various software applications (also known as tools) exploited to assure these requirements. If we want to create an environment containing such a program, we must first choose a program, which might be difficult to combine with other programs. Creating this environment is very challenging because we must determine which of the programs is more difficult to relate with other programs. It is testing framework, because this program is developed in C ++ programming language. There are several ways how to include the functionality of other programs into the testing framework: Using program independently of the testing framework – for example running a binary test in a debugger Including programs into the testing framework – this is the subject of this thesis Implementing the functionality of a given software as a module/plug-in in the testing framework 2 Today, there are many test frameworks, but there is no environment to interconnect the test framework and programs. It is similar to IDE, which interconnects programs: a source code editor program a compiler and / or an interpreter program build automation programs a debugger program One of the goals of this thesis is to implement a software application that provides comprehensive facilities for the aforementioned issues of the software code quality. This environment should be configurable and extensible by developers according to their needs. In order to design environment correctly, first we have to use concepts related to software testing. Then we determine requirements of the test framework and compare it with the existing testing frameworks. On the basis of our analysis we select a test framework that will be used for our testing of developed software. Then a review of the programs that can be used to achieve the source code quality follows. We focus on the existing programs developed for the individual test framework as well. As we review later, there is no environment that allows additional programs to cooperate with the test framework, what forces us to define the requirements and implement a new environment. We also define functional requirements for the extension of the testing framework. Finally, we describe the implementation of the test framework extension and the environment. This thesis focuses on software testing methodology in the C++ programming language and comparison of existing testing systems (also known as frameworks) from the first-level testing point of view. Today, there are many of software testing methodologies and the choice depends on many factors.