Home > other >  Run all catch2 tests in one compile unit without tag definition
Run all catch2 tests in one compile unit without tag definition

Time:01-11

I have the following project structure:

test_main.cc

#define CATCH_CONFIG_MAIN

#include "catch2.hpp"

test1.cc

#include "catch2.hpp"

TEST_CASE("test1", "[test1]") {
  REQUIRE(1 == 1);
}

test2.cc

#include "catch2.hpp"

TEST_CASE("test2", "[test2]") {
  REQUIRE(2 == 2);
}

Now, I can run all tests with e.g. test1 using the command line option test1 at the runtime of the test executable.

I usually tag all test in one file with the same tags, since I use one file to combine all tests for the same topic. Thus, using a file and tags violated the DRY principle.

My question is there a way to run all catch2 tests defined in one file?

CodePudding user response:

I've found in documentation this:

Catch2/command-line.md at devel · catchorg/Catch2 · GitHub

Filenames as tags

-#, --filenames-as-tags

When this option is used then every test is given an additional tag which is formed of the unqualified filename it is found in, with any extension stripped, prefixed with the # character.

So, for example, tests within the file ~\Dev\MyProject\Ferrets.cpp would be tagged [#Ferrets].

Looks like it works, so now just filter test based on that tag.

There is even exact example in documentation

...

-# [#somefile]          Matches all tests from the file 'somefile.cpp'
  •  Tags:  
  • Related