Questions tagged [catch-unit-test]

Catch is a unit testing framework for C++

Catch2 is a unit testing framework for C++11 and later. Catch2 offers a BDD syntax, fixtures, Matchers and dynamic data-generation, and a simplified syntax using overloaded assertions. Catch2 is designed for simplicity – it is released as a single include file and allows the use of natural C++ expressions inside assertions.

Catch2 is hosted on GitHub, available at catch-lib.net, and is openly discussed in a Google group.

93 questions
29
votes
4 answers

How to use floating point tolerances in the Catch framework?

I'm using the Catch test framework. In the introductory blog post the author mentions the following feature: Floating point tolerances supported in an easy to use way I couldn't find any documentation on how to do this. How is this done in…
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
25
votes
2 answers

Catch lib (unit testing) and CTest (CMake) integration

I'm looking for successful example of Catch CatchLib integration with CMake test (Ctest) . as I understand this is additional cmake script which has to parse application ouput? Did someone already written this? probably shared this?…
amigo421
  • 2,429
  • 4
  • 26
  • 55
15
votes
2 answers

How to use CMake with Catch2?

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 | +--…
fronthem
  • 4,011
  • 8
  • 34
  • 55
12
votes
1 answer

Test that two std::vectors are equal using CATCH C++ unit test framework

I an new to using CATCH, and I wondering how one would go about testing whether two std::vectors are equal. My very naive attempt is this: #define CATCH_CONFIG_MAIN #include "catch.hpp" #include TEST_CASE( "are vectors equal",…
Akavall
  • 82,592
  • 51
  • 207
  • 251
11
votes
2 answers

Unit Testing with Catch C++ is interfering with my main()

I'm new to unit testing and decided to use the Catch framework for c++ because it seemed easy to integrate with its one header file. However, I have a multifile binary search tree program (files are: main.cpp, Tree.h, Tree.hxx, TreeUnitTests.cpp,…
nhershy
  • 643
  • 1
  • 6
  • 20
10
votes
0 answers

Mocking for Catch - C++ testing

Could you, please, suggest me a mocking system to use with Catch for testing C++? I'd really like one that does NOT require me to define any new classes (like I saw Google Mock does) :), something like Mockito for Java. I've checked Mockitopp and…
PhantomR
  • 605
  • 4
  • 16
9
votes
3 answers

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

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…
miqelm
  • 354
  • 4
  • 13
8
votes
1 answer

How do I make my cmake targets for catch2 testing and project running more extendable and sensible?

After a lot of hard work and research, I've managed to make multiple cmake targets to separate running my program from running tests on the code. But I don't like what I've made because I see redundancy in the CMakeList.txt files. Currently I have…
DavidTriphon
  • 159
  • 1
  • 7
8
votes
1 answer

Compile error "error: expected ';' at end of declaration list" using Catch testing framework in C++

I'm trying to learn C++ by implementing some simple algorithms in it. In order to test these algorithms, I'd like to use Catch2. Here is a program I came up for binary search: #define CATCH_CONFIG_MAIN #include "catch.hpp" #include using…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
7
votes
2 answers

How to compare floating point in catch2

I am using Catch v2.13.1 What is the correct way to compare float values. I thought the below would fail, but both pass. REQUIRE(1147332687.7189338 == Approx(1147332688.4281545).margin(0.0001)); REQUIRE(1147332687.7189338 ==…
Inquisitor
  • 73
  • 1
  • 4
6
votes
1 answer

Best practices for Unit testing with Catch2 in Visual Studio

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…
DavidZi
  • 305
  • 2
  • 7
6
votes
3 answers

Use C++ catch framework to verify assert statement

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…
Zack
  • 6,232
  • 8
  • 38
  • 68
5
votes
0 answers

How can I make catch2 run tests that reside in a static library?

My usual workflow with catch2 is to have a single console application that contains all the test cases and the tests 'runner'. For example: file1.cpp, file2.cpp contains tests: #include TEST_CASE("test", "[test]") { …
Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
5
votes
2 answers

How to improve branch coverage in C++

I have a fairly large test suite for a C++ library with close to 100% line coverage, but only 55.3% branch coverage. Skimming through the results of lcov, it seems as if most of the missed branches can be explained by C++'s many ways to throw…
Niels Lohmann
  • 2,054
  • 1
  • 24
  • 49
5
votes
2 answers

Compare Vector of Doubles Using Catch

I'm using the Catch unit testing framework, and I'd like to compare a vector of doubles. This other answer suggests using Approx to compare floating point/double values, but this doesn't work for a vector of them. Is there any convenient way of…
1
2 3 4 5 6 7