Чтобы получить имя теста в рамках Catch2 на C++, вы можете использовать несколько методов. Вот несколько примеров:
Метод 1: использование SECTION
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
TEST_CASE("MyTest") {
SECTION("Subsection1") {
// Get the test name using Catch2 macros
std::string testName = Catch::getResultCapture().getCurrentTestName();
// Use the test name as needed
// ...
}
}
Метод 2: использование TEST_CASE_METHOD
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
class MyTestFixture {
public:
std::string getCurrentTestName() {
return Catch::getResultCapture().getCurrentTestName();
}
};
TEST_CASE_METHOD(MyTestFixture, "MyTest") {
// Get the test name using the fixture method
std::string testName = getCurrentTestName();
// Use the test name as needed
// ...
}
Метод 3: использование SECTION с Capturer
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
TEST_CASE("MyTest") {
SECTION("Subsection1") {
// Create a Capturer to capture test information
Catch::Capturer capturer;
// Get the test name from the Capturer
std::string testName = capturer.sections().back().name;
// Use the test name as needed
// ...
}
}
Это всего лишь несколько примеров того, как можно получить имя теста в платформе Catch2. Не забудьте включить необходимые заголовки Catch2 и соответствующим образом настроить конфигурацию Catch2.