1 year ago

#283780

test-img

Nathan Furnal

How to properly setup Catch2 tests in a Makefile?

I'm trying to setup tests with Catch2 and setup a testing rule in a Makefile, it's a pre-requisite, I can't use CMake. I'm not sure how to go about that.

For example, I have something along those lines:

src
|
|--------- model
              |
              | 
              |-------- classA.hpp
              |-------- classA.cpp
              |-------- classB.hpp
              |-------- classB.cpp

tests/
|
|-------- catch.hpp
|-------- test-main.cpp
|-------- test_classA.cpp
|-------- test_classB.cpp

I'd like to have Makefile rules for a test target to run all the tests. Reading a practical example from the Catch docs was helpful to setup the files but I'm a bit lost about what to afterwards. test-main uses an hyphen an not an underscore, to avoid the wildcard later (since it's compiled only once).

For now, my (simplified) Makefile is along those lines:

testdir := tests
test_sources := $(wildcard $(testdir)/test_*.cpp)

Then, this is where the example confuses me, it seems I need to compile test-main.cpp, my classes in the model directory AND add the test sources files during the creation of the test executable.

This is what is said in the above example in Catch's README.

[See this example from the docs](https://imgur.com/a/QkLd5DH).

I just don't know how to create Makefile rules that will allow me to do that and I can't seem to find an example online with many test files.

Thanks for the help.

c++

unit-testing

testing

makefile

catch2

0 Answers

Your Answer

Accepted video resources