- To build and run the new tests, one must pass
-DENABLE_TEST=onto cmake - seeNEW_INSTALL.md. The google test library will then be downloaded and the correct targets will be defined for cmake. - To compile them,
makeas normal from a build directory. - The entire set of tests can be run with
./bin/runTests. The parametersnoPrint,dryandverbosecan be specified as before, but they will apply to all tests which are run. One can also runmake test, which will run the tests throughctest, but the output will be significantly less verbose and therefore less helpful in the case of a failure.
One may often want to run a specific test or set of tests. This is achieved using google test's built-in 'filtering' system. To run only the PolyEval tests, for example, one simply runs
./bin/runTests --gtest_filter='*PolyEval*'
More details on this filtering can be found in Google's documentation here.
The majority of the tests currently residing in src/tests are ported from the old tests which took several parameters. This maps reasonably well onto google test's 'Parameterised tests', more of which can be read about here.
Currently, the parameters to run tests with are written in the tests themselves, and require modification in the code to run with different parameters. Some runtime overriding of these parameters would be possible by placing a function call in the INSTANTIATE_TEST_CASE_P macro, but this has not been done yet.
The google test framework gives us a few of main benefits:
- The presence of 'fixtures', which are classes to contain common set-up and teardown code. These are created by inheriting from
::testing::Testor::testing::TestWithParam<T>, and typically have a name such asGTest_extractDigits. - A rich set of expectation/assertion macros, such as
EXPECT_EQ,EXPECT_GT, and many more which can be read about here. A test will typically contain at least one of these, the result of which determines the success or failure of the test. - A convenient runner which provides utilities such as filtering, repeating, shuffling, and convenient, consistent printouts. This adds significant convenience when only some tests are failing, since a summary as printed at the end specifying which tests failed, with which parameters they failed, etc.
- Create a new file for the tests, let's say called
GTest_foo.cpp. - Add
GTest_foo.cppandGTest_footo the appropriate lists intests/CMakeLists.txt. - In
GTest_foo.cpp, be sure to include both"gtest/gtest.h"and"test_common.h. The latter will give access to the parametershelib_test::noPrint,helib_test::dryandhelib_test::verbose. - Create an anonymous namespace in which to put everything, so that the global namespace for the test runner is not polluted.
- Create a class called
GTest_foo, inheriting from::testing::Testor::testing::TestWithParam<some_type>for non-parameterized or parameterized tests (respectively). Common setup and teardown code/data should be placed in this class. - Create actual test code inside a
TEST_ForTEST_Pfor non-parameterized or parameterized tests (respectively). - If using parameterized tests, create some parameters to run the tests with using the
INSTANTIATE_TEST_CASE_Pmacro.
More details on how to use these google test utilities can be found by the helpful google test documentation:
- Basic overview: https://github.com/google/googletest/
- Introduction to using google test: https://github.com/google/googletest/blob/master/googletest/docs/primer.md
- More detailed documentation: https://github.com/google/googletest/blob/master/googletest/README.md
- Advanced features: https://github.com/google/googletest/blob/master/googletest/docs/advanced.md
It may also be helpful to look at the existing examples of the above style of testing, which can be found in the src/tests folder.