| ||||||||||
Mini howToI won't explain every thing here, because I've already written a User-Book which took me a lot of time to make. Here, I just present an excerpt of the first chapter.Generating one test suite with one test caseTo help you to figure yourself how to use RapidoTest, this part teaches how to write a test case and to build the test suite. To keep things easy I will suppose I'am the author of the "strlen" function -- this function just returns the length of a NULL terminated character strings. (man 3 strlen). Now, I would like to write and run a test case for testing this function. Before doing this I must first introduce what "assertions" are.AssertionsTo work with RapidoTest, one has to write assertions. Assertions are run-time checks that ensure the correctness of the program at a specific point of time. One could see assertions as a sort of contract (a kind of detailed specification) that the program must respect at run-time. When the program respects one assertion, we say that the program "passed" this assertion; otherwise the program fails to pass the assertion. In the latter case, the execution of the test case is stopped at the failed assertion and an anomaly report is automatically generated. With the above definition, writing a test case for strlen() would consist for instance, to assert the output of the function given a specific input as stated below:
The above figure says that strlen() should respectively return 4 and 0 when it handles the inputs "ABCD" and "". In RapidoTest, there exists an assertion called: void assertIntEquals(int expected, int returned) This assertion just compares the returned value to the expected value. If a mismatch occurs, the execution of the test case is stopped (by throwing an AssertionException). Declaration of a test case in RapidoTestIn RapidoTest a test case is a C-like function which doesn't have neither input nor output parameters. Furthermore, to be added to the test suite, the function MUST BE embedded in one namespace (the one of your choice, but choose a namespace's name dedicated to the tests). Here is an example of test case declaration:
WARNING: don't put other things than function declarations in this namespace; otherwise RapidoTest may be confused and may not detect accurately all your test cases.
As you can see, this is a straightforward way to write test cases. Now, let's go back to our previous strlen() test case. Having
introduced how to declare a test case and what assertions are, I present below a possible implementation of a test case for strlen: First I included "string.h" and "rapidotest.hh" so strlen() and assertIntEquals() are defined. Then in a namespace which I called "check", I implemented a C-like function called "test_strlen()". This function uses a couple of times "assertIntEquals" for each use case I presented earlier. Et voilà! We have done 99% of the hard work! Generating the test suiteOne of RapidoTest's main features is to automatically generate C++ code for building the test suite. This feature includes the detection of test cases that need to be added to the test suite. For doing this, we will make use of one utility command that comes with the tarball which is: "rapidotest-update". First, let's save the test case as "t_strlen.cpp".
In a terminal window, enter the following command: This command line tells rapidotest-update to scan input file "t_strlen.cpp" and to look for test cases present in the namespace "check". Then we said to generate a new file "testsuite.cpp" that contains all the C++ code needed to initialize and run the test suite. At the end of the process the file: "testsuite.cpp" is created in the current directory. Compiling and running the test suite
In a terminal window, enter the following command:
and run...
This end this mini howTo. | ||||||||||
|