我正在使用UnitTest ++来允许我为某些C++代码(应该在Linux或Mac OS X上构建)创建单元测试.我有这样的目录结构:
src
- Foo.cpp
- Bar.cpp
test
- FooTest.cpp
- BarTest.cpp
- Main.cpp
- Makefile
UnitTest++
- libUnitTest++.a
Run Code Online (Sandbox Code Playgroud)
这个Makefile(改编自UnitTest ++ Makefile)可以很好地工作(使用GNU make):
test = TestFooAndBar
src = ../src/Foo.cpp \
../src/Bar.cpp
test_src = Main.cpp \
FooTest.cpp \
BarTest.cpp
lib = ../UnitTest++/libUnitTest++.a
objects = $(patsubst %.cpp,%.o,$(src))
test_objects = $(patsubst %.cpp,%.o,$(test_src))
.PHONY: all
all: $(test)
@echo Running unit tests...
@./$(test)
$(test): $(lib) $(test_objects) $(objects)
@echo Linking $(test)...
@$(CXX) $(LDFLAGS) -o $(test) $(test_objects) $(objects) $(lib)
.PHONY: clean
clean:
-@$(RM) -f $(objects) …Run Code Online (Sandbox Code Playgroud)