I created this as a testbed/showcase for a basic use of the google test framwork within visual studio. The architecture in this project is pretty simple, but it demonstrates discrete fixtures for the class being tested (both public and private member functions).
In a single-project solution, I went back and forth on the merits of two architectures:
- All the unit tests concentrated into a single UnitTests.cpp file, with separate .h/cpp files for the harness classes
- A .h/.cpp for the tests of each individual class that would contain all the harnesses and tests needed for that class Ultimately, I think the second option is probably best. You will be dealing with a lot of files quickly, but a logical naming convention (the foo class is tested by the tests in footest.h/cpp) keeps that from getting confusing. If you did not plan on relying on test fixture classes, and doing all your work with the TEST macro, then I think the single UnitTests.cpp approach would be better- but it would have to be a very small project for that to make any sense, and small projects have no downsides to more files.
An important note for developers that work with hardware, databases, or any setup that isn't always available as you develop: there are unit test playlists in visual studio that you access by right-clicking the unit tests in the test explorer. Using this you can create playlists for the various situations you will be developing in.
- Create a Project (say a console application)
- use NuGet (right click project) to add one of the Microsoft.googletest adapters (depending on whether you want static or dynamic compilation, and static or dynamic runtime). Static/Dynamic is recommended
- add #define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING to preprocessor definitions (hopefully the need to do this will soon disappear)
- add the following two lines to main
- testing::InitGoogleTest(&argc, argv);
- return RUN_ALL_TESTS();
- add a new class to the project
- add a test fixture class to the project. In the header:
- include <gtest.gtest.h>
- include the class you want to test
- forward declare that class
- inherit from : testing :: test
- add stubs for (static) SetUpTestCase, (static) TearDownTestCase(),SetUp, and TearDown
- add a static pointer member variable for the class you want to test
- In the implementation(cpp) file of the test harness:
- initialize your pointer to the class to be NULL
- in SetUpTestCase, delete the object if it is not NULL, and then assign it to a new object
- in TearDownTestCase, delete the object, and set the pointer to NULL
- in the header for the class you want to test:
- use FRIEND_TEST(TestCaseName, TestName); to anticipate tests and expose methods- public or private
- create a UnitTests.cpp file. In it, declare the tests you want using TEST_F
- use the test playlist to add your new tests to either StandAlone or WithHardware (or database, or whatever)
- implement your object
Read the advanced documentation for google test here