Best practice using boost test and tests that shou

2019-05-26 16:24发布

问题:

I'm looking for a reasonable approach to test C++ template based software, where I want to check template arguments. If the argument do not fit certain criteria, I want the compiler to issue an error. So far so good...

Now I want to test that invalid template parameters are indeed revoked by the compiler. I could setup a single test scenario and fiddle something within the build system (cmake) that tries to compile the scenario, but this sounds very painful. I think I'm not the first who tries to solve this. Some 10 years ago, I've used an approach, where I used a single file for multiple tests and enabled a single test with the preprocessor. But that's not very elegant either.

Any suggestions on how to solve this (best within my current tools-set: c++, cmake, boost-test, bash, python)?

回答1:

CMake's try_compile sounds like a reasonable tool for the job.

The main issue here is that try_compile will run at CMake configure time, which is not the best time to do testing. I would thus propose separating the compilation of the tests into its own CMake project.

The outer CMake project would then build your code as before and include a custom build target that runs CMake on the test project. This way you should even be able to include the compile-time tests into your CTest suite.

I did not actually implement anything like this with CMake, so I cannot gurarantee it would really work as expected, let alone that it is the best approach. But if I had to write such a system, that's probably how I would start.