簡體   English   中英

為什么要“進行測試”和“ ./test/Test”

[英]Why 'make test' and “./test/Test”

現在,我使用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

根/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);
   }
 }

您可以使用環境變量來修改ctest的行為(make測試最終將執行該行為)。

例如:

CTEST_OUTPUT_ON_FAILURE=1 make test

這將為失敗的測試可執行文件輸出完整的輸出。

您可能感興趣的另一個是CTEST_PARALLEL_LEVEL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM