单元测试运行配置 [英] Unit test run configuration

查看:51
本文介绍了单元测试运行配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要启动并运行 Cmocka 单元测试框架.我的设置是:

I need to get up and running with Cmocka unit testing framework. My setup is:

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

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

src/math/subtraction/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

单元测试

test/math/addition/add_test.c

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

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

test/math/subtraction/sub_test.c

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

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

test/math/addition/add_test.c(来自 cmocka.组织)

#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);
}

我是 C 单元测试的新手,基本上无法设置单元测试,包括链接 Cmocka 库等.

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.

我的想法是拥有多个单元测试文件,而不是将所有单元测试放在一个文件中.

My idea is to have several unit test files instead of putting all unit tests in one file.

扩大规模

从 1 个测试文件到 2 和 3,它将至少有 10 个以上的文件.寻找一些优化和衔接,以很好地扩展和易于管理.到目前为止,这是我所做的.

Going from 1 test file to 2 and 3 and it will be at least 10+ files. 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

这是目前的观察结果.

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

在扩大规模的同时,这种方式更好还是可以有更好的方法?

While scaling up, Is it better this way or there could be better approaches?

P.S. 我已经移除了 CFLAGS 线,没有它也能正常工作,帮助我清理并减少了一些混乱.可以吗?如果 .h 文件的路径不正确,我的 IDE (clion) 会显示红色的摆动线,因此我在测试文件中使用完整路径来包含 src 文件.

P.S. I've removed CFLAGS line, it's working fine without it, helped me to clean up and reduce some clutter. 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.

P.P.S 它在项目的根目录上创建测试可执行文件,如何在 bin 文件夹中创建所有二进制文件,然后在项目结束时删除所有文件.

P.P.S 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.

推荐答案

我会添加一个 test 目标.该目标将取决于您的所有测试程序,然后应该执行这些程序;您可能希望添加单独的目标来执行程序,并只保留一个主测试目标以确保所有这些目标都被执行.每个测试程序都依赖于测试所需的目标文件;如果你在做加法测试,让加法测试依赖于addition.o 和add_test.o.像往常一样链接它们,然后执行它们.

I would add a test target. 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. Link them as you would always do and then execute them.

示例:

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.

这应该可以解决问题:

test: add_test.log sub_test.log

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

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

您应该在干净的目标中使用 $(RM) 而不是 rm -rf .$(RM) 与平台无关,而 rm -rf 仅适用于 UNIXy 平台.

You should use $(RM) instead of rm -rf in your clean target. $(RM) is platform independent while rm -rf only works on UNIXy platforms.

这篇关于单元测试运行配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆