During my working on CTCI (cracking the coding interview), as the code became more complex and numerous I began to feel less secure about the robustness of my code. Also reading the Unix Philosophy it kept emphasizing on robustness and I figured that testing was worth spending some time on.
For C++ there are many choices, the few that popped up during my search were Catch, Boost, and GoogleTest. In all honesty I didn't expect myself to end up with GoogleTest because I had wanted a relatively simple and straight-foward solution.
I tried Catch (now Catch2) first. All it required was a header include. However I think because I was using Visual Studio 2013 Express to write my code, the header file didn't compile. I could either figure out the compilation issue or move on to a different solution. The docs on Catch didn't really offer much debugging details so I moved on to googletest and proceeded to download the files on my Windows 10 laptop and compiled them with CMake. However something disagreed and I think it was complaining that my build tools were too old.
I caved and downloaded Visual Studio 2019 and it's a very enjoyable experience. It also has a GoogleTest quick start project which is searchable - WOW, kudos to the dev team to make this so smooth for us. So I actually didn't need to build the GoogleTest and import the .lib files! Yay.
Issues arised when I tried to use this on my Windows 8.1 (but more powerful core i7) laptop and I ended up upgrading to Windows 10 since the Windows 8.1 SDK was depreciated.
Of course the upgrade had its kinks - the sound, monitors, and search were not working right after the upgrade. : ( It took ~ one and a half day to get everything working normally again. :D
But all this effort was worth it. I became more confident in my ability to deal with computer hardware and IT stuff like upgrading drivers and whatnot.
In the end I currently have ~150 tests to test my code from chapter 1-3. This gives some confidence that the basics work, and that I do have a way to validate my work (to some extent). Of course with C++ there's always possibility for memory leaks so there's always room for improvement testing-wise.
However I'm quite happy with the GoogleTest setup and will re-iterate it here:
- Create new visual studio project and search for googletest
- Name it and create filters for source files and header files
- Code as usual. (In the new VS need to include "pch.h" in every .cpp file)
- If writing tests only there doesn't need to be a main because gtest (google test) will create a main for the user if not provided (there's also a setting in VS to change this if needed)
- The tests all start with TEST(<testSuiteName>, <testCaseName>) { <test content> }
- Use EXPECT_ when the failure is non-fatal (doesn't affect next steps of the test) and user ASSERT_ when the test failure is fatal and the rest of the test should be aborted. (E.g. if a previous function returns a value that will be read next.)
- GoogleTest includes true/false, eq, ne, lt, gt, ge, le (i.e. ==, !=, >, <, >=, <=) for both EXPECT_ and ASSERT_
- Strings have their own streq, strne, strcaseeq, strcasene (case means case-insensitive) for EXPECT_ and ASSERT_
- Use "nullptr" when comparing pointers as it is typed
- Run the tests by compiling and then running like usual. The green means pass and red means failed. (This can be customized in settings too)