在Visual Studio(TDD)中构建和运行C ++单元测试

时间:2021-06-29 15:39:10

I have a large project for which I am attempting to use TDD. I am using Tut as my test framework, which has its flaws but is sufficient for what I need.

我有一个大型项目,我试图使用TDD。我使用Tut作为我的测试框架,它有它的缺点但足以满足我的需要。

I need to exploit link time test seams, each test must be in its own executable. The project for this executable then launches itself as a post build step.

我需要利用链接时间测试接缝,每个测试必须在其自己的可执行文件中。然后,此可执行文件的项目将作为后期构建步骤启动。

Unfortunately, this means that my Visual Studio Sln is filling up with tests, which is not scalable and also hides the actual important projects.

不幸的是,这意味着我的Visual Studio Sln正在填充测试,这些测试不可扩展,并且还隐藏了实际的重要项目。

Does anyone know of a better way of running these tests? Is it possible to hide projects from a build and yet still have them build?

有谁知道运行这些测试的更好方法?是否可以从构建中隐藏项目但仍然可以构建它们?

3 个解决方案

#1


4  

"Is it possible to hide projects from a build and yet still have them build?"

“是否有可能从构建中隐藏项目,但仍然可以构建它们?”

You can make separate solution for test cases. Then you can set up post build step of your main projects. This post-build should build tests-projects via separate solution and run them. Building test-projects should be done via command line (see e.g. here for details).

您可以为测试用例制作单独的解决方案。然后,您可以设置主项目的后期构建步骤。这个后期构建应该通过单独的解决方案构建测试项目并运行它们。构建测试项目应该通过命令行完成(详见此处)。

If you need to link your unit tests with some lib-s from main projects, you can use

如果您需要将单元测试与主项目中的某些lib-s链接,则可以使用

#pragma comment(lib, "libname")

See this MSDN page for details.

有关详细信息,请参阅此MSDN页面。

#2


0  

You could try to group your tests into folders (are they called filters?) within your solution in Solution Explorer. This would separate tests from your other projects.

您可以尝试在解决方案资源管理器中的解决方案中将测试分组到文件夹中(它们称为过滤器吗?)。这会将测试与您的其他项目分开。

But, otherwise, could you avoid using link time test seams (Yes, I know, its probably too late to suggest this) and use polymorphism to provide your run-time variation and have less test projects?

但是,否则,您是否可以避免使用链接时间测试接缝(是的,我知道,这可能为时已晚),并使用多态来提供运行时变化并减少测试项目?

#3


0  

Don't know if you're still looking for a solution. But here's an idea:

不知道你是否还在寻找解决方案。但这是一个想法:

You can keep all your tests in one library and write an application that spawns itself and executes each test. This way you end up with one executable (and hence one project) for a suite and each test will be like a separate executable.

您可以将所有测试保存在一个库中,并编写一个自行生成并执行每个测试的应用程序。这样,您最终会得到一个可执行文件(因此一个项目),每个测试将像一个单独的可执行文件。

This is in-fact the mechanism used in CUnitWin32. You might even be able to wrap your tests in that framework.

这实际上是CUnitWin32中使用的机制。您甚至可以将测试包装在该框架中。

#1


4  

"Is it possible to hide projects from a build and yet still have them build?"

“是否有可能从构建中隐藏项目,但仍然可以构建它们?”

You can make separate solution for test cases. Then you can set up post build step of your main projects. This post-build should build tests-projects via separate solution and run them. Building test-projects should be done via command line (see e.g. here for details).

您可以为测试用例制作单独的解决方案。然后,您可以设置主项目的后期构建步骤。这个后期构建应该通过单独的解决方案构建测试项目并运行它们。构建测试项目应该通过命令行完成(详见此处)。

If you need to link your unit tests with some lib-s from main projects, you can use

如果您需要将单元测试与主项目中的某些lib-s链接,则可以使用

#pragma comment(lib, "libname")

See this MSDN page for details.

有关详细信息,请参阅此MSDN页面。

#2


0  

You could try to group your tests into folders (are they called filters?) within your solution in Solution Explorer. This would separate tests from your other projects.

您可以尝试在解决方案资源管理器中的解决方案中将测试分组到文件夹中(它们称为过滤器吗?)。这会将测试与您的其他项目分开。

But, otherwise, could you avoid using link time test seams (Yes, I know, its probably too late to suggest this) and use polymorphism to provide your run-time variation and have less test projects?

但是,否则,您是否可以避免使用链接时间测试接缝(是的,我知道,这可能为时已晚),并使用多态来提供运行时变化并减少测试项目?

#3


0  

Don't know if you're still looking for a solution. But here's an idea:

不知道你是否还在寻找解决方案。但这是一个想法:

You can keep all your tests in one library and write an application that spawns itself and executes each test. This way you end up with one executable (and hence one project) for a suite and each test will be like a separate executable.

您可以将所有测试保存在一个库中,并编写一个自行生成并执行每个测试的应用程序。这样,您最终会得到一个可执行文件(因此一个项目),每个测试将像一个单独的可执行文件。

This is in-fact the mechanism used in CUnitWin32. You might even be able to wrap your tests in that framework.

这实际上是CUnitWin32中使用的机制。您甚至可以将测试包装在该框架中。