ooc  1.3c
Object Oriented tollkit fo ANSI C
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
testcase.h File Reference

TestCase class - base class for unit testing. More...

#include <stdio.h>
#include "exception.h"
Include dependency graph for testcase.h:

Data Structures

struct  TestCaseMethod
 Test method order table. More...
 
struct  TestCaseVtable
 TestCase virtual functions. More...
 

Macros

Test method definitions.

Components defining test methods and execution of them.

#define TEST(name)
 A test method order table entry. More...
 
TestCase assertions.

These macros can be used in the test methods and in before(), after(), before_class(), after_class() methods.

If the assertion fails, the unit test run will report a fail message indicating the source file name and the line number in the source file. The test run will continue with the next statement.

#define fail()
 Unconditional fail. More...
 
#define assertTrue(expr)
 Fail if not TRUE. More...
 
#define assertFalse(expr)
 Fail if not FALSE. More...
 
#define assertNull(expr)
 Fail if not NULL. More...
 
#define assertNotNull(expr)
 Fail if NULL. More...
 
#define assertZero(expr)
 Fail if not 0. More...
 
#define assertNotZero(expr)
 Fail if 0L. More...
 
#define failMsg(msg)
 Unconditional fail with a specific message. More...
 
#define assertTrueMsg(expr, msg)
 Fail if not TRUE with a specific message. More...
 
#define assertFalseMsg(expr, msg)
 Fail if not FALSE with a specific message. More...
 
#define assertNullMsg(expr, msg)
 Fail if not NULL with a specific message. More...
 
#define assertNotNullMsg(expr, msg)
 Fail if NULL with a specific message. More...
 
#define assertZeroMsg(expr, msg)
 Fail if not 0 with a specific message. More...
 
#define assertNotZeroMsg(expr, msg)
 Fail if 0 with a specific message. More...
 

Functions

int testcase_run (TestCase)
 Run the whole TestCase. More...
 
int testcase_run_before_class (TestCase)
 Start the execution of a test series. More...
 
void testcase_run_test (TestCase, const char *, test_method_type)
 Run a single test. More...
 
int testcase_run_after_class (TestCase)
 Finishes the execution of a test series. More...
 

Detailed Description

TestCase class - base class for unit testing.

TestCase is a base class for unit testing. All of your Unit tests inherits TestCase. The process to create a new unit test in your project:

  1. Create a new test class with:
    $ ooc --new MyTest --template test
  2. Override the necessary virtual methods
  3. implement your test methods
  4. create the test methods table
  5. compile your test file as executable (you can use the makefile template from ooc/template/unit folder)
  6. run the test file

Macro Definition Documentation

#define assertFalse (   expr)

Fail if not FALSE.

Reports a failed status if not FALSE and continues.

#define assertFalseMsg (   expr,
  msg 
)

Fail if not FALSE with a specific message.

Reports a failed status with the message if not FALSE and continues.

Note
The message must be a char* and will not be freed.
#define assertNotNull (   expr)

Fail if NULL.

Reports a failed status if NULL and continues.

#define assertNotNullMsg (   expr,
  msg 
)

Fail if NULL with a specific message.

Reports a failed status with the message if NULL and continues.

Note
The message must be a char* and will not be freed.
#define assertNotZero (   expr)

Fail if 0L.

Reports a failed status if 0 and continues.

#define assertNotZeroMsg (   expr,
  msg 
)

Fail if 0 with a specific message.

Reports a failed status with the message if 0 and continues.

Note
The message must be a char* and will not be freed.
#define assertNull (   expr)

Fail if not NULL.

Reports a failed status if not NULL and continues.

#define assertNullMsg (   expr,
  msg 
)

Fail if not NULL with a specific message.

Reports a failed status with the message if not NULL and continues.

Note
The message must be a char* and will not be freed.
#define assertTrue (   expr)

Fail if not TRUE.

Reports a failed status if not TRUE and continues.

#define assertTrueMsg (   expr,
  msg 
)

Fail if not TRUE with a specific message.

Reports a failed status with the message if not TRUE and continues.

Note
The message must be a char* and will not be freed.
#define assertZero (   expr)

Fail if not 0.

Reports a failed status if not 0 and continues.

#define assertZeroMsg (   expr,
  msg 
)

Fail if not 0 with a specific message.

Reports a failed status with the message if not 0 and continues.

Note
The message must be a char* and will not be freed.
#define fail ( )

Unconditional fail.

Reports a failed status and continues.

#define failMsg (   msg)

Unconditional fail with a specific message.

Reports a failed status with the message and continues.

Note
The message must be a char* and will not be freed.
#define TEST (   name)

A test method order table entry.

This macro places a test method entry point into the test method order table. The test methods will be executed in the order they apper in the table.

Parameters
nameThe name of the test method.
See Also
TestCaseMethod

Function Documentation

int testcase_run ( TestCase  self)

Run the whole TestCase.

If the testcase was constructed with a method order table then this function runs all methods defined in the table.
It also runs the before_class() end after_class() methods.
This is the convenient way of running test methods and suites for the most cases.
This is equvivalent with the following pseudo code:

        if( testcase_run_before_class(self) == 0)
            testcase_run_all_methods(self);
        return testcase_run_after_class(self);
Parameters
selfThe TestCase to be run. The method order table must exist!
Returns
0 if the was no error, 1 if any error occurs
int testcase_run_after_class ( TestCase  self)

Finishes the execution of a test series.

Close the TestCase for subsequent tests.
Actually calls the after_class() virtual methods recursively.

Parameters
selfThe TestCase
Returns
0 if the whole test series was successful, non-zero if failed.
Note
Do not use, if test method table is used!
int testcase_run_before_class ( TestCase  self)

Start the execution of a test series.

Prepares the TestCase for subsequent tests.
Actually calls the before_class() virtua methods recursively.

Parameters
selfThe TestCase
Returns
0 if successful, non-zero if failed.
Note
Do not use, if test method table is used!
void testcase_run_test ( TestCase  self,
const char *  name,
test_method_type  method 
)

Run a single test.

Runs a single test method with the given display name. The testcase_run_before_class() function must be called before, and testcase_run_after_class() must be called at the end.
The testcase_run_test() can be called any times between testcase_run_before_class() and testcase_run_after_class().
The before() and after() methods are called automatically.

Parameters
selfThe test case
nameThe display name of the test
methodThe test function to invoke
Returns
Nothing
Note
Do not use, if test method table is used!