Questions tagged [catch2]

Catch is a unit testing framework for C++

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.

153 questions
13
votes
2 answers

Running unit tests excluding a specific tag in Catch2

Can I run test cases based on "not matching" a particular tag in Catch2? TEST_CASE("Check the data validity","[Working]"){ REQUIRE(true); } TEST_CASE("Check the input","[InProgress]"){ REQUIRE(true); } TEST_CASE("Validate the…
Wander3r
  • 1,801
  • 17
  • 27
9
votes
2 answers

What is the correct way to compile multiple test sources with Catch2?

I have the following project structure: test_main.cc #define CATCH_CONFIG_MAIN #include "catch2.hpp" test1.cc #include "catch2.hpp" #include "test_utils.hpp" TEST_CASE("test1", "[test1]") { REQUIRE(1 == 1); } test2.cc #include…
Collin
  • 1,777
  • 4
  • 26
  • 42
8
votes
1 answer

How to avoid update checks with CMake FetchContent?

all. I decided to use the new cmake macro to download external dependencies. I took the sample code from the documentation for the Catch2 library. include(FetchContent) FetchContent_Declare( Catch2 GIT_REPOSITORY…
e.proydakov
  • 544
  • 6
  • 18
8
votes
0 answers

How to access custom command line options from within a Catch2 test case?

I added my own command line option using a custom main file†: †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…
Sparkler
  • 2,581
  • 1
  • 22
  • 41
7
votes
2 answers

Catch2 installation on ubuntu 20.04 #include

I am trying to install Catch2 on ubuntu 20.04. Used instruction from here. This is what i do: $ git clone https://github.com/catchorg/Catch2.git $ cd Catch2 $ cmake -Bbuild -H. -DBUILD_TESTING=OFF $ sudo cmake --build build/ --target install Than…
c0nn3ct
  • 155
  • 1
  • 1
  • 7
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
7
votes
0 answers

Ctest does not find catch2 tests with subdirectory

I don't seem to get my CTest project to recognize my Catch2 tests. The test project itself builds fine and I manage to run the tests using the executable created by it. However when running ctest -V The output I keep getting…
Mike
  • 3,775
  • 8
  • 39
  • 79
6
votes
1 answer

How do you add separate test files with Catch2 and CMake?

In the documentation, they only compile a single file test.cpp which presumably contains all the tests. I want to separate my individual tests from the file that contains #define CATCH_CONFIG_MAIN, like so. If I have a file test.cpp which contains…
W. Albuquerque
  • 315
  • 2
  • 12
6
votes
2 answers

Can't use overloaded comparison operator with Catch test

I have a simple unit-test using Catch 2.11.1: #define CATCH_CONFIG_MAIN #include "catch.hpp" #include #include namespace A::B { namespace C { struct S { }; } using type = std::pair
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
6
votes
1 answer

How to get a relative path for CMake unit tests?

I have a project built with CMake that uses Catch2 for unit tests. Some of the unit tests exercise code that loads data from a file like this: std::string resource_dir = "TEST_CWD/resources/"; std::ifstream infile{resource_dir + "datafile.txt"} The…
Adam
  • 16,808
  • 7
  • 52
  • 98
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
1 answer

How do I integrate Catch2 with the QT Event Loop?

Some parts of QT rely on the event loop being up and running (or at least generate warnings otherwise). How do you integrate Catch2 tests with the QT event loop?
johnnyb
  • 622
  • 5
  • 17
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
4
votes
2 answers

Unable to use redirected std::cout in Catch2

I'm writing test for my class using Catch2 (single header file version catch.hpp). My class output some text into std::cout, and I cannot change this behavior. I need to retrieve output from std::cout for comparison. To do this, I redirect std::cout…
pklgn
  • 43
  • 1
  • 3
4
votes
2 answers

How to write GUI tests for wxWidgets

I would like to create a test suite for my C++ wxWidgets app and am having trouble with figuring out how to test GUI components. The article in the docs about how to write unit tests is written from the perspective of augmenting the existing unit…
DHamrick
  • 8,338
  • 9
  • 45
  • 62
1
2 3
10 11