3.1.29. waf_tools.mock

CMock and Unity test runner integration for the Waf build system.

This tool automates the generation of mock source files (via CMock) and test-runner source files (via Unity’s generate_test_runner.rb) for C unit tests. It is designed to work together with the preprocess and gcov tools in this toolchain.

3.1.29.1. Usage

  1. Load the tool during configure

    def configure(ctx):
        ctx.find_program("ruby", var="RUBY")
        ctx.load("mock", tooldir=TOOLDIR)
    
  2. Declare test targets in a test.json file (one per test directory)

    [
        {
            "source": [
                "src/app/abc/abc.c"
            ],
            "test": "test_abc.c",
            "includes": [
                "src/app/abc",
                "src/app/def"
            ],
            "mocks": [
                "src/app/def/def.h"
            ],
            "defines": [
                "EXAMPLE_DEFINE=1u"
            ],
            "use": [
                "M"
            ]
        }
    ]
    
  3. In the variant’s build script (tests/unit/wscript), load and instantiate the test configurations

    def build(bld):
        bld.load_test_configs()
        for i, cfg in enumerate(bld.test_configs):
            bld(
                features=cfg["features"],
                target=cfg["target"],
                source=cfg["source"],
                includes=cfg["includes"],
                mocks=cfg["mocks"] + ["test-framework", f"{var}-c-test-constants"],
                cmock_config=cfg["cmock_config"],
                uselib=cfg["COVERAGE"],
                defines=cfg["defines"],
                cflags=cfg["cflags"],
                path=cfg["path"],
                idx=i,
            )
    

3.1.29.2. Dependencies

  • Ruby interpreter (stored in env.RUBY)

  • CMock (bundled under tools/cmock/)

  • Unity (bundled as CMock vendor dependency)

  • preprocess waf tool (this toolchain)

Functions

add_mock_cflags(self)

Append mock-specific warning-suppression flags to mock compilation tasks.

configure(ctx)

Configure paths to CMock and the Unity test runner generator.

create_mocks(self, header_nodes, test_file)

Declare mock output nodes for each header to be mocked.

create_result(bld)

Create aggregated unit-test result files per target from the stored test results.

create_runners(self, test_file)

Declare the runner output node under runner/.

extract_targets_from_results(bld, test_results)

Group unit-test results by target name.

find_test_file(self)

Find the test source file associated with the current target.

load_test_configs(self)

Load test configurations from JSON files.

resolve_node(self, path)

Resolve a path string to a waf Node.

setup(self)

Set up mock generation and test runner creation for a test target.

Classes

CMock(*k, **kw)

Task that generates CMock mock source files from header files.

Runner(*k, **kw)

Task that generates Unity test runner source files.