为什么'make test'和“./test/Test”

时间:2016-06-14 08:37:13

标签: c++ makefile cmake googletest

现在我正在使用CMake建立一个c ++测试环境。其实我已经意识到我想做什么,但我对2种不同的测试输出风格感到困惑。 在下面的例子中,'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)

测试/检验/立方厘米

 #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);
   }
 }

1 个答案:

答案 0 :(得分:2)

您可以使用环境变量修改ctest的行为(这是使测试最终执行的内容)。

例如:

CTEST_OUTPUT_ON_FAILURE=1 make test

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

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