Let's talk about the cxxtest test framework: When we use cxxtest to write test cases, we only need to write one. h file, include the header file of the class or function you want to test in this file, and then write various test functions. And. corresponding to the H file. the CPP file uses the cxxtest script cxxtestgen. pl. h file and one. CPP file, compiled with GCC/g ++, get an executable file, run this file, and then execute the test case you have compiled.
Using Google test as your testing framework is a bit different:
You need to compile a test case file (for example, unittest. CPP), include the header file of the class or function you want to test in this file (of course, # include <gtest/gtest. h>, this is required by Google test), and then write various tests in it. After writing, you need to write a file containing the main () function, such as main. CPP, the content is simple, as follows:
# Include <gtest/gtest. h>
Int main (INT argc, char ** argv)
{
Testing: initgoogletest (& argc, argv );
Return run_all_tests ();
}
A little messy. Let's take a look: there are currently four files: Main. cpp, unittest. cpp, And. H files of the class you need to test and. cpp files.
Then, use gcc/g ++ to compile these files, get an executable file, run the file, and then execute the test case you have compiled.
Note:
(1) After Google test is installed, you can use it to compile a test case. You may find that the test case cannot be run and the prompt "error while loading shared libraries: libgtest. so.0: cannot open shared object file: no such file or directory ", check the installation path of the Google test dynamic library, for example, my rhel5 is installed under/usr/local/lib/by default. At this time, you need to copy the same file under/usr/lib, of course, the simpler method is to create a symbolic link (that is, "shortcut "):
CD/usr/lib/
Ln-S/usr/local/lib/libgtest. so.0.0.0 libgtest. so.0
(2) when compiling the sample that comes with Google test, there are several notes: You must bring the-lgtest option and the-lpthread option (which does not seem to be side-by-side, but if it is not included, several errors will be prompted. For example, the command for compiling sample1 is:
Gcc-O sample0000exec sample1.cc sample0000unittest.cc main. cpp-lgtest-lpthread
Sampledomainexec is the generated executable file name, And sample1.cc is equivalent to the class you need to test above. CPP file, samplew.unittest.cc is equivalent to the unittest. CPP file, Main. CPP is the main. CPP file.
After compilation, execute sampledomainexec and output the following results. The test passes:
[=========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from factorialtest
[Run] factorialtest. Negative
[OK] factorialtest. Negative (0 MS)
[Run] factorialtest. Zero
[OK] factorialtest. zero (0 MS)
[Run] factorialtest. Positive
[OK] factorialtest. Positive (0 MS)
[----------] 3 tests from factorialtest (1 MS total)
[----------] 3 tests from isprimetest
[Run] isprimetest. Negative
[OK] isprimetest. Negative (0 MS)
[Run] isprimetest. trivial
[OK] isprimetest. trivial (0 MS)
[Run] isprimetest. Positive
[OK] isprimetest. Positive (0 MS)
[----------] 3 tests from isprimetest (0 MS total)
[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (3 MS total)
[Passed] 6 tests.
Summary:
Cxxtest uses scripts to generate a test containing main. CPP file, Google test is your own write containing main. CPP file (which is actually a fixed content), in contrast, which is not troublesome. The key is that Google test is "seamless integration" with Google mock. If you want to use Google mock, you 'd better choose Google test (Google mock can also be used together with cxxtest, see the Google mock documentation ).