In principle, unit testing sounds simple. Unit tests are used to isolate and validate small parts of a program are working. In practice, I don't really know how it's done.
As a first attempt, I tried to build a simple program with tests using google test, https://code.google.com/p/googletest/. After downloading and unzipping the library, I moved it into a folder -- SomeDirectory/libs/gtest-1.6.0. I then copied these files into a separate directory.
- Makefile -- this is used by make to build both the test program and my program.
- sample1.h, sample1.cc -- These define some functions that are linked into the test program and into my program.
- sample_unittest.cc -- This defines test macros that are run in the test program.
If you put these files into a directory, you are almost ready to run the test. You will have to update the paths in the Makefile.
- GTEST_DIR = SomeDirectory/libs/gtest-1.6.0
- USER_DIR = ./
I wanted to add a real program with this test suite, so I wrote this:
To build the program I updated the Makefile to build "my_program" like this:
TESTS = sample1_unittest my_program
and then I added this to the end of the Makefile:
my_program : sample1.o main.o
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@
After running make, I had two programs. "sample1_unittest" was the original test program and "my_program" was the 'real' program that I was testing.
No comments:
Post a Comment