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

How to run tests that depends on the source code (.cpp files from src) which has to be built as executable library? #347

Open
duckladydinh opened this issue Oct 2, 2019 · 3 comments

Comments

@duckladydinh
Copy link
Contributor

Hallo,

I have a problem when trying to run a test for my maven nar-plugin-project. Since the project must be built as executable library, my test cannot link to it, and if I tried to build shared (or static) together with executable, it would result in compiler error (use multithreading). Since this case is very hard to reproduce, could someone give me a hint how you usually wrote tests for an executable project?

Thank you.

@bchiodo
Copy link
Contributor

bchiodo commented Dec 19, 2019

If I am understanding you correctly, you want to run tests (I assume something like googletest) against an executable module?

It is my understanding that while an executable can have unit tests, those unit tests won't have the object files compiled from the source of the executable module itself. You will need to create a second library module that includes the object files you want to test -- but then of course you can test those object files in the library build, making executable unit tests largely redundant.

Of course, other types of tests outside of nar, like integration tests, are always possible.

@sdedwards
Copy link

The way I usually do it is to add the executable source code as an additional test source directory using the Build Helper Maven Plugin. This means of course that the test-compile phase will rebuild everything but has the advantage of you being able to change compile options for the unit tests, e.g. add profiling options.
One thing to watch out for with this approach is you might up with two main functions (the one you have defined for your executable and one defined by your unit test framework). So you either have to use #ifndef to exclude the executable main function if compiling in the test phase, or better, arrange your source in sub-directories so that you can easy exclude the main file from the test compilation.

@bchiodo
Copy link
Contributor

bchiodo commented Dec 19, 2019

The way I usually do it is to add the executable source code as an additional test source directory using the Build Helper Maven Plugin. This means of course that the test-compile phase will rebuild everything but has the advantage of you being able to change compile options for the unit tests, e.g. add profiling options.
One thing to watch out for with this approach is you might up with two main functions (the one you have defined for your executable and one defined by your unit test framework). So you either have to use #ifndef to exclude the executable main function if compiling in the test phase, or better, arrange your source in sub-directories so that you can easy exclude the main file from the test compilation.

Wouldn't the use of cpp.testOptions and/or linker.testOptions work for that? Or is the problem that you want to exclude items in the test compile defined in cpp.options?

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

3 participants