如何让CMake构建但不运行测试

时间:2017-11-23 14:59:47

标签: cmake

在CMake中,我如何指示应该构建C测试程序,但不能运行?

对于netCDF C库,一个由天气,气候和太空科学家使用的免费软件科学数据包,我们有一个CMake和一个autotools构建。该代码可在github上找到。你可以在那里看到CMake文件。

在我们的测试中,我们有一些shell脚本和一些C程序。在这种情况下,我需要在运行C测试之前完成一些shell脚本操作。因为我希望所有这些都能用于并行构建,所以最简单的解决方案似乎是让shell程序在需要时调用C测试程序,而不是在测试期间调用C测试程序。

例如,我有一个测试程序tst_interops3.c(在目录nc_test4中)。我还有一个shell脚本测试run_get_hdf4_files.sh。这是:

#!/bin/sh

# This shell gets files from the netCDF ftp site for testing.

# $Id: run_get_hdf4_files.sh,v 1.4 2009/07/15 15:16:05 ed Exp $

set -e
echo ""
file_list="AMSR_E_L2_Rain_V10_200905312326_A.hdf AMSR_E_L3_DailyLand_V06_20020619.hdf \
    MYD29.A2009152.0000.005.2009153124331.hdf MYD29.A2002185.0000.005.2007160150627.hdf \
    MOD29.A2000055.0005.005.2006267200024.hdf"
echo "Getting HDF4 test files $file_list"

for f1 in $file_list
do
    if ! test -f $f1; then
    curl -O "ftp://ftp.unidata.ucar.edu/pub/netcdf/sample_data/hdf4/$f1.gz"
    gunzip $f1.gz
    fi
done

${execdir}/tst_interops3

echo "SUCCESS!!!"

exit 0

此脚本下载一些数据文件,然后运行程序tst_interops3,它读取数据文件以确保netCDF可以读取HDF4数据文件。 (HDF4是一种传统的科学数据格式)。

在autotools中,我在check_PROGRAMS中列出了tst_interops3,但没有在TESTS中列出。这会导致它被构建,但不会运行。 (它在脚本中运行。)

然而,我无法弄清楚如何在CMake中做同样的事情。如何构建测试程序,但不能运行?

1 个答案:

答案 0 :(得分:0)

没有预定义的"检查" CMake中的目标;与CMake一起使用的大多数测试基础设施定义了" test"伪目标,但不是"检查"。

所以你可以定义"检查"手动定位,并将其设置为依赖于您希望在此阶段构建的每个目标(如可执行文件):

# Add "check" target which simply executes "ctest" for perform testing.
add_custom_target(check COMMAND "ctest")

# Create an executable which is needed only for testing.
# It *won't* be built on "make all" or "make".
add_executable(test_program_a EXCLUDE_FROM_ALL test_program_a.c)

# Mark the executable to be built on "make check".
add_dependencies(check test_program_a)

# Create some test which involves the executable created above.
# This test is executed on "ctest" (or "make test").
# So it will be executed on "make check" too, after building the test program.
add_test(NAME test.01 COMMAND test_program_a "val1")
相关问题