meson test 的 --test-args 参数

基于 meson构建的应用程序,可以借助 meson test来进行测试。meson test支持很多参数,如下所示:

$ meson test [-h] [--maxfail MAXFAIL] [--repeat REPEAT] [--no-rebuild]
             [--gdb] [--gdb-path GDB_PATH] [--list] [--wrapper WRAPPER]
             [-C WD] [--suite SUITE] [--no-suite SUITE] [--no-stdsplit]
             [--print-errorlogs] [--benchmark] [--logbase LOGBASE]
             [--num-processes NUM_PROCESSES] [-v] [-q]
             [-t TIMEOUT_MULTIPLIER] [--setup SETUP]
             [--test-args TEST_ARGS]
             [args [args ...]]

各个参数的含义如下:

positional arguments:
  args                                  Optional list of test names to run.
                                        "testname" to run all tests with that
                                        name, "subprojname:testname" to
                                        specifically run "testname" from
                                        "subprojname", "subprojname:" to run all
                                        tests defined by "subprojname".

optional arguments:
  -h, --help                            show this help message and exit
  --maxfail MAXFAIL                     Number of failing tests before aborting
                                        the test run. (default: 0, to disable
                                        aborting on failure)
  --repeat REPEAT                       Number of times to run the tests.
  --no-rebuild                          Do not rebuild before running tests.
  --gdb                                 Run test under gdb.
  --gdb-path GDB_PATH                   Path to the gdb binary (default: gdb).
  --list                                List available tests.
  --wrapper WRAPPER                     wrapper to run tests with (e.g.
                                        Valgrind)
  -C WD                                 directory to cd into before running
  --suite SUITE                         Only run tests belonging to the given
                                        suite.
  --no-suite SUITE                      Do not run tests belonging to the given
                                        suite.
  --no-stdsplit                         Do not split stderr and stdout in test
                                        logs.
  --print-errorlogs                     Whether to print failing tests' logs.
  --benchmark                           Run benchmarks instead of tests.
  --logbase LOGBASE                     Base name for log file.
  --num-processes NUM_PROCESSES         How many parallel processes to use.
  -v, --verbose                         Do not redirect stdout and stderr
  -q, --quiet                           Produce less output to the terminal.
  -t TIMEOUT_MULTIPLIER, --timeout-multiplier TIMEOUT_MULTIPLIER
                                        Define a multiplier for test timeout,
                                        for example when running tests in
                                        particular conditions they might take
                                        more time to execute. (<= 0 to disable
                                        timeout)
  --setup SETUP                         Which test setup to use.
  --test-args TEST_ARGS                 Arguments to pass to the specified
                                        test(s) or all tests

在这里,重点关注两个参数:--test-argsargs。如上述所引用的内容,args参数指定了需要运行的测试用例名称,并且可指定多次,即运行多个测试用例。--test-args参数会将其取值传给应用程序,作为应用程序的参数。下面借助一个简单的示例程序来使用一下这两个参数:
首先,定义执行程序的源码文件:main.c,内容如下所示:

#include <stdio.h>

int main(int argc, char *argv[])
{
	int i = 0;

	for (i = 0; i < argc; ++i) {
		printf("arg-%d: %s\n", i, argv[i]);
	}

	return 0;
}

其次,定义构建脚本:meson.build,内容如下所示:

project('simple', 'c')
exe = executable('main', 'main.c')
test('test1', exe)
test('test2', exe)
test('test3', exe)

使用 meson test运行测试用例:

$ meson test -C build/ --test-args="-a 1 -b 2" test1 test2
ninja: Entering directory `/home/zhoumin/test/meson/build'
ninja: no work to do.
1/2 test1        OK              0.01s
2/2 test2        OK              0.01s

Ok:                 2
Expected Fail:      0
Fail:               0
Unexpected Pass:    0
Skipped:            0
Timeout:            0

Full log written to /home/zhoumin/test/meson/build/meson-logs/testlog.txt

上述 test1test2作为 meson test的参数,指定了需要运行的测试用例名称。--test-args="-a 1 -b 2"作为 meson test的参数,指定了给应用程序传递的参数信息:"-a 1 -b 2",打开 build/meson-logs/testlog.txt文件,可以看到应用程序接收到的参数列表,如下所示:

==================================== 1/2 =====================================
test:         test1
start time:   10:36:27
duration:     0.01s
result:       exit status 0
command:      MALLOC_PERTURB_=237 /home/zhoumin/test/meson/build/main -a 1 -b 2
----------------------------------- stdout -----------------------------------
arg-0: /home/zhoumin/test/meson/build/main
arg-1: -a
arg-2: 1
arg-3: -b
arg-4: 2
==============================================================================

参考资料

  1. https://mesonbuild.com/Commands.html
  2. https://mesonbuild.com/Meson-sample.html
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值