gpt4 book ai didi

c++ - 为什么是 'make test' 和 "./test/Test"

转载 作者:行者123 更新时间:2023-11-28 02:05:09 29 4
gpt4 key购买 nike

现在我正在使用 CMake 设置一个 C++ 测试环境。其实我已经意识到我想做什么,但我对两种不同的测试输出风格感到困惑。在我下面的示例中,“make test”实际上做了什么?我认为“make test”和“./test/Test”输出相同,但不完全相同。 'make test' 输出与 googletest 输出风格不同。虽然测试结果看起来一样,但我对这些输出并不满意。

输出差异

$ make test                 
Running tests...
Test project /path/to/sample/build
Start 1: MyTest
1/1 Test #1: MyTest ...........................***Failed 0.02 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) = 0.02 sec

The following tests FAILED:
1 - MyTest (Failed)
Errors while running CTest
make: *** [test] エラー 8

$ ./test/Test
Running main() from gtest_main.cc
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from MyLibTest
[ RUN ] MyLibTest.valCheck
/path/to/test/test.cc:10: Failure
Expected: sqr(1.0)
Which is: 1
To be equal to: 2.0
Which is: 2
[ FAILED ] MyLibTest.valCheck (0 ms)
[ RUN ] MyLibTest.negativeValCheck
[ OK ] MyLibTest.negativeValCheck (0 ms)
[----------] 2 tests from MyLibTest (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
[ FAILED ] 1 test, listed below:
[ FAILED ] MyLibTest.valCheck

1 FAILED TEST

命令

mkdir build
cd build
cmake ..
make test // NOT googletest output style
./test/Test // It looks googletest output

我的环境

root
- CMakeLists.txt
+ src/
- CMakeLists.txt
- main.cc
- sqr.cc
- sqr.h
+ test/
- CMakeLists.txt
- test.cc

root/CMakeLists.txt

 cmake_minimum_required(VERSION 2.8)
project (MYTEST)

add_subdirectory(src)
add_subdirectory(test)
enable_testing()
add_test(NAME MyTest COMMAND Test)

测试/CMakeLists.txt

 cmake_minimum_required(VERSION 2.8)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set(GTEST_ROOT /path/to/googletest/googletest)
include_directories(${GTEST_ROOT}/include/)
link_directories(${GTEST_ROOT}/build/)
add_executable(Test ${CMAKE_CURRENT_SOURCE_DIR}/test.cc)
target_link_libraries(Test sqr gtest gtest_main pthread)

测试/测试/cc

 #include "../src/sqr.h"
#include <gtest/gtest.h>

namespace {
class MyLibTest : public ::testing::Test{};

TEST_F(MyLibTest, valCheck) {
EXPECT_EQ(sqr(3.0), 9.0);
EXPECT_EQ(sqr(1.0), 2.0); // it fails!
}

TEST_F(MyLibTest, negativeValCheck) {
EXPECT_EQ(sqr(-3.0), 9.0);
}
}

最佳答案

您可以使用环境变量修改 ctest 的行为(这是 make test 最终将执行的行为)。

例如:

CTEST_OUTPUT_ON_FAILURE=1 make test

这将打印失败的测试可执行文件的完整输出。

您可能感兴趣的另一个是 CTEST_PARALLEL_LEVEL

关于c++ - 为什么是 'make test' 和 "./test/Test",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37806931/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com