Menu

Tree [r4268] / trunk / tests / libtest /
 History

HTTPS access


File Date Author Commit
 bin 2025-01-27 jkoshy [r4111] libtest: Turn on expansion of the Subversion 'I...
 driver 2025-10-14 jkoshy [r4238] libtest: Handle potential failure of strdup().
 examples 2025-01-27 jkoshy [r4111] libtest: Turn on expansion of the Subversion 'I...
 lib 2025-01-27 jkoshy [r4111] libtest: Turn on expansion of the Subversion 'I...
 Makefile 2025-01-27 jkoshy [r4111] libtest: Turn on expansion of the Subversion 'I...
 README.rst 2019-01-21 jkoshy [r3672] Only use the 'test_' or 'TEST_' prefixes for sy...

Read Me

test(3) - a library for writing tests

The test(3) API and its related scaffolding generator (make-test-scaffolding(1)) work together to reduce the boilerplate needed for tests.

Quick Start

The following source code defines a test suite that contains a single test:

/* File: test.c */
#include "test.h"

enum test_result
tf_goodbye_world(testcase_state tcs)
{
        return (TEST_PASS);
}

By convention, test functions are named using a tf_ prefix.

Given an object file compiled from this source, the make-test-scaffolding(1) utility would generate scaffolding describing a single invocable test named "goodbye_world".

Test Cases

Test functions that are related to each other can be grouped into test cases. The following code snippet defines a test suite with two test functions contained in a test case named "helloworld":

/* File: test.c */
#include "test.h"

TEST_CASE_DESCRIPTION(helloworld) =
    "A description of the helloworld test case.";

enum test_result
tf_helloworld_hello(testcase_state tcs)
{
        return (TEST_PASS);
}

enum test_result
tf_helloworld_goodbye(testcase_state tcs)
{
        return (TEST_FAIL);
}

Test cases can define their own set up and tear down functions:

/* File: test.c continued. */
struct helloworld_test { .. state used by the helloworld tests .. };

enum testcase_status
tc_setup_helloworld(testcase_state *tcs)
{
        *tcs = ..allocate a struct helloworld_test.. ;
        return (TEST_CASE_OK);
}

enum testcase_status
tc_teardown_helloworld(testcase_state tcs)
{
        .. deallocate test case state..
        return (TEST_CASE_OK);
}

The set up function for a test case will be invoked prior to any of the functions that are part of the test case. The set up function can allocate test-specific state, which is then passed to each test function for its use.

The tear down function for a test case will be invoked after the test functions in the test case are invoked. This function is responsible for deallocating the resources allocated by its corresponding set up function.

Building Tests

Within the Elftoolchain Project's sources, the elftoolchain.test.mk rule set handles the process of invoking the make-test-scaffolding(1) utility and building an test executable.

# Example Makefile.

TOP=    ..path to the top of the elftoolchain source tree..

TEST_SRCS=      test.c

.include "$(TOP)/mk/elftoolchain.test.mk"

Further Reading

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.