The workshop video assumes you are working with an existing CMake project file, one in which you will create a test project. This may not be the case for all projects, in this write up we'll perform the necessary step to create a test project and have it build within an existing project.
Before you begin open the existing file found in the Workshop folder and take a look at them, in particular get familar with the Person class we will be unit testing.
- class Person
- {
- int _age;
- std::string _name;
- public:
- Person( const int age, const std::string& name );
- Person( const Person& obj );
- Person& operator=( const Person& obj );
- void setAge( const int age );
- const int getAge() const;
- void setName( const std::string& name );
- std::string getName() const;
- };
In this write-up most of the steps will still be the same as in the workshop video. The only difference will be how we edit the CMakeLists.txt that gets generated for the test file.
Let's begin by opening the command shell and changing into the Workshop folder. From the Workshop subfolder, type the following commands.
Now copy utgen binary from safetynet/Bin/ folder into the 'test' folder. You can do this from the shell by typing:
To complete the test class, we need to create the unit test methods. There are 2 methods we want to test, to generate unit test method for each, the switch '/t' is used followed by the names of the methods to be tested. Type the following from the shell:
The next step is to generate a test project, we will create a CMake makefile project. We use the utgen utility again and pass it the "/project" switch, what follows is the (output) name of the unit test executable. Next we list the path to the unit test project, which in our case is the current folder that is denoted by ".", and finally we need to list the source file of the subject class. Type the following at the shell:
Open the generated CMakeLists.txt in an editor and un-comment the line found near the bottom of the file:
This adds the source file person.cpp to the variable TestSubjectClasses, it is used to declare files that get included in the unit test build. You can list more files if the situation dictates. Notice there is a "../" before person.cpp, this tells CMake to search the parent folder using a relative path.
If everything went well, you should get a stream of output in the console from the unit test and also see them in the SafetyNet UI test monitor. Note, the unit test binary is located under the folder (or something similar for other platforms):
safetynet/Workshop/test/build_make/
The switch r:test, will run the test after the build. You will see 2 failing test results in the SafetyNet test monitor and also in the console output.
You have successfully created a unit test project and a unit test class. What's left as an exerices is to complete each unit test case and add actual test code and also to add new test casees by hand for methods, getName and setName.
Let's now take a look at how to generate a CodeBlocks and VisualStudio project.
CodeBlocks project generationOnce a Visual Studio project has been created, you will need to make sure to perform a "Release" build, otherwise the test app will fail to run (due to linkage) since the SafetyNet binaries are Release builds. The "UnitTest.dll" from SafetyNet/Bin/ is required for the unit test exe to run.
If you want your test project made available for editing using Visual Studio or CodeBlocks, it is worth reading Creating SafetyNet IDE Projects. You will also learn how to run the test automatically after a build.