Unit Tests

1

Testing

4 minute

C project tests often start as another main:

#include <assert.h>

int main(void)
{
    assert(checksum((uint8_t[]){1, 2, 3}, 3) == 6);
    return 0;
}

Or as small programs, shell scripts, and Makefile targets:

test:
	./test_checksum
	./test_parser

Rust puts the entry point into Cargo:

cargo test

First model: Rust tests are not a separate project system. They are part of the crate.

Minimal Unit Test

A normal function:

fn checksum(data: &[u8]) -> u32 {
    data.iter().map(|b| *b as u32).sum()
}

A nearby test:

Read More