CCUnit
2.1
A C Unit Testing Library
|
Here is a short cookbook to help you get started.
[see also Japanese documents]
Tests in CCUnit can be run automatically. They are easy to set up and once you have written them, they are always there to help you keep confidence in the quality of your code.
To make a simple test, here is what you do:
When you want to check a value, call CCUNIT_ASSERT(bool) and pass a bool that is true if the test succeeds
ASSERT macro is listed in others as well in Assert Macros.
For example, see the test of the library of the simple complex number as an example. (This sample program is in the examples/complex
directory) To test whether the initialization of the complex numeber and two complex numbers are equal.
These two test functions are made one test case together, and we can run it.
Include the necessary header files.
First, Allocate the test case memory.
Then, register the test functions in the test case. The assertion should specify the name of the function and the explanation of the function to indicate it at the unsuccessful time.
Run the test case, and take out the result.
Deallocate the memory whitch you assigned, and return the result.
You find the sample code made in the above in the files testComplex.c
and runTestCase.c
in the examples/complex
directory.
To compile and run, do the following:
When the test case is run, that specific test functions will be run. This is not a useful thing to do, however, as no diagnostics will be displayed. One will normally use a Executing Test to display the results.
How do you run your tests and collect their results? The TestCase store result of own tests into the TestResult. At the same time, the test case informs the TestRunner of the executive conditionsn of the test.
Once you have a test suite, you'll want to run it. CCUnit provides TestRunner to define the suite to be run and to display its results.
To use the TestRunner, include the header files for the tests in runTestCaseRunner.c
:
First, make the TestSuite, and register the TestCase.
And call to ccunit_runTestRunner(CCUnitTestRunner*, CCUnitTestSuite *):
Deallocate the memories.
The sample code made in the above is in the directory examples/complex
, the files testComplex.c, complexTestSuite.c
and runTestRunner.c
.
To compile and run, do the following:
The TestRunner will run the tests. If all the tests pass, you'll get an informative message. If any fail, you'll get the following information:
For example, try to mistake it in the test case of testComplex.c
on purpose.
In this example, two test cases are run. Then, you will see that the second is unsuccessful.
That was a very simple test. Ordinarily, you'll have many little test cases that you'll want to run on the same set of objects. To do this, use global variables.
What if you have two or more tests that operate on the same similar set of objects?
Tests need to run against the background of a known set of data. you are writing tests you will often find that you spend more time writing the code to set up the data than you do in actually testing values.
In the sample code of the previous section, some complex numbers are allocated, and it is deleted.
Often, you will be able to use the same data for several different tests. Each case will assign slightly different values to the data and will check for different results. Then, if the test is finished, clean-up the data. The function setUp
and tearDown
are used at such time.
When you have a common global variables, here is what you do:
Define the global variable.
Write the function setUp
to allocate the complex.
Write the function tearDown
to delete the memory of that complex.
Write the test function.
Register the function setUp
and tearDown
. At this time, the name of the function must have begun with "setUp"
or "tearDown"
.
Run the test case, and return the result.
The sample code made in the above is in the directory examples/complex
, the files are testComplexSetup.c
and runTestCaseSetup.c
.
To compile and run, do the following:
In the previous section, the initialization was done in every test every time. But we do the initialization only once, and we may run the multiple test. Then, only when all the tests are finished, it may seem to carry out the code for the settlement.
You register the setUpBeforeClass
and tearDownAfterClass
function in the test case for that. Then, the test functions are run in the following order:
How do you set up your tests so that you can run them all at once? Once you have several tests, organize them into a suite.
CCUnit provides a TestSuite module that runs any number of TestFuncs together. You saw, above, how to run test suite.
To create a suite of two or more tests, you do the following:
Create two test cases, and register test functions to each cases.
Create a test suite, then register that two test cases to it.
Create test runner and run the tests.
The sample code made in the above is in the directory examples/complex
, the files are runTestSuite.c
, testComplexSetup.c
and testComplexArith.c
.
To compile and run, do the following:
TestSuites don't only have to contain TestCases . They can also contain TestSuites. For example, you can create a TestSuite in your code and I can create one in mine, and we can run them together by creating a TestSuite that contains both:
As you might have noticed, the work to register the test function in the test case and to register the test case in the test suite is easy to mistake concerning the repetition.
Actually, though the test function of test_complex_to_string()
was defined in testComplexSetup.c
, it was ignored by the above explanation, and it didn't register for the test case.
A Creating TestSuite set of functions and command have been created to automatically implements the creating suite function.
You make the file which you write the test case in for that as follows.
First, declare the test suite as this in the comment of the javaDoc style. As for the comment of the javaDoc style, the starts of the comment block of the C style are two asterisks "<tt>**</tt>".
Next, define the test cases. The test case is gathering of the global variables and the functions that surrounded in the declaration of the end of the test case with the declaration of the name of the test case written in the comment of the javaDoc style.
One file can define the multiple test case.
As for the test function, the name must have begun with test. You can write the explanation of the test function in the comment of the javaDoc style before each test function. If the comment isn't being written, the name of the test function is registered as an explanation of the function.
There are setUp
, tearDown
, setUpBeforeClass
and tearDownAfterClass
as a special test function. Their works as a functional function of the test case that Those function as a functional function of the test case which I explained before so that you may know it from that name. Define as the function to begin respectively with such a name.
To generate creating suite function code, run ccunit_makeSuite
tool.
The function to make TestSuite is ccunit_suite
. But, it can be changed to another name by the -f
option of the ccunit_makeSuite
command, too.
The next is taken to use the code which you generated automatically.
Declare the prototype of the function which you generated. Try not to forget char* name
of the argument.
Call the function which you generated, and take out the test suite.
Run it by the test runner.
The sample code here will be found with runTestSuite.c
, testComplex.c
and testComplexArith.c
of the examples/complex
dhirectory.
To compile and run, do the following:
To generate a code, the test case source codes must be formatted by following pseudo-BNF:
The sample program made in the above is in the examples/complex
directory.
complex - some complex number library test cases
|
hosts this site. | Send comments to: CCUnit Developer |
Generated on Sun Jul 14 2013 09:12:23 for CCUnit by
![]() |