<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 4.2 使用Catch2庫進行單元測試 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-04/recipe-02 中找到,包含一個C++的示例。該示例在CMake 3.5版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows上進行過測試。* 前面的配置中,使用返回碼來表示`test.cpp`測試的成功或失敗。對于簡單功能沒問題,但是通常情況下,我們想要使用一個測試框架,它提供了相關基礎設施來運行更復雜的測試,包括固定方式進行測試,與數值公差的比較,以及在測試失敗時輸出更好的錯誤報告。這里,我們用目前比較流行的測試庫Catch2( https://github.com/catchorg/Catch2 )來進行演示。這個測試框架有個很好的特性,它可以通過單個頭庫包含在項目中進行測試,這使得編譯和更新框架特別容易。這個配置中,我們將CMake和Catch2結合使用,來測試上一個求和代碼。 我們需要`catch.hpp`頭文件,可以從 https://github.com/catchorg/Catch2 (我們使用的是版本2.0.1)下載,并將它與`test.cpp`一起放在項目的根目錄下。 ## 準備工作 `main.cpp`、`sum_integers.cpp`和`sum_integers.hpp`與之前的示例相同,但將更新`test.cpp`: ```c++ #include "sum_integers.hpp" // this tells catch to provide a main() // only do this in one cpp file #define CATCH_CONFIG_MAIN #include "catch.hpp" #include <vector> TEST_CASE("Sum of integers for a short vector", "[short]") { auto integers = {1, 2, 3, 4, 5}; REQUIRE(sum_integers(integers) == 15); } TEST_CASE("Sum of integers for a longer vector", "[long]") { std::vector<int> integers; for (int i = 1; i < 1001; ++i) { integers.push_back(i); } REQUIRE(sum_integers(integers) == 500500); } ``` `catch.hpp`頭文件可以從https://github.com/catchorg/Catch2 (版本為2.0.1)下載,并將它與`test.cpp`放在項目的根目錄中。 ## 具體實施 使用Catch2庫,需要修改之前的所使用`CMakeList.txt`: 1. 保持`CMakeLists.txt`大多數部分內容不變: ```cmake # set minimum cmake version cmake_minimum_required(VERSION 3.5 FATAL_ERROR) # project name and language project(recipe-02 LANGUAGES CXX) # require C++11 set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) # example library add_library(sum_integers sum_integers.cpp) # main code add_executable(sum_up main.cpp) target_link_libraries(sum_up sum_integers) # testing binary add_executable(cpp_test test.cpp) target_link_libraries(cpp_test sum_integers) ``` 2. 對于上一個示例的配置,需要保留一個測試,并重命名它。注意,`--success`選項可傳遞給單元測試的可執行文件。這是一個Catch2選項,測試成功時,也會有輸出: ```cmake enable_testing() add_test( NAME catch_test COMMAND $<TARGET_FILE:cpp_test> --success ) ``` 3. 就是這樣!讓我們來配置、構建和測試。CTest中,使用`-V`選項運行測試,以獲得單元測試可執行文件的輸出: ```shell $ mkdir -p build $ cd build $ cmake .. $ cmake --build . $ ctest -V UpdateCTestConfiguration from :/home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build/DartConfiguration.tcl UpdateCTestConfiguration from :/home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build/DartConfiguration.tcl Test project /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build Constructing a list of tests Done constructing a list of tests Updating test list for fixtures Added 0 tests to meet fixture requirements Checking test dependency graph... Checking test dependency graph end test 1 Start 1: catch_test 1: Test command: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build/cpp_test "--success" 1: Test timeout computed to be: 10000000 1: 1: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1: cpp_test is a Catch v2.0.1 host application. 1: Run with -? for options 1: 1: ---------------------------------------------------------------- 1: Sum of integers for a short vector 1: ---------------------------------------------------------------- 1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:10 1: ................................................................... 1: 1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:12: 1: PASSED: 1: REQUIRE( sum_integers(integers) == 15 ) 1: with expansion: 1: 15 == 15 1: 1: ---------------------------------------------------------------- 1: Sum of integers for a longer vector 1: ---------------------------------------------------------------- 1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:15 1: ................................................................... 1: 1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:20: 1: PASSED: 1: REQUIRE( sum_integers(integers) == 500500 ) 1: with expansion: 1: 500500 (0x7a314) == 500500 (0x7a314) 1: 1: =================================================================== 1: All tests passed (2 assertions in 2 test cases) 1: 1/1 Test #1: catch_test ....................... Passed 0.00 s 100% tests passed, 0 tests failed out of 1 Total Test time (real) = 0.00 se ``` 4. 我們也可以測試`cpp_test`的二進制文件,可以直接從Catch2中看到輸出: ```shell $ ./cpp_test --success ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cpp_test is a Catch v2.0.1 host application. Run with -? for options ------------------------------------------------------------------- Sum of integers for a short vector ------------------------------------------------------------------- /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:10 ................................................................... /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:12: PASSED: REQUIRE( sum_integers(integers) == 15 ) with expansion: 15 == 15 ------------------------------------------------------------------- Sum of integers for a longer vector ------------------------------------------------------------------- /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:15 ................................................................... /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:20: PASSED: REQUIRE( sum_integers(integers) == 500500 ) with expansion: 500500 (0x7a314) == 500500 (0x7a314) =================================================================== All tests passed (2 assertions in 2 test cases) ``` 5. Catch2將生成一個可執行文件,還可以嘗試執行以下命令,以探索單元測試框架提供的選項: ```shell $ ./cpp_test --help ``` ## 工作原理 Catch2是一個單頭文件測試框架,所以不需要定義和構建額外的目標。只需要確保CMake能找到`catch.hpp`,從而構建`test.cpp`即可。為了方便起見,將它放在與`test.cpp`相同的目錄中,我們可以選擇一個不同的位置,并使用`target_include_directory`指示該位置。另一種方法是將頭部封裝到接口庫中,這可以在Catch2文檔中說明( https://github.com/catchorg/catch2/blob/maste/docs/build.systems.md#cmake ): ```cmake # Prepare "Catch" library for other executables set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/catch) add_library(Catch INTERFACE) target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR}) ``` 然后,我們對庫進行如下鏈接: ```cmake target_link_libraries(cpp_test Catch) ``` 回想一下第3中的討論,在第1章從簡單的可執行庫到接口庫,是CMake提供的偽目標庫,這些偽目標庫對于指定項目外部目標的需求非常有用。 ## 更多信息 這是一個簡單的例子,主要關注CMake。當然,Catch2提供了更多功能。有關Catch2框架的完整文檔,可訪問 https://github.com/catchorg/Catch2 。 Catch2代碼庫包含有CMake函數,用于解析Catch測試并自動創建CMake測試,不需要顯式地輸入`add_test()`函數,可見 https://github.com/catchorg/Catch2/blob/master/contrib/ParseAndAddCatchTests.cmake 。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看