如何使用Google Test测试EXE? [英] How to test an EXE with Google Test?
问题描述
我在Visual Studio中有一个C ++项目,并添加了另一个专门用于测试的项目。这两个项目都是EXE(控制台应用程序)。那么我如何在第二个项目中使用第一个项目呢?
I have a C++ project in Visual Studio, and have added another project exclusively for testing. Both of these projects are EXEs (console apps). So how do I use the first project inside the second?
只是要澄清一下,如果第一个项目是一个图书馆,那么这个问题在某种程度上是不言而喻的。只是包含在第二个项目中,但是作为EXE,这就是问题所在。
Just to clarify, the question here would be somewhat self-evident if the first project was a library that one could simply include in the second project but, being an EXE, this is where the problem lies.
推荐答案
根据您的评论,您有一个C ++控制台应用程序( MyApp)
,为此您开发了一些特定于应用程序的类,您希望在
Visual Studio中使用googletest对其进行单元测试。
Per your comments, you have a C++ console application (MyApp)
for which you have developed some application-specific classes that you want to unit-test with googletest in
Visual Studio. How?
正如您所说,如果要对库进行单元测试,显然
是可行的。您将:
As you say, if you wanted to unit-test a library the way to do it would be obvious. You would:
- 1)创建一个项目以创建单元测试应用程序(
UnitTest
)。 - 2)配置include-search目录,以便编译器可以找到库的头文件。
- 3)配置library-search目录,以便
- 4)将库本身添加到链接器输入中。
- 5)使
UnitTest
项目依赖于库项目,因此构建UnitTest
可确保MyApp
启动至今。 - 6)为每个googletest文档的
UnitTest
应用编写代码。
- 1) Create a project to create a unit-testing application (
UnitTest
). - 2) Configure the include-search directories so that the compiler can find the library's headers.
- 3) Configure the library-search directories so that the linker can find the library itself.
- 4) Add the library itself to the linker inputs.
- 5) Make the
UnitTest
project dependent on the library project, so that buildingUnitTest
ensuresMyApp
is up-to-date. - 6) Code the
UnitTest
app per googletest docs.
但是由于要进行单元测试的类特定于 MyApp
,因此您没有任何
库。
But since the classes you want to unit-test are specific to MyApp
, you don't have any
library.
钻探警长对此的回答是:您没有包含要进行单元测试的类的库吗?所以做一个!
A drill-sergeant answer to that is: You don't have a library containing the classes you want to unit-test? So make one!
那样,您使用3个项目:-
That way you use 3 projects:-
-
MyAppLib
,生成包含要进行单元测试的所有功能的库。 -
MyApp
,生成与当前相同的可执行文件,但链接MyAppLib
-
UnitTest
,生成对MyAppLib
进行单元测试的可执行文件,还链接了MyAppLib
MyAppLib
, generating library that contains all the functionality you want to unit-test.MyApp
, generating the same executable as at present, but linkingMyAppLib
UnitTest
, generating an executable that unit-testsMyAppLib
, also linkingMyAppLib
但是,如果您不喜欢中士的答案,您可以解决它。
However if you don't like the drill-sergeant answer you can work around it.
从通常的构建系统角度(Visual Studio中设计的一种)来看,
MyApp
项目的重要输出是构建目标- .exe
。
生成的 .obj
文件只是中间副产品。 VS不为您提供支持
来将这些副产品视为从属项目的自动链接器输入,并且如果从属项目也是相同项目的 .exe
排序-根据您的情况-这样的自动链接无论如何都将是不可能的,因为将对主入口点进行多次定义。
From the usual build-system point of view (the one designed into Visual Studio),
the important output of the MyApp
project is the build-target - the .exe
.
The .obj
files generated are just intermediate by-products. VS offers you no support
for treating these by-products as automatic linker inputs of a dependent project, and if a dependent project was also an .exe
of the same sort - as it is your case - then such automatic linkage would be impossible anyhow because the main entry point would be multiply defined.
但是从单元测试的角度来看反之亦然。 .exe
没什么意义,而 .obj
文件的(部分)全部或部分包含以下内容的实现:您要进行单元测试的类。在课本中,在 foo.h
中定义了类 foo
并在中实现的类foo.cpp
,在 UnitTest
的链接中需要目标文件 foo.obj
。
But from the unit-testing point of view it's the other way round. The .exe
is of no interest, whereas (some of) the .obj
files wholly or partly contain the implementations of the classes you want to unit test. In the text-book case where class foo
is defined in foo.h
and implemented in foo.cpp
, the object file foo.obj
is needed in the linkage of UnitTest
.
为简单起见,假设 MyApp
仅使用一个特定于应用程序的类 foo
,
在 foo.h
中定义,并在 foo.cpp
中实现。然后,您有两个选择来构建 UnitTest
。
For simplicity, assume that MyApp
employs just one application-specific class foo
,
defined in foo.h
and implemented in foo.cpp
. Then you have two options for building UnitTest
.
-
a)您可以将
foo.cpp
添加到UnitTest
的源文件中。当然,不要复制。只需从MyApp
的源文件夹中添加现有项目。然后,您就完成了,但是此
课程的缺点是foo.cpp
会在
中的内进行不当编辑。 UnitTest
项目。
a) You can add
foo.cpp
to the source files ofUnitTest
. Don't copy it of course. Just Add an existing item from the source folder ofMyApp
. Then you're done, but this course has the downside thatfoo.cpp
is exposed to untoward editing within theUnitTest
project.
b)您可以只使用 foo.obj
就像链接 UnitTest
所需的静态库一样,并按照上面的步骤1)-6)进行操作。这特别意味着在步骤3)中,使用包含 \path\的库搜索目录配置了
(相对或绝对形式)。 UnitTest
的{Debug | Release}构建\to\MyApp\ {Debug | Release}
b) You can treat foo.obj
just like a static library required for the linkage of UnitTest
and follow steps 1) - 6) above. This means in particular at step 3) that the {Debug|Release} build of UnitTest
is configured with library-search directories that include \path\to\MyApp\{Debug|Release}
(either in relative or absolute form).
实际上,对于选项b),很可能来自 MyApp
的一个 .obj
文件您将不得不在 UnitTest
中进行链接,并且其数量很可能会随着时间的增长而增加。维持 UnitTest
的正确链接可能会变得很麻烦,您可能会得出这样的结论:操练中士毕竟是正确的。
In reality, for option b), there's very likely more than one .obj
file from MyApp
that you will have to link in UnitTest
, and quite likely that their number will grow with time. Maintaining the right linkage of UnitTest
could become a chore, and you might come to the conclusion that the drill-sergeant was right after all.
这篇关于如何使用Google Test测试EXE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!