基于 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-args
和 args
。如上述所引用的内容,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
上述 test1
和 test2
作为 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
==============================================================================
参考资料