如何运行单元测试?编译器标志?静态库? [英] How do you run your unit tests? Compiler flags? Static libraries?

查看:166
本文介绍了如何运行单元测试?编译器标志?静态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用TDD,我很好奇他人采取什么方法来运行他们的测试。作为参考,我使用google测试框架,但我相信这个问题适用于大多数其他测试框架和C / C ++以外的语言。

I'm just getting started with TDD and am curious as to what approaches others take to run their tests. For reference, I am using the google testing framework, but I believe the question is applicable to most other testing frameworks and to languages other than C/C++.

我的一般方法到目前为止已经做了三件事之一:

My general approach so far has been to do one of three things:


  1. 将大多数应用程序写入静态库,然后创建两个可执行文件。一个可执行文件是应用程序本身,另一个是具有所有测试的测试运行器。两者都链接到静态库。

  1. Write the majority of the application in a static library, then create two executables. One executable is the application itself, while the other is the test runner with all of the tests. Both link to the static library.

将测试代码直接嵌入应用程序本身,并使用编译器标志启用或禁用测试代码。

Embed the testing code directly into the application itself, and enable or disable the testing code using compiler flags. This is probably the best approach I've used so far, but clutters up the code a bit.

将测试代码直接嵌入应用程序本身,并且

Embed the testing code directly into the application itself, and, given certain command-line switches either run the application itself or run the tests embedded in the application.

没有任何一个命令行开关可以运行应用程序本身或运行应用程序中嵌入的测试。这些解决方案特别优雅 ...

None of these solutions are particularly elegant...

如何做?

推荐答案

我倾向于喜欢静态库超过DLL,所以我的大多数C ++代码结束在静态库,如你所发现,容易测试为dll。

I tend to favour static libs over dlls so most of my C++ code ends up in static libs anyway and, as you've found, they're as easy to test as dlls.

对于构建到exe的代码,我有一个单独的测试项目,它只包括被测试的源文件,进入exe或者我建立一个新的静态库包含大多数的exe和测试,以同样的方式,我测试所有的其他静态库。我发现我通常采用'最多的代码在库的方法与新的项目和'将源文件从exe项目到测试项目'的方法,当我复古测试现有的应用程序。

For code that builds into an exe I either have a separate test project which simply includes the source files that are under test and that are usually built into the exe OR I build a new static lib that contains most of the exe and test that in the same way that I test all of my other static libs. I find that I usually take the 'most code in a library' approach with new projects and the 'pull the source files from the exe project into the test project' approach when I'm retro fitting tests to existing applications.

我不喜欢你的选择2和3。管理2的构建配置可能比拥有一个单独的测试项目简单拉入它需要的源,包括所有的测试到exe,因为你的建议3是错误的;)

I don't like your options 2 and 3 at all. Managing the build configurations for 2 is probably harder than having a separate test project that simply pulls in the sources it needs and including all of the tests into the exe as you suggest in 3 is just wrong ;)

这篇关于如何运行单元测试?编译器标志?静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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