在Cmake中使用REQUIRED_FILES进行单元测试 [英] Using REQUIRED_FILES for unit tests in cmake

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

问题描述

尝试使用 REQUIRED_FILES的简短示例

$ tree .
.
├── CMakeLists.txt
└── main.cxx





$ cat main.cxx 
int main() { return 0; }





$ cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.10)

add_executable(a.out main.cxx)

include(CTest)
add_test(NAME a.out COMMAND a.out)
set_property(TEST a.out PROPERTY REQUIRED_FILES $<TARGET_FILE:a.out>)

一个可执行文件,不执行任何操作,并且是一个仅要求自身存在的测试。

One executable, which does nothing, and is a test that just requires itself to exist.

$ mkdir build && cd build && cmake .. && ctest
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/lib64/ccache/cc
-- Check for working C compiler: /usr/lib64/ccache/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/lib64/ccache/c++
-- Check for working CXX compiler: /usr/lib64/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /[...]/req/build
Test project /[...]/req/build
    Start 1: a.out
Could not find executable /[...]/req/build/a.out
Looked in the following places:
/[...]/req/build/a.out
/[...]/req/build/a.out
/[...]/req/build/Release/a.out
/[...]/req/build/Release/a.out
/[...]/req/build/Debug/a.out
/[...]/req/build/Debug/a.out
/[...]/req/build/MinSizeRel/a.out
/[...]/req/build/MinSizeRel/a.out
/[...]/req/build/RelWithDebInfo/a.out
/[...]/req/build/RelWithDebInfo/a.out
/[...]/req/build/Deployment/a.out
/[...]/req/build/Deployment/a.out
/[...]/req/build/Development/a.out
/[...]/req/build/Development/a.out
[...]/req/build/a.out
[...]/req/build/a.out
[...]/req/build/Release/a.out
[...]/req/build/Release/a.out
[...]/req/build/Debug/a.out
[...]/req/build/Debug/a.out
[...]/req/build/MinSizeRel/a.out
[...]/req/build/MinSizeRel/a.out
[...]/req/build/RelWithDebInfo/a.out
[...]/req/build/RelWithDebInfo/a.out
[...]/req/build/Deployment/a.out
[...]/req/build/Deployment/a.out
[...]/req/build/Development/a.out
[...]/req/build/Development/a.out
Unable to find required file: /[...]/req/build/a.out
1/1 Test #1: a.out ............................***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.00 sec

The following tests FAILED:
      1 - a.out (Not Run)
Errors while running CTest

我认为 REQUIRED_FILES 的目的是避免运行此测试,因为该文件不存在,因此 not 测试失败。正确使用此属性的正确方法是什么?

I thought the point of REQUIRED_FILES is to avoid running this test, because that file doesn't exist, and hence not have a failed test. What's the right way to aactually use this property?

推荐答案

您正在正确使用该属性,因为ctest并未实际运行测试。 CTest不能将测试标记为未运行,否则不会将其标记为失败,因此它将测试放置在 Not Run bin。

You are using the property correctly as the ctest didn't actually run the test. CTest can't marked the test as successful as it didn't run, and won't mark the test as failed due to the required files so it places the test in the Not Run bin.

为什么找不到所需的文件,这是由于未构建a.out二进制文件。您缺少脚本中的步骤:

As for why the required files are not found, that is due to the fact that a.out binary was not built. You are missing a step in your script:

mkdir build && cd build && cmake .. && cmake --build . && ctest

您还可以将脚本简化为:

You could also simplify the script to:

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

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