全部。
\n我决定使用新的 cmake 宏来下载外部依赖项。\n我从 Catch2 库的文档中获取了示例代码。
\ninclude(FetchContent)\n\nFetchContent_Declare(\n Catch2\n GIT_REPOSITORY https://github.com/catchorg/Catch2.git\n GIT_TAG v2.13.4\n)\nFetchContent_GetProperties(Catch2)\nif(NOT Catch2_POPULATED)\n FetchContent_Populate(Catch2)\n add_subdirectory(${catch2_SOURCE_DIR} ${catch2_BINARY_DIR})\nendif()\nRun Code Online (Sandbox Code Playgroud)\n该解决方案效果很好,除了在我离线时重新启动 cmake 的能力(没有 wifi 和移动网络,只有我和我的笔记本电脑)。\n我收到以下错误:
\n[0/7] Performing update step for 'catch2-populate'\nfatal: \xc2\xabhttps://github.com/catchorg/Catch2.git/\xc2\xbb \xd0\xbd\xd0\xb5\xd0\xb4\xd0\xbe\xd1\x81\xd1\x82\xd1\x83\xd0\xbf\xd0\xbd\xd0\xbe: Could not resolve host: github.com\nCMake Error at /Users/evgeny.proydakov/repository/ihft/build/_deps/catch2-subbuild/catch2-populate-prefix/tmp/catch2-populate-gitupdate.cmake:97 (execute_process):\n execute_process failed command indexes:\n\n 1: "Child return code: 128"\n\nFAILED: catch2-populate-prefix/src/catch2-populate-stamp/catch2-populate-update \ncd /Users/evgeny.proydakov/repository/ihft/build/_deps/catch2-src && /usr/local/Cellar/cmake/3.20.1/bin/cmake -P /Users/evgeny.proydakov/repository/ihft/build/_deps/catch2-subbuild/catch2-populate-prefix/tmp/catch2-populate-gitupdate.cmake\nninja: build stopped: subcommand failed.\n\nCMake Error at /usr/local/Cellar/cmake/3.20.1/share/cmake/Modules/FetchContent.cmake:1012 (message):\n Build step for catch2 failed: 1\nCall Stack (most recent call first):\n /usr/local/Cellar/cmake/3.20.1/share/cmake/Modules/FetchContent.cmake:1141:EVAL:2 (__FetchContent_directPopulate)\n /usr/local/Cellar/cmake/3.20.1/share/cmake/Modules/FetchContent.cmake:1141 …Run Code Online (Sandbox Code Playgroud) 我有以下项目结构:
test_main.cc
#define CATCH_CONFIG_MAIN
#include "catch2.hpp"
Run Code Online (Sandbox Code Playgroud)
测试1.cc
#include "catch2.hpp"
#include "test_utils.hpp"
TEST_CASE("test1", "[test1]") {
REQUIRE(1 == 1);
}
Run Code Online (Sandbox Code Playgroud)
测试2.cc
#include "catch2.hpp"
#include "test_utils.hpp"
TEST_CASE("test2", "[test2]") {
REQUIRE(2 == 2);
}
Run Code Online (Sandbox Code Playgroud)
test_utils.hpp
#pragma once
#include <iostream>
void something_great() {
std::cout << ":)\n";
}
Run Code Online (Sandbox Code Playgroud)
如果我使用类似的东西进行编译clang++ -std=c++17 test_main.cc test1.cc test2.cc,则该函数something_great在 test1.o 和 test2.o 中都有定义。这会导致类似的错误
duplicate symbol __Z15something_greatv in:
test1.cc.o
test2.cc.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to …Run Code Online (Sandbox Code Playgroud) 我正在使用 Catch v2.13.1
比较浮点值的正确方法是什么?我以为下面的会失败,但都通过了。
REQUIRE(1147332687.7189338 == Approx(1147332688.4281545).margin(0.0001));
REQUIRE(1147332687.7189338 == Approx(1147332688.4281545));
Run Code Online (Sandbox Code Playgroud)
然而这按预期失败了
REQUIRE(abs(1147332687.7189338 - 1147332688.4281545) <= Approx(0).margin(0.0001));
Run Code Online (Sandbox Code Playgroud)
我不明白为什么前两个语句不起作用
我正在尝试安装Catch2我正在尝试在 ubuntu 20.04 上
使用这里的指令的指令。
这就是我所做的:
$ git clone https://github.com/catchorg/Catch2.git
$ cd Catch2
$ cmake -Bbuild -H. -DBUILD_TESTING=OFF
$ sudo cmake --build build/ --target install
Run Code Online (Sandbox Code Playgroud)
比它告诉我一切都好:输出链接。
但是:当我尝试从这里编译示例时: //
主程序
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <catch2/catch.hpp>
std::uint64_t Fibonacci(std::uint64_t number) {
return number < 2 ? 1 : Fibonacci(number - 1) + Fibonacci(number - 2);
}
TEST_CASE("Fibonacci") {
CHECK(Fibonacci(0) …Run Code Online (Sandbox Code Playgroud) 我似乎没有让我的 CTest 项目识别我的 Catch2 测试。测试项目本身构建良好,我设法使用它创建的可执行文件运行测试。但是在运行时
ctest -V
Run Code Online (Sandbox Code Playgroud)
我不断得到的输出是:
UpdateCTestConfiguration from :/home/user/code/project/libs/DartConfiguration.tcl
UpdateCTestConfiguration from :/home/user/code/project/libs/DartConfiguration.tcl
Test project /home/user/code/project/libs/
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
No tests were found!!!
Run Code Online (Sandbox Code Playgroud)
我的设置如下:
文件夹结构:
libs
??? maths
? ??? matrix.cpp / hpp
? ??? function.cpp / hpp
? ??? CMakeLists.txt
??? ctest
? ??? matrix_test.cpp
? ??? function_test.cpp
? ??? CMakeLists.txt
??? build
Run Code Online (Sandbox Code Playgroud)
???CMakeLists.txt
我从构建文件夹构建 …
我正在使用实际devel分支中的 Catch2,并且在尝试比较两个时出现链接器错误vector<string_view>
该问题可以重现如下:
#include <vector>
#include <string_view>
#include <catch2/catch_test_macros.hpp>
TEST_CASE("Can split string", "[split]") {
vector<string_view> splittedString = {"this is a string view"sv};
vector<string_view> EXPECTED = {"I'm"sv, "Elvis"sv,"Dukaj"sv};
REQUIRE(splittedString == EXPECTED);
}
Run Code Online (Sandbox Code Playgroud)
我的CMakeLists.txt:
find_package(Catch2 REQUIRED)
# ...
target_link_libraries(${PROJECT_NAME} PRIVATE Catch2::Catch2WithMain)
Run Code Online (Sandbox Code Playgroud)
尝试构建时出现以下链接器错误:
Catch2/src/catch2/../catch2/catch_tostring.hpp:126: error: undefined reference to `Catch::StringMaker<std::basic_string_view<char, std::char_traits<char> >, void>::convert[abi:cxx11](std::basic_string_view<char, std::char_traits<char> >)'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我错过了一些内容吗?
我使用自定义主文件†添加了我自己的命令行选项:
† https://github.com/catchorg/Catch2/blob/master/docs/own-main.md#adding-your-own-command-line-options
// ...
auto cli
= session.cli() // Get Catch's composite command line parser
| Opt( height, "height" ) // bind variable to a new option, with a hint string
["-g"]["--height"] // the option names it will respond to
("how high?"); // description string for the help output
// ...
}
Run Code Online (Sandbox Code Playgroud)
现在我想height在测试用例中使用命令行选项。什么是最好的方法来做到这一点?
QT 的某些部分依赖于事件循环的启动和运行(或者至少生成警告)。如何将 Catch2 测试与 QT 事件循环集成?
在文档中,他们只编译一个文件test.cpp,其中可能包含所有测试。我想将我的个人测试与包含的文件分开#define CATCH_CONFIG_MAIN,就像这样。
如果我有一个test.cpp包含#define CATCH_CONFIG_MAIN一个单独的测试文件的文件simple_test.cpp,我已经设法以simple_test.cpp这种方式生成一个包含测试的可执行文件:
find_package(Catch2 REQUIRED)
add_executable(tests test.cpp simple_test.cpp)
target_link_libraries(tests Catch2::Catch2)
include(CTest)
include(Catch)
catch_discover_tests(tests)
Run Code Online (Sandbox Code Playgroud)
但是,这是生成可执行文件的可接受的方式吗?从不同的教程中,如果我有更多的测试,我应该能够创建一个测试源库并将它们链接到test.cpp生成可执行文件:
find_package(Catch2 REQUIRED)
add_library(test_sources simple_test.cpp another_test.cpp)
target_link_libraries(test_sources Catch2::Catch2)
add_executable(tests test.cpp)
target_link_libraries(tests test_sources)
target_link_libraries(tests Catch2::Catch2)
include(CTest)
include(Catch)
catch_discover_tests(tests)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这个时,我收到了 CMake 警告Test executable ... contains no tests!。
总而言之,我应该创建一个测试库吗?如果是这样,我怎样才能让它包含我的测试。否则,将新的 test.cpp 文件添加到函数中是否正确add_executable?
我有一个使用 Catch 2.11.1 的简单单元测试:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <utility>
#include <any>
namespace A::B
{
namespace C
{
struct S
{
};
}
using type = std::pair<C::S, std::any>;
}
inline bool operator==(A::B::type const&, A::B::type const&)
{
return true;
}
TEST_CASE("test", "[test]")
{
auto t1 = std::make_pair(A::B::C::S(), std::any());
auto t2 = std::make_pair(A::B::C::S(), std::any());
REQUIRE(t1 == t2);
}
Run Code Online (Sandbox Code Playgroud)
上面的简单程序会产生以下错误:
$ g++ -Wall -Wextra -Wpedantic test-single.cpp -std=c++17
In file included from /usr/include/c++/9/bits/stl_algobase.h:64,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/string:40,
from catch.hpp:457,
from test-single.cpp:2:
/usr/include/c++/9/bits/stl_pair.h: In instantiation …Run Code Online (Sandbox Code Playgroud)