catch-unit-test

Best practices for Unit testing with Catch2 in Visual Studio

丶灬走出姿态 提交于 2020-12-01 12:18:35
问题 I'm new to unit testing in C++ and want to get some advice on this. I use Visual Studio 2019 for development and I chose Catch2 as my testing library, I also got the Test Adapter for Catch2 installed. I read docs for both Catch2 and Test Adapter for Catch2 on GitHub, but I still cannot figure out a proper way to use unit test in Visual Studio. Let's assume that I already have a project with some classes in it and I want to test those classes. Should I put files with test code in the same

“Catch” unit testing framework - REQUIRE_THROWS_AS

这一生的挚爱 提交于 2020-01-04 07:52:43
问题 I started to use "Catch" unit testing framework and so far it's really great. I have used VS built in unit testing framwork with great pain . one thing I have noticed that the macro REQUIRE_THROWS_AS does not behave as one would expect from the docs: REQUIRE_THROWS_AS( expression, exception type ) and CHECK_THROWS_AS( expression, exception type ) Expects that an exception of the specified type is thrown during evaluation of the expression. when I try to write TEST_CASE("some test") { SECTION(

How to use CMake with Catch2?

霸气de小男生 提交于 2019-12-18 05:04:06
问题 From Catch2's example, I tried to run this example with cmake where structure of my project is like this: /factorial +-- CMakeLists.txt +-- /bin +-- /include | +-- catch.hpp | +-- fact.hpp +-- /src | +-- CMakeLists.txt | +-- fact.cpp +-- /test +-- CMakeLists.txt +-- test_fact.cpp fact.cpp : unsigned int factorial( unsigned int number ) { return number <= 1 ? number : factorial(number-1)*number; } fact.hpp : #ifndef FACT_H #define FACT_H unsigned int factorial(unsigned int); #endif test_fact

How to use CMake with Catch2?

China☆狼群 提交于 2019-12-18 05:03:05
问题 From Catch2's example, I tried to run this example with cmake where structure of my project is like this: /factorial +-- CMakeLists.txt +-- /bin +-- /include | +-- catch.hpp | +-- fact.hpp +-- /src | +-- CMakeLists.txt | +-- fact.cpp +-- /test +-- CMakeLists.txt +-- test_fact.cpp fact.cpp : unsigned int factorial( unsigned int number ) { return number <= 1 ? number : factorial(number-1)*number; } fact.hpp : #ifndef FACT_H #define FACT_H unsigned int factorial(unsigned int); #endif test_fact

Test C++ template class using Catch framework

南笙酒味 提交于 2019-12-13 07:33:54
问题 I'm looking for a good way to use Catch to test a templated class. I have something that almost works: #define RUN_ALL(fn, params) \ fn<uint8_t, bool>(params); \ fn<uint8_t, char>(params); \ fn<uint16_t, bool>(params); \ fn<uint16_t, char>(params); \ fn<uint32_t, bool>(params); \ fn<uint32_t, char>(params); \ fn<uint64_t, bool>(params); \ fn<uint64_t, char>(params); template<typename A, typename B> void test_number_one() { REQUIRE(...) } TEST_CASE("Foo::Foo() works nicely", "[SmallGraph]") {

Is there a cross-compiler method to disable coverage flags for test executables using CMake?

两盒软妹~` 提交于 2019-12-13 03:39:49
问题 I am creating a generic C++, CMake, and Catch project template that I plan on using in the future, and want to enable code coverage reports for it. To do so, I decided to add the following CMake module into my list of Modules: CodeCoverage.cmake. My usage for this essentially boils down to the following snippet: if (ENABLE_COVERAGE AND NOT CMAKE_CONFIGURATION_TYPES) if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug") message(STATUS "Coverage in a non-debug build may lead to misleading results! Setting

CmakeList - Not able to create correct CMakelist for C++ Project subfolders

こ雲淡風輕ζ 提交于 2019-12-12 02:26:00
问题 I have a C++ Project folder which contains two folders. a C++ src folder with multiple sub-folders and CmakeList.txt file. The src folder contains main.cpp file. **Cmakelist.txt src folder** project (TestProject) # Include all global directories and the current directory include_directories(${GLOBAL_INCLUDES} ".") link_directories(${GLOBAL_LIB_DIRS}) file(GLOB_RECURSE GLOBAL_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}" "*.cpp") add_executable(${PROJECT_NAME} ${GLOBAL_SOURCES}) target_link_libraries(

Catch test case order

狂风中的少年 提交于 2019-12-11 08:55:42
问题 Can I guarantee the order of execution with multiple TEST_CASE s with Catch ? I am testing some code using LLVM, and they have some despicable global state that I need to explicitly initialize. Right now I have one test case that's like this: TEST_CASE("", "") { // Initialize really shitty LLVM global variables. llvm::InitializeAllTargets(); llvm::InitializeAllTargetMCs(); llvm::InitializeAllAsmPrinters(); llvm::InitializeNativeTarget(); llvm::InitializeAllAsmParsers(); // Some per-test setup

Use C++ catch framework to verify assert statement

与世无争的帅哥 提交于 2019-12-10 13:57:00
问题 Is it possible to use the C++ CATCH framework to verify that an assert statement correctly identifies an invalid precondition? // Source code void loadDataFile(FILE* input) { assert(input != NULL); ... } // Test code TEST_CASE("loadDataFile asserts out when passed NULL", "[loadDataFile]") { loadDataFile(NULL) // Now what do I look for? } 回答1: Assuming that the first section of your example is the source code under test and the second part is the unit test, then you'll need to make a choice in

Test floating point std::vector with C++ Catch

China☆狼群 提交于 2019-12-04 20:26:27
问题 Is there any possibility in Catch C++ Unit test framework to compare std::vectors that are floating point type based? I know that I can compare size of both containers and each element (using Approx) but this is messy. Comparison of integral types vector works properly. Now, I must use such construction REQUIRE(computed.size() == expected.size()); for (size_t i = 0; i < computed.size(); ++i) REQUIRE(computed[i] == Approx(expected[i])); But I would like to use one liner (it works for integral