Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests alongside code with multiple executables #199

Closed
Omegastick opened this issue Mar 18, 2019 · 2 comments
Closed

Tests alongside code with multiple executables #199

Omegastick opened this issue Mar 18, 2019 · 2 comments

Comments

@Omegastick
Copy link

Description

I would like to write my tests alongside my code, but I'm having trouble trying to figure out where to put the doctest implementation.

I have a doctest.cpp file that looks like this:

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

A main.cpp that looks like this:

#include "thing.h"

int main(int argc, const char *argv[]) {
    do_thing();
}

thing.h is self-explanatory, but thing.cpp looks like this:

do_thing() {}

TEST_CASE("Test thing") {
    CHECK(1 == 1);
}

CMake is used to create two executables, a Tests executable and a Main executable.

If I don't include doctest.cpp in my target sources for Main, I get undefined reference errors because it can't find a definition for all the testing stuff in doctest.

However, if I do include it I get errors because there are multiple main() functions in one target.

Ideally, I would like to be able to wrap my tests in an #ifdef that checks for whether or not an implementation is available, but I'm not sure if that's possible.

What am I supposed to do here?

@onqtam
Copy link
Member

onqtam commented Mar 18, 2019

DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN implements the test runner and also defines a main() function.

DOCTEST_CONFIG_IMPLEMENT implements ONLY the test runner.

If you define your own main() then you should use DOCTEST_CONFIG_IMPLEMENT - have a look at the relevant docs.

You will need the test runner implemented in your main executable (that means doctest.cpp) since you are writing your tests alongside your production code.

You can also define DOCTEST_CONFIG_DISABLE when building the main executable so tests are written in the production code but aren't compiled (you will still need doctest.cpp so it all links). This way you won't need to #ifdef the tests.

You could also entirely remove the test executable and use the main executable for running the tests - docs.

Let me know if this helps!

@Omegastick
Copy link
Author

Thanks very much for the help, I've gone for the first option of including it in my own main() function.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants