Parameterized Unit Testing with Pex
This hands-on tutorial will teach the principles of Parameterized Unit Testing [5,4] with Pex [2], an automatic test input generator for .NET which performs a systematic program analysis, similar to path bounded model-checking.
- PDF / 294,800 Bytes
- 11 Pages / 430 x 660 pts Page_size
- 79 Downloads / 230 Views
Abstract. This hands-on tutorial will teach the principles of Parameterized Unit Testing [5,4] with Pex [2], an automatic test input generator for .NET which performs a systematic program analysis, similar to path bounded model-checking. A parameterized unit test is simply a method that takes parameters, calls the code under test, and states assertions.
1
Unit Tests
A unit test is a self-contained program that checks an aspect of the implementation under test. A unit is the smallest testable part of the program. Here is a typical unit test of the array list that describes the normal behavior of the Add method with respect to the indexing operator []. public void SomeAddTest () { // exemplary data object element = new object (); ArrayList list = new ArrayList (1); // method sequence list . Add ( element ); // assertions Assert . IsTrue ( list [0] == element ); }
We partition each unit test into three parts. – Unit tests take exemplary data as test inputs that are passed to the called methods. – Unit tests consist of a method sequence which represents a typical scenario for the usage of an API. – Assertions encode the test oracle of a unit test. The test fails if any assertion fails or an exception is thrown but not caught. The above unit test specifies the behavior of the array list by example. Strictly speaking, this unit test only says that by adding a new object to an empty array list, this object becomes the first element of the list. What about other array lists and other objects? B. Beckert and R. H¨ ahnle (Eds.): TAP 2008, LNCS 4966, pp. 171–181, 2008. c Springer-Verlag Berlin Heidelberg 2008
172
2
J. de Halleux and N. Tillmann
Parameterized Unit Tests (PUTs)
A straightforward extension is to allow parameters for unit tests. Here is a parameterized version of the array list unit test. Under the condition that a given array list is not null, this parameterized unit test asserts that after adding an element to the list, the element is indeed present at the end of the list: public void SomeAddSpec ( // data ArrayList list , object element ) { // assumptions PexAssume . IsTrue ( list != null ); // method sequence int len = list . Count ; list . Add ( element ); // assertions PexAssert . IsTrue ( list [ len ] == element ); }
This test is more general than the original test. It states that for all nonnull array lists, and all objects, after adding the object to the array list, it is contained at the end. Parameterized unit tests like this one can be called with various input values, perhaps drawn from an attached database. Unit testing frameworks that support parameterized unit tests sometimes refer to them as data-driven tests (e.g. in [2]). Unlike many other forms of specification documents, PUTs are written on the level of the actual software APIs, in the programming language of the software project. This allows PUTs to evolve naturally with the code against which they are written. 2.1
Separation of Concerns
Splitting the specification and test cases by parameterized unit testing is a separation of co
Data Loading...