Skip to content

Instantly share code, notes, and snippets.

@pqnelson
Last active January 29, 2019 16:22
Show Gist options
  • Save pqnelson/9c66975028fe1e772d137f46ac14fcf2 to your computer and use it in GitHub Desktop.
Save pqnelson/9c66975028fe1e772d137f46ac14fcf2 to your computer and use it in GitHub Desktop.
Unit Tests in ANSI C

Basic Structure

Since C does not provide a "canonical" unit testing framework, we have to implement our own. The basic template for testing frameworks is called xUnit, which we'll follow.

For C, I guess the analogous xUnit patterns would be:

  • Test runner: the .c file with the main() function running all (or some specified subset of) the test suites
  • Test suite: a collection of test cases
  • Test case: the function which is a single unit test, i.e., has a single assert

Note, strictly speaking, a single .c file may contain multiple test suites, though it seems that JUnit considers a single .java file to be a single test suite.

The test runner's output may be human readable, or a dump to an xml file, or something else (or some combination of these).

References:

Implementation. I actually implemented an xUnit-style framework for unit tests in my toy ML compiler/interpreter, see test.c and runner.c, as well as the associated header files. The implementation mostly follows the design discussed here, with some small, insignificant modifications.

Output

For better or worse, JUnit seems to be the "modern standard" (although it is inspired from SUnit). JUnit has XML output, the schema discussed in junit-xml-output.md, which seems to work with the assumption that all code occurs in a class. It would be nice to make a C unit-testing-framework produce JUnit consistent XML output, but that might be too hard.

Basic Sketch

So, it seems (unfortunately) there must be some global data. We need to keep track of the test suites for the runner.

Test Suite

Lets begin thinking about a test suite, which is a composite pattern consisting of a list of test cases. Since this is C, we should think of using a linked list of test cases. We can actually stick a lot of metadata onto the test_case_list.

struct property {
    char *name;
    char *value;
    struct property *next;
};

struct test_suite {
    const char *name;
    struct test_case_list *cases;
    struct property *properties;
    time_t start_time;
    time_t end_time;
    int errors, failures;
    const char *hostname;
    int tests;
    int failures;
    int errors;
    double time;
};
extern struct test_suite *current_suite;
struct test_suite* new_test_suite(char *name);
void free_test_suite(struct test_suite *suite);
void test_suite_setHostname(struct test_suite *suite, char *hostname);
void add_test_case(struct test_suite *suite, struct test_case *test_case);

Test Case

Now, we need to consider the test case. It should have a function pointer to a void test(void) signature function.

typedef enum { CASE_UNTESTED = -1, CASE_SUCCESS = 0, CASE_FAIL, CASE_ERROR, CASE_SKIPPED } test_case_status;
typedef enum { FAIL_NONE = 0, FAIL_FAILURE, FAIL_ERROR } fail_type;

struct fail_or_error {
    fail_type type;
    char *message; /* message specified by assert or error */
    char *type;    /* type of assert or error that occurred */
};

struct test_case {
    void (*test_case)(void);     /* the test case */
    /* everything else is metadata */
    const char *name;
    const char *classname; /* junit compatiblity */
    struct fail_or_error *failure;
    double time;
    time_t start_time;
    time_t end_time;
    test_case_status result;
};
struct test_case* new_test_case(void (*test_case)(void), char *name, char *classname);
void free_test_case(struct test_case *test);
void test_case_failure(struct test_case *test, char *message, char *type);
void test_case_error(struct test_case *test, char *message, char *type);

// TODO: handle milliseconds for ISO 8601 format?
inline char* test_case_time(struct test_case *test) {
    if (CASE_UNTESTED == test->result) return "";
    char buff[28];
    strftime(buff, 20, "%Y-%m-%d %H:%M:%SZ", gmtime_r(&(test->start_time)));
    return buf;
}

Suites have a list of test cases

A suite is a composite design pattern, i.e., a list of test_case objects. We have some rudimentary code for that:

struct test_case_list {
    struct test_case_list *next; /* linked-list structure */
    struct test_case *test;
    char *systemOut;
    char *systemErr;
};
struct test_case_list new_test_case_node(struct test_case *test_case);
void free_test_case_list(struct test_case_list *list);

Test Runner

To let the test runner...run... we need to keep track of all the test suites. We simplify the design choice to just keep track of a composite test_suite. So we need, sadly, a global variable:

struct test_suite_list {
    struct test_suite *suite;
    struct test_suite_list *next;
};
extern struct test_suite_list *all_suites;
// boiler plate functions
struct test_suite_list* new_test_suite_list(struct test_suite *suite);
void free_suite_list(struct test_suite_list *list);

#define test_suite(name) /* current_suite = new_test_suite(#name); test_suite_list_addSuite(all_suites, current_suite); */
#define add_test(fn) fn; /* prototype the function */ \
    add_test_case(current_suite, new_test_case((fn), #fn, __FILE__)); \
    fn ()

/* example:
test_suite(my_awesome_test_suite)
add_test(math_makes_sense1) {
    assertEqualInt(1, 1);
}
// more tests
*/

We need to have a variety of assert macros, which takes 3 arguments (LHS, RHS, message) with the message have some default value if missing. Variadic macros make this easier, doing things like

#define assertEqualIntHelper(lhs, rhs, msg, ...) ((lhs) == (rhs) ? test_status = SUCCESS : (test_status = FAIL, fail_message = (msg)))
#define assertEqualInt(lhs, rhs, ...) assertEqualIntHelper(lhs, rhs, __VA_ARGS__, #lhs " != " #rhs)

I suppose we could generalize it to permit

#define assertEqual(lhs, rhs, comparator, msg, ...) (comparator(lhs, rhs) ? /* etc. */ )
#define defaultEqualityComparator(lhs, rhs) ((lhs) == (rhs))

The idea of variadic macros came from here.

Running a test case

We need some way to have a test case run. Each test_case is a command pattern, but we need to record the results of the assertEqual() macros in the suite.

A test suite, after running, should report a string to the user that looks like Tests run: 86, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.727 sec - (filename).

void test_case_run(struct test_case *test_case, struct test_suite *suite) {
    test_result = CASE_UNTESTED;
    // set test_case->start = ...
    test_case->run();
    // set test_case->end = ...
    suite->tests++;
    if (test_result == CASE_FAILURE) {
        suite->failures++;
    } else if (test_result == CASE_SKIPPED) {
        suite->skipped++;
    } else if (test_result == CASE_ERROR) {
        suite->errors++;
    } else if (test_result == CASE_SUCCESS) {
        // huzzah!
    } else {
        // panic!!!
    }
    // print to file?
    // print to screen? 
}

void test_suite_run(struct test_suite *suite) {
    time_t start = time();
    struct test_case_list *caseIter = suite->cases;
    struct test_case *test;
    while(NULL != caseIter) {
        test = caseIter->test_case;
        test_case_run(test, suite);
        caseIter = caseIter->next;
    }
    time_t end = time();
    // print to file?
    // print to screen? 
}

void run_all_suites() {
    if (NULL == all_suites) return;
    struct suite_list *iter = all_suites;
    struct test_suite *suite;
    while (NULL != iter) {
        suite = iter->suite;
        test_suite_run(suite);
        iter = iter->next;
    }
}

void cleanup_all_suites() {
    if (NULL == all_suites) return;
    struct suite_list *iter = all_suites;
    struct test_suite *suite = iter->suite;
    struct test_suite *next;
    while (NULL != iter) {
        iter = iter->next;
        next = iter->suite;
        free_test_suite(suite);
        suite = next;
    }
}

int main() {
    run_all_suites();
    cleanup_all_suites();
    return 0;
}

The assertEqual() macro needs to set the test_case result status, and possibly create some kind of failure object. If we could update a TEST_NAME macro whenever a new test were defined, these macros would work:

#define testCaseForFailure() find_case(find_suite(__FILE__), TEST_NAME)
#define assertEqual(...) (... ? /* success */ : addFailure(testCaseForFailure(), msg))

Addendum

The correct structure, I think, should be to have the test_case::run() function implicitly pass along a pointer to the test_case object and use this implicitly in the assert() macros. The modified code looks like:

/* changes to test_case */
struct test_case {
    void (*run)(struct test_case *this);
    /* ... */
}
void test_case_run(struct test_case *test_case);

/* changes to test_suite + macros */
#define test_suite(name) /* { \
  static struct test_suite *current_suite = new_test_suite(#name); \
  test_suite_list_addSuite(all_suites, current_suite); } */
#define add_test(fn) void fn (struct test_case *this); /* prototype the function */ \
    add_test_case(current_suite, new_test_case((fn), #fn, __FILE__)); \
    void fn (struct test_case *this)

/* changes to assert macros, requires being inside a test_case() */
#define unlessError(result) (0 != errno ? (this->result = CASE_ERROR, errno=0) : this->result = result)
#define assertEqual(lhs, rhs, comparator, msg, ...) (comparator(lhs, rhs) ? unlessError(CASE_SUCCESS) : unlessError(CASE_FAILURE))

Printing Results

char* timeISO(time_t time);

static void printIndent(FILE *stream, size_t layer) {
    while((layer--) > 0) fprintf(stream, "  ");
}

static void failure_or_error_printXML(struct fail_or_error *fail, FILE *stream) {
    fprintf(stream, "<%s", (fail->type == FAIL_FAILURE ? "failure" : "error"));
    if (NULL != fail->message) 
        fprintf(stream, " message=\"%s\"", fail->message);
    if (NULL != faill->type)
        fprintf(stream, " type=\"%s\"", fail->type);
    fprintf(stream, "></%s>", (fail->type == FAIL_FAILURE ? "failure" : "error"));
}

void test_case_printXML(struct test_case *test_case, FILE *stream) {
    indent(2);
    fprintf(stream, "<testcase name=\"%s\" classname=\"%s\" time=\"%f\"",
            test_case->name, test_case->classname, (test_case->end) - (test_case->start));
    if (CASE_SUCCESS = test_case->result) {
        fprintf(stream, "/>\n");
    } else {
        fprintf(stream, ">\n")
        indent(3)
        if (CASE_FAILURE == test_case->result) {
            failure_or_error_printXML(test_case->failure, stream);
            fprintf(stream, "\n");
        } else (CASE_SKIPPED == test_case->result) {
            fprintf(stream, "<skipped />\n");
        } else if (CASE_ERROR == test_case->result) {
            failure_or_error_printXML(test_case->failure, stream);
            fprintf(stream, "\n");
        }
        indent(2);
        fprintf(stream, "</testcase>");
    }
}

void test_suite_printXML(struct test_suite *suite, FILE *stream) {
    indent(1);
    fprintf(stream, "<testsuite name=\"%s\" errors=\"%d\" skipped=\"%d\" tests=\"%d\" failures=\"%d\" time=\"%f\" timestamp=\"%s\">\n",
    suite->name, suite->errors, suite->skipped, suite->failures, suite->tests, (suite->end) - (suite->start), timeISO(suite->start));
    /* print the test cases */
    struct test_case_list *iter = suite->cases;
    while (NULL != iter) {
        struct test_case *test_case = iter->test;
        test_case_printXML(test_case, stream);
        iter = iter->next;
    }
    /* close the tag */
    indent(1);
    fprintf("</testsuite>\n");
}

void test_suite_printToScreen(struct test_suite *suite) {
    printf("Tests run: %d, Failures: %d, Errors: %d, Skipped: %d, Time elapsed: %f sec - %s",
    suite->tests, suite->failures, suite->errors, suite->skipped, (suite->end) - (suite->start), suite->name);
}

Enterprise Tester has a good page summarizing the spec for Junit XML output. Some example output:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
   <testsuite name="JUnitXmlReporter" errors="0" tests="0" failures="0" time="0" timestamp="2013-05-24T10:23:58" />
   <testsuite name="JUnitXmlReporter.constructor" errors="0" skipped="1" tests="3" failures="1" time="0.006" timestamp="2013-05-24T10:23:58">
      <properties>
         <property name="java.vendor" value="Sun Microsystems Inc." />
         <property name="compiler.debug" value="on" />
         <property name="project.jdk.classpath" value="jdk.classpath.1.6" />
      </properties>
      <testcase classname="JUnitXmlReporter.constructor" name="should default path to an empty string" time="0.006">
         <failure message="test failure">Assertion failed</failure>
      </testcase>
      <testcase classname="JUnitXmlReporter.constructor" name="should default consolidate to true" time="0">
         <skipped />
      </testcase>
      <testcase classname="JUnitXmlReporter.constructor" name="should default useDotNotation to true" time="0" />
   </testsuite>
</testsuites>

The schema annotated

<testsuites>        => the aggregated result of all junit testfiles
  <testsuite>       => the output from a single TestSuite
    <properties>    => the defined properties at test execution
      <property>    => name/value pair for a single property
      ...
    </properties>
    <error></error> => optional information, in place of a test case - normally if the tests in the suite could not be found etc.
    <testcase>      => the results from executing a test method
      <system-out>  => data written to System.out during the test run
      <system-err>  => data written to System.err during the test run
      <skipped/>    => test was skipped
      <failure>     => test failed
      <error>       => test encountered an error
    </testcase>
    ...
  </testsuite>
  ...
</testsuites>

XSD Schema Definition

The XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
   <xs:annotation>
      <xs:documentation xml:lang="en">JUnit test result schema for the Apache Ant JUnit and JUnitReport tasks
Copyright © 2011, Windy Road Technology Pty. Limited
The Apache Ant JUnit XML Schema is distributed under the terms of the GNU Lesser General Public License (LGPL) http://www.gnu.org/licenses/lgpl.html
Permission to waive conditions of this license may be requested from Windy Road Support (http://windyroad.org/support).</xs:documentation>
   </xs:annotation>
   <xs:element name="testsuite" type="testsuite" />
   <xs:simpleType name="ISO8601_DATETIME_PATTERN">
      <xs:restriction base="xs:dateTime">
         <xs:pattern value="[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}" />
      </xs:restriction>
   </xs:simpleType>
   <xs:element name="testsuites">
      <xs:annotation>
         <xs:documentation xml:lang="en">Contains an aggregation of testsuite results</xs:documentation>
      </xs:annotation>
      <xs:complexType>
         <xs:sequence>
            <xs:element name="testsuite" minOccurs="0" maxOccurs="unbounded">
               <xs:complexType>
                  <xs:complexContent>
                     <xs:extension base="testsuite">
                        <xs:attribute name="package" type="xs:token" use="required">
                           <xs:annotation>
                              <xs:documentation xml:lang="en">Derived from testsuite/@name in the non-aggregated documents</xs:documentation>
                           </xs:annotation>
                        </xs:attribute>
                        <xs:attribute name="id" type="xs:int" use="required">
                           <xs:annotation>
                              <xs:documentation xml:lang="en">Starts at '0' for the first testsuite and is incremented by 1 for each following testsuite</xs:documentation>
                           </xs:annotation>
                        </xs:attribute>
                     </xs:extension>
                  </xs:complexContent>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:complexType name="testsuite">
      <xs:annotation>
         <xs:documentation xml:lang="en">Contains the results of exexuting a testsuite</xs:documentation>
      </xs:annotation>
      <xs:sequence>
         <xs:element name="properties">
            <xs:annotation>
               <xs:documentation xml:lang="en">Properties (e.g., environment settings) set during test execution</xs:documentation>
            </xs:annotation>
            <xs:complexType>
               <xs:sequence>
                  <xs:element name="property" minOccurs="0" maxOccurs="unbounded">
                     <xs:complexType>
                        <xs:attribute name="name" use="required">
                           <xs:simpleType>
                              <xs:restriction base="xs:token">
                                 <xs:minLength value="1" />
                              </xs:restriction>
                           </xs:simpleType>
                        </xs:attribute>
                        <xs:attribute name="value" type="xs:string" use="required" />
                     </xs:complexType>
                  </xs:element>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
         <xs:element name="testcase" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
               <xs:choice minOccurs="0">
                  <xs:element name="error">
                     <xs:annotation>
                        <xs:documentation xml:lang="en">Indicates that the test errored.  An errored test is one that had an unanticipated problem. e.g., an unchecked throwable; or a problem with the implementation of the test. Contains as a text node relevant data for the error, e.g., a stack trace</xs:documentation>
                     </xs:annotation>
                     <xs:complexType>
                        <xs:simpleContent>
                           <xs:extension base="pre-string">
                              <xs:attribute name="message" type="xs:string">
                                 <xs:annotation>
                                    <xs:documentation xml:lang="en">The error message. e.g., if a java exception is thrown, the return value of getMessage()</xs:documentation>
                                 </xs:annotation>
                              </xs:attribute>
                              <xs:attribute name="type" type="xs:string" use="required">
                                 <xs:annotation>
                                    <xs:documentation xml:lang="en">The type of error that occured. e.g., if a java execption is thrown the full class name of the exception.</xs:documentation>
                                 </xs:annotation>
                              </xs:attribute>
                           </xs:extension>
                        </xs:simpleContent>
                     </xs:complexType>
                  </xs:element>
                  <xs:element name="failure">
                     <xs:annotation>
                        <xs:documentation xml:lang="en">Indicates that the test failed. A failure is a test which the code has explicitly failed by using the mechanisms for that purpose. e.g., via an assertEquals. Contains as a text node relevant data for the failure, e.g., a stack trace</xs:documentation>
                     </xs:annotation>
                     <xs:complexType>
                        <xs:simpleContent>
                           <xs:extension base="pre-string">
                              <xs:attribute name="message" type="xs:string">
                                 <xs:annotation>
                                    <xs:documentation xml:lang="en">The message specified in the assert</xs:documentation>
                                 </xs:annotation>
                              </xs:attribute>
                              <xs:attribute name="type" type="xs:string" use="required">
                                 <xs:annotation>
                                    <xs:documentation xml:lang="en">The type of the assert.</xs:documentation>
                                 </xs:annotation>
                              </xs:attribute>
                           </xs:extension>
                        </xs:simpleContent>
                     </xs:complexType>
                  </xs:element>
               </xs:choice>
               <xs:attribute name="name" type="xs:token" use="required">
                  <xs:annotation>
                     <xs:documentation xml:lang="en">Name of the test method</xs:documentation>
                  </xs:annotation>
               </xs:attribute>
               <xs:attribute name="classname" type="xs:token" use="required">
                  <xs:annotation>
                     <xs:documentation xml:lang="en">Full class name for the class the test method is in.</xs:documentation>
                  </xs:annotation>
               </xs:attribute>
               <xs:attribute name="time" type="xs:decimal" use="required">
                  <xs:annotation>
                     <xs:documentation xml:lang="en">Time taken (in seconds) to execute the test</xs:documentation>
                  </xs:annotation>
               </xs:attribute>
            </xs:complexType>
         </xs:element>
         <xs:element name="system-out">
            <xs:annotation>
               <xs:documentation xml:lang="en">Data that was written to standard out while the test was executed</xs:documentation>
            </xs:annotation>
            <xs:simpleType>
               <xs:restriction base="pre-string">
                  <xs:whiteSpace value="preserve" />
               </xs:restriction>
            </xs:simpleType>
         </xs:element>
         <xs:element name="system-err">
            <xs:annotation>
               <xs:documentation xml:lang="en">Data that was written to standard error while the test was executed</xs:documentation>
            </xs:annotation>
            <xs:simpleType>
               <xs:restriction base="pre-string">
                  <xs:whiteSpace value="preserve" />
               </xs:restriction>
            </xs:simpleType>
         </xs:element>
      </xs:sequence>
      <xs:attribute name="name" use="required">
         <xs:annotation>
            <xs:documentation xml:lang="en">Full class name of the test for non-aggregated testsuite documents. Class name without the package for aggregated testsuites documents</xs:documentation>
         </xs:annotation>
         <xs:simpleType>
            <xs:restriction base="xs:token">
               <xs:minLength value="1" />
            </xs:restriction>
         </xs:simpleType>
      </xs:attribute>
      <xs:attribute name="timestamp" type="ISO8601_DATETIME_PATTERN" use="required">
         <xs:annotation>
            <xs:documentation xml:lang="en">when the test was executed. Timezone may not be specified.</xs:documentation>
         </xs:annotation>
      </xs:attribute>
      <xs:attribute name="hostname" use="required">
         <xs:annotation>
            <xs:documentation xml:lang="en">Host on which the tests were executed. 'localhost' should be used if the hostname cannot be determined.</xs:documentation>
         </xs:annotation>
         <xs:simpleType>
            <xs:restriction base="xs:token">
               <xs:minLength value="1" />
            </xs:restriction>
         </xs:simpleType>
      </xs:attribute>
      <xs:attribute name="tests" type="xs:int" use="required">
         <xs:annotation>
            <xs:documentation xml:lang="en">The total number of tests in the suite</xs:documentation>
         </xs:annotation>
      </xs:attribute>
      <xs:attribute name="failures" type="xs:int" use="required">
         <xs:annotation>
            <xs:documentation xml:lang="en">The total number of tests in the suite that failed. A failure is a test which the code has explicitly failed by using the mechanisms for that purpose. e.g., via an assertEquals</xs:documentation>
         </xs:annotation>
      </xs:attribute>
      <xs:attribute name="errors" type="xs:int" use="required">
         <xs:annotation>
            <xs:documentation xml:lang="en">The total number of tests in the suite that errorrd. An errored test is one that had an unanticipated problem. e.g., an unchecked throwable; or a problem with the implementation of the test.</xs:documentation>
         </xs:annotation>
      </xs:attribute>
      <xs:attribute name="time" type="xs:decimal" use="required">
         <xs:annotation>
            <xs:documentation xml:lang="en">Time taken (in seconds) to execute the tests in the suite</xs:documentation>
         </xs:annotation>
      </xs:attribute>
   </xs:complexType>
   <xs:simpleType name="pre-string">
      <xs:restriction base="xs:string">
         <xs:whiteSpace value="preserve" />
      </xs:restriction>
   </xs:simpleType>
</xs:schema>

Relax NG Compact Schema

For junit.rnc, we have in Relax NG Compact Schema:

# junit.rnc: 
#---------------------------------------------------------------------------------- 
start = testsuite 

property = element property { 
   attribute name {text}, 
   attribute value {text} 
} 

properties = element properties { 
   property* 
} 

failure = element failure { 
   attribute message {text}, 
   attribute type {text}, 
   text 
} 

testcase = element testcase { 
   attribute classname {text}, 
   attribute name {text}, 
   attribute time {text}, 
   failure? 
} 

testsuite = element testsuite { 
   attribute errors {xsd:integer}, 
   attribute failures {xsd:integer}, 
   attribute hostname {text}, 
   attribute name {text}, 
   attribute tests {xsd:integer}, 
   attribute time {xsd:double}, 
   attribute timestamp {xsd:dateTime}, 
   properties, 
   testcase*, 
   element system-out {text}, 
   element system-err {text} 
} 
#---------------------------------------------------------------------------------- 

And for junitreport.rnc, we have:

# junitreport.rnc 
#---------------------------------------------------------------------------------- 
include "junit.rnc" { 
   start = testsuites 
   testsuite = element testsuite { 
      attribute errors {xsd:integer}, 
      attribute failures {xsd:integer}, 
      attribute hostname {text}, 
      attribute name {text}, 
      attribute tests {xsd:integer}, 
      attribute time {xsd:double}, 
      attribute timestamp {xsd:dateTime}, 
      attribute id {text}, 
      attribute package {text}, 
      properties, 
      testcase*, 
      element system-out {text}, 
      element system-err {text} 
   } 
} 

testsuites = element testsuites { 
   testsuite* 
}
@pqnelson
Copy link
Author

The body of a <failure> (and <error>) element is typically the stacktrace of the failure or error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment