简体   繁体   English

单元测试运行配置

[英]Unit test run configuration

I need to get up and running with Cmocka unit testing framework. 我需要启动并运行Cmocka单元测试框架。 My setup is: 我的设置是:

src/math/addition/add.c (+add.h) src / math / addition / add.c(+ add.h)

int add(int a, int b) {return a + b;}

src/math/subtraction/sub.c (+sub.h) src /数学/减法/sub.c(+ sub.h)

int sub(int a, int b) {return a - b;}

Makefile 生成文件

VPATH := src src/math src/math/addition
CFLAGS += -Isrc -Isrc/math -Isrc/math/addition
all: libMath clean

libMath: add.o sub.o
    ar rcs bin/libMath add.o sub.o

clean:
    rm -rf *.o

%.o: %.c %.h

Unit Tests 单元测试

test/math/addition/add_test.c 测试/数学/加法/add_test.c

#include "../src/math/addition/add.h"

void test_add() {
     assert(add(4, 5), 9);
}

test/math/subtraction/sub_test.c 测试/数学/减法/sub_test.c

#include "../src/math/subtraction/sub.h"

void test_sub() {
    assert(sub(9, 5), 4);
}

test/math/addition/add_test.c (from cmocka.org ) test / math / addition / add_test.c (来自cmocka.org

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

/* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
    (void) state; /* unused */
}

int main(void) {
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(null_test_success),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}

I'm new to unit testing in C and basically can't get my head around to setup the unit tests including linking the Cmocka library etc. 我是C语言单元测试的新手,基本上无法动手设置单元测试,包括链接Cmocka库等。

My idea is to have several unit test files instead of putting all unit tests in one file. 我的想法是拥有多个单元测试文件,而不是将所有单元测试放在一个文件中。

Edit based on Clearer's answer 根据Clearer的答案进行编辑

Scaling up 扩大

Going from 1 test file to 2 and 3 and it will be at least 10+ files. 从1个测试文件到2和3个文件,至少将有10个以上的文件。 Looking for some optimizations and articulations to scale up nicely and for easy management. 寻找一些优化和表达以很好地扩展并易于管理。 Here's what I've so far. 这是我到目前为止的内容。

VPATH := src/math/add  src/math/sub  src/math/mul # split src/test path
VPATH += test/math/add test/math/sub test/math/mul

all: libMath clean

libMath: add.o sub.o mul.o
    ar rcs bin/libMath add.o sub.o mul.o # suggestion? $^

test: add_test sub_test mul_test clean
    ./add_test
    ./sub_test
    ./mul_test

add_test: add_test.o add.o
    $(CC) -o $@ $^

sub_test: sub_test.o sub.o
    $(CC) -o $@ $^

mul_test: mul_test.o mul.o
    $(CC) -o $@ $^

clean:
    $(RM) *.o

%.o: %.c %.h

Here's the observation so far. 这是到目前为止的观察结果。

  1. The pattern seems to be like adding a new target for each couple of test and src files. 这种模式似乎就像为每个test和src文件添加一个新目标。
  2. Adding the .o object to the libMath both in the prerequisites and the command 在先决条件和命令中都将.o对象添加到libMath中
  3. adding the test executable under test: target in the prerequisites and the command 在先决条件和命令中添加被测试的测试可执行文件test: target

While scaling up, Is it better this way or there could be better approaches? 在扩大规模的同时,这会更好吗?还是可能有更好的方法?

PS I've removed CFLAGS line, it's working fine without it, helped me to clean up and reduce some clutter. PS我删除了CFLAGS线,如果没有它,它的工作正常,可以帮助我清理并减少混乱。 is it ok? 可以吗 My IDE (clion) shows red wiggly lines if the path isn't right to .h files so I'm using full paths in the test files to include src files. 如果路径不适合.h文件,我的IDE(clion)会显示红色的摆动行,因此我在测试文件中使用完整路径来包含src文件。

PPS It creates test executables on the root of the project, how to have all the binaries created in the bin folder and then delete all at the end of the project. PPS它在项目的根目录上创建测试可执行文件,以及如何在bin文件夹中创建所有二进制文件,然后在项目末尾删除所有二进制文件。

I would add a test target. 我会添加一个test目标。 That target would depend on all your test programs and should then execute the programs; 该目标将取决于您的所有测试程序,然后应执行这些程序; you may want to add individual targets to execute the programs and just keep one master test target to make sure all of them are executed. 您可能需要添加单个目标以执行程序,而只保留一个主测试目标以确保所有目标均已执行。 Each test program would depend on the object files that are required for the test; 每个测试程序都将取决于测试所需的目标文件。 if you're doing an addition test, let the addition test depend on the addition.o and add_test.o. 如果您要进行加法测试,则使加法测试取决于additional.o和add_test.o。 Link them as you would always do and then execute them. 像往常一样链接它们,然后执行它们。

Example: 例:

test: addition_test
   ./addition_test

addition_test: add_test.o add.o
    $(CC) -o $@ $^

Scaling tests 缩放测试

You can scale your tests by adding two rules and deleting most of the other rules, related to testing: 您可以通过添加两个规则并删除与测试相关的其他大多数规则来扩展测试:

test: add_test_run sub_test_run

%_run: %
    ./$<

 %_test: %.o %_test.o
    $(CC) -o $@ $^

Should do everything you want. 应该做您想要的一切。 This allows running tests in parallel; 这允许并行运行测试。 you can avoid running tests that doesn't need to be run by creating a file at the end of each run, say: a log file that tells you the result of the test run. 您可以通过在每次运行结束时创建一个文件来避免运行不需要运行的测试,例如:一个告诉您测试运行结果的日志文件。

This should do the trick: 这应该可以解决问题:

test: add_test.log sub_test.log

%.log: %
    ./$^ > $@

 %_test: %.o %_test.o
    $(CC) -o $@ $^

You should use $(RM) instead of rm -rf in your clean target. 您应该在干净的目标中使用$(RM)而不是rm -rf $(RM) is platform independent while rm -rf only works on UNIXy platforms. $(RM)与平台无关,而rm -rf仅在UNIXy平台上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM