Example: xUnit Style
What follows is an example of writing tests with FCTX using the common xUnit style of testing. In this example we will test a routine that involves creating an object, testing that object, and destroying that object.
Lets get started,
1 #include "fct.h"
Now FCTX has been "installed", we can start to write our test framework. The first thing we must do is define our testing scope:
1 FCT_BGN() {
2
3 } FCT_END();
This effectively sets up our start and end for our unit tests. All our unit tests will be defined between the FCT_BGN and FCT_END definitions.
Lets start writing a unit test. In this case we are going to use the xUnit framework, with a setup and teardown block. It is important to remember that you do not need to do this for each and every FCTX Suite.
First lets sketch out the skeleton for the test.
1 #include "fct.h"
2
3 FCT_BGN() {
4 FCT_FIXTURE_SUITE_BGN(example_suite) {
5
6 FCT_SETUP_BGN() {
7 } FCT_SETUP_END();
8
9 FCT_TEARDOWN_BGN() {
10 } FCT_TEARDOWN_END();
11
12 FCT_TEST_BGN(test_object_basic) {
13 } FCT_TEST_END();
14
15 } FCT_FIXTURE_SUITE_END();
16
17 } FCT_END();
In the above example we have the standard "setup" and "teardown" routines that will be executed before and after each test. The tests are all defined by the FCT_TEST_BGN and FCT_TEST_END macros. Now lets populate this test so it "does something".
1 #include "fct.h"
2
3 FCT_BGN() {
4 my_object_t *my_object; /* Our test suite date */
5 FCT_FIXTURE_SUITE_BGN(example_suite) {
6
7 FCT_SETUP_BGN() {
8 my_object = my_object_new();
9 } FCT_SETUP_END();
10
11 FCT_TEARDOWN_BGN() {
12 my_object__del(my_object);
13 } FCT_TEARDOWN_END();
14
15 FCT_TEST_BGN(test_object_basic) {
16 /* Otherwise no point in testing further! */
17 fct_req(my_object != NULL);
18 /* Run a check, in this case we hope that "foo" returns true. */
19 fct_chk(my_object__foo(my_object) == 1);
20 } FCT_TEST_END();
21
22 } FCT_FIXTURE_SUITE_END();
23
24 } FCT_END();
Now you can keep adding more and more tests to the example_suite, and each test will perform the common "setup" and "teardown" operation on the my_object_t. You can also define more test suites within the FCT_BGN and FCT_END.
More information about test suites can be found in the latest FCTX documentation.
