[英]How do I compile and link unit tests?
I have recently started trying to use TDD in one of my C++ projects. 我最近开始尝试在我的一个C ++项目中使用TDD。 I am very new at this, and I have some very basic questions regarding to the way unit tests are compiled and used.
我对此非常陌生,而且我对编译和使用单元测试的方式有一些非常基本的问题。 I am using the Boost.Test library on Linux.
我在Linux上使用Boost.Test库。
Is it common to compile a single large test program, containing all test suites, for all your units? 为所有单元编译包含所有测试套件的单个大型测试程序是否常见? How about splitting the tests into many smaller independent test programs?
如何将测试分成许多较小的独立测试程序?
How is linking handled (with make
)? 如何处理链接(使用
make
)? Every test program object must be linked with the object file(s) from my source program that contains whatever it is that's being tested. 每个测试程序对象都必须与源程序中的目标文件链接,该目标文件包含正在测试的内容。 Is there a way to handle this automatically?
有没有办法自动处理? To be more specific, is there anyway to write a
Makefile
so that make
automatically determines what object files must be linked together to generate a certain unit test program? 更具体一点,是否有编写
Makefile
以便make
自动确定哪些目标文件必须链接在一起以生成某个单元测试程序?
Update : My code is organized in many .cpp/.h files, and is currently monolithic (no libraries). 更新 :我的代码组织在许多.cpp / .h文件中,目前是单片(没有库)。 The unit tests are in a separate directory, usually in 1-to-1 relationship with the .cpp files from my source tree.
单元测试位于一个单独的目录中,通常与源树中的.cpp文件保持一对一的关系。
Update 2 : Hoping that this will make my question less broad, here is an excerpt from the Makefile I am using: 更新2 :希望这会使我的问题不那么广泛,这里是我正在使用的Makefile的摘录:
$(TSTDIR)%.$(TEST): $(OBJDIR)%.$(TEST).$(OBJEXT) $(OBJDIR)%.$(OBJEXT)
@mkdir -p $(@D)
@$(CXX) -o $@ $^ $(TSTLIBS)
$(OBJDIR)%.$(OBJEXT): $(SRCDIR)%.$(SRCEXT) $(DEPDIR)%.$(DEPEXT)
@mkdir -p $(@D)
@$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ $<
$(TEST)
is just a marker I am using to differentiate between my unit tests and other files. $(TEST)
只是我用来区分单元测试和其他文件的标记。 Note that I am currently linking all test programs with the object file that bears the same name. 请注意,我目前正在将所有测试程序与具有相同名称的目标文件链接起来。 However this will break if symbols from another object file are also needed.
但是,如果还需要来自另一个目标文件的符号,这将会中断。
Update 3 : This is an example of what I am talking about in the above paragraph. 更新3 :这是我在上一段中讨论的一个例子。
MyOtherClass.h: MyOtherClass.h:
class MyOtherClass
{
public:
int Foo();
}
MyOtherClass.cpp: MyOtherClass.cpp:
#include "MyOtherClass.h"
int MyOtherClass::Foo()
{
return 0;
}
MyClass.h: MyClass.h:
#include "MyOtherClass.h"
class MyClass
{
public:
int Foo();
private:
MyOtherClass moc_;
}
MyClass.cpp: MyClass.cpp:
#include "MyClass.h"
int MyClass::Foo()
{
return moc_.Foo();
}
TestMyClass.cpp would test if MyClass::Foo
returns 0. Using my current Makefile, this will not compile, as the test program needs to be linked with both MyClass.o and MyOtherClass.o. TestMyClass.cpp将测试
MyClass::Foo
返回0.使用我当前的Makefile,这将无法编译,因为测试程序需要与MyClass.o和MyOtherClass.o链接。
I found this excellent answer that was given to a related question. 我找到了一个相关问题的优秀答案 。
It's possible to solve the linking problem by using static or dynamic libraries. 通过使用静态或动态库可以解决链接问题。 If the object files being tested are compiled into libraries, then when linking the unit test program, it will pull whatever dependencies it needs from the library.
如果正在测试的目标文件被编译到库中,那么当链接单元测试程序时,它将从库中提取它所需的任何依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.