最佳实践使用boost测试和测试,不应该编译 [英] Best practice using boost test and tests that should not compile

查看:187
本文介绍了最佳实践使用boost测试和测试,不应该编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个合理的方法来测试基于C ++模板的软件,其中我想检查模板参数。如果参数不符合某些标准,我希望编译器发出错误。到目前为止这么好...

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...

现在我想测试无效的模板参数是否被编译器撤销。我可以设置单个测试场景,并在构建系统(cmake)中尝试编译场景中的某些东西,但这听起来很痛苦。我想我不是第一个试图解决这个问题。大约10年前,我使用了一种方法,其中我使用单个文件进行多个测试,并使用预处理器启用单个测试。但是这不是很优雅。

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.

任何关于如何解决这个问题的建议(最好在我目前的工具集:c ++,cmake,boost-test,bash,python) ?

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

推荐答案

CMake的 try_compile 听起来像一个合理的工作工具。

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

这里的主要问题是 try_compile 会在CMake配置时运行,这不是测试的最佳时机。因此,我建议将测试的编译分成其自己的CMake项目。

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.

然后外部CMake项目将像以前一样构建您的代码,并包含一个自定义构建目标CMake对测试项目。这样,你应该能够将编译时测试包括在CTest套件中。

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.

我实际上没有用CMake实现这样的东西,所以我不能保证将真正按预期工作,更不用说它是最好的方法。但如果我不得不写这样的系统,这可能是我如何开始。

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.

这篇关于最佳实践使用boost测试和测试,不应该编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆