www.wildbearsoftware.com

Example: Quick Test

Sometimes you want to get up and running some tests quickly. You don't care about xUnit's setup/teardown, you don't care about fixtures and figuring out test suites. You want to write a test, and you want to write it now! Well along FCTX to help make this happen.

Lets go over a simple example that tests that your newly crafted string equal function (str_eq}) is working correctly. First lets write up the basics,

   1 #include "fct.h"      /* FCTX is installed! */
   2 #include "your_api.h" /* Your api that you are testing! */
   3  
   4 FCT_BGN() {
   5 
   6 } FCT_END();

Now lets write our first test:

   1 #include "fct.h"      /* FCTX is installed! */
   2 #include "your_api.h" /* Your api that you are testing! */
   3  
   4 FCT_BGN() {
   5 
   6    FCT_QTEST_BGN(your_api__smoke_test) {
   7       int ok = your_api__do_something(1, 2, 3);
   8       fct_chk( ok );
   9    } FCT_QTEST_END();
  10 
  11 } FCT_END();

There you have written your first test, assuming your api is in a file called your_api.c, we can compile and run with something like,

  • $ gcc test.c your_api.c
    $ ./a.out 

and you would see something like,

your_api__smoke_test .............................................. PASS

------------------------------------------------------------------------
PASSED (1/1 tests)

we could add another quick test,

   1 #include "fct.h"      /* FCTX is installed! */
   2 #include "your_api.h" /* Your api that you are testing! */
   3  
   4 FCT_BGN() {
   5 
   6    FCT_QTEST_BGN(your_api__smoke_test) {
   7       int ok = your_api__do_something(1, 2, 3);
   8       fct_chk( ok );
   9    } FCT_QTEST_END();
  10 
  11    FCT_QTEST_BGN(your_api__bad_data) {
  12       int ok = your_api__do_something(0, 0, 0);
  13       fct_chk( !ok );
  14    } FCT_QTEST_END();
  15 
  16 } FCT_END();

recompile and run, and we will see,

your_api__smoke_test .............................................. PASS
your_api__bad_data ................................................ PASS

------------------------------------------------------------------------
PASSED (2/2 tests)

and so on.

The goal of FCTX is to make it as easy as possible for you to write tests as quickly as possible. If you don't need test suites and setup/teardown idioms, and you don't want to have to remember to register your functions, then FCTX will hopefully make it easier for you to write unit tests.