
Choose your environment: Windows with Visual Studio or Linux with or without CMake; use optional videos, slides, and live coding, and join the forum to suggest new platforms.
Install Google Test across Linux, CMake, and Windows Visual Studio, and explore a sample project with a library and a test runner. Compare static and dynamic linking.
Install Visual Studio from the official download, choose the Community Edition, and install the C++ development workload. Create an empty project, add main.cpp, build, and run to display Hello World.
Instead of installing GTest, we will use FetchContent in CMake to get the framework for us
Install Google Test and Google Mock on Linux by cloning or downloading the repo, build with cmake and make, then copy gtest and gmock into /usr/local/include and libraries to /usr/lib.
Create a minimal Google Test project on Linux to verify an add function using TEST and ASSERT_EQ, build with CMake or g++, and link Google Test (and pthread), enabling C++11.
Move the tested code into a common library and link it to both the test runner and the main application using CMake.
discover what a unit test is: an automated regression check that validates a single function or class, using a simple add two numbers example to show pass or fail.
Explore the big picture of software testing, from unit testing and white-box approaches to integration and system testing, and understand functional, stress, load, and acceptance testing.
Explore the three-part unit test structure: arrange, act, and assert, including setup, execution, and verification. Emphasize single-behavior tests and avoid risky cleanup, for clearer, more maintainable code.
Explore unit testing with Google Test by building a count_positives function test suite using the TEST macro, arranging input vectors, performing actions, and asserting expected results.
Learn how fatal and non-fatal assertions control test flow in Google Test and Google Mock, using assert and expect to verify true, false, equal, not equal, and lower-than or greater-than.
Demonstrate Google Test assertions with code examples, comparing ASSERT_EQ and ASSERT_GE, and showing fatal versus non-fatal failures using ASSERT vs EXPECT and failure messages.
Learn how to use assertions designed for C strings in Google Test, including cert_eq for equality, cert_str for content comparison (not pointers), and cert_str_case_eq for case-insensitive checks.
Learn to test exception behavior in Google Test using assert throw, assert any throw, and assert no throw to verify a function's exception type or absence of exceptions.
Define a square root test suite to verify exception behavior. Test negative input throws std::runtime_error and positive input does not throw, using NegativeArgumentTest and PositiveArgumentTest with ASSERT_THROW and ASSERT_NO_THROW.
Use test fixtures to reuse setup and teardown when they are similar. A fixture is a class derived from the testing namespace's test class with overridable setup and teardown.
Demonstrates using a test fixture to share setup for testing an account class with deposit, withdraw, getBalance, and transfer, including setup, teardown, and fixture-based tests.
Explain the fixture flow for each test, including constructor, setup, test execution, teardown, and destructor, and highlight the roles of constructor and destructor.
Leverage parameterized tests to run the same test body with different inputs by extending a param template class and instantiating test suite P with generators range, values, and combine.
Small update
Follow the first section's Linux instructions to enable Google Mock, and for Visual Studio, watch these videos to set up Google Mock.
Mastering mocking techniques to isolate tests by replacing dependencies with controllable objects, verify calls and parameters with matchers, using both old and new methods in Google Test and Google Mock.
Learn how mocking replaces real objects with test doubles or fakes to isolate the class under test, track calls, and verify interactions with predefined responses.
Learn to mock methods by extending a class and using the mock method macro, covering return type, parameters, const and noexcept, plus legacy versus current versions in Google Mock.
Explore the legacy mocking approach in Google Test with MOCK_METHODn and MOCK_CONST_METHOD, illustrating connect, disconnect, updateSalary, and getSalary parameter counts.
Learn to define method behavior in Google Mock using on call and expect call, chaining actions, return values, and constraints like times and matchers to verify exact call patterns.
Demonstrates mocking with Google Test and Google Mock by validating updateSalary calls on a database connection, using EXPECT_CALL, exact invocation count, and the testing::_ matcher for flexible parameters.
Show how to configure Google Mock actions for specific mock calls, including throwing errors with WillOnce and testing::Throw, and using free functions, lambdas, bound member functions, or std::bind.
Test Google Test and Google Mock assertions on a std::vector by validating generated numbers under a limit with modulus, using ElementsAre and RangeTest to cover bounds.
Explore how to implement callbacks in C++ unit tests with Google Test and Google Mock, using std::function, setting on connect handlers, and mocking expectations with parameter matchers.
Learn to mock private methods by exposing them as virtual and public, and to handle static methods with a helper class and friend relationships, enabling proper unit tests with mocks.
Learn how to run a subset of unit tests in Google Test using command-line and code filters, including Unix vs Windows test suites and test name patterns.
We also have a quick look at what other projects related to Google Test exist.
Configure the test project's properties and debugging command line arguments in Visual Studio to generate an xml report for Google Test.
Celebrate completing the course by starting to use Google Test in your C++ unit testing projects and providing feedback.
This course is for software developers and students who want to be better programmers by writing more solid, bug-free code using the Google Test framework.
Your code will get better and you will have more confidence that your code is correct, because you will catch more bugs using automated tests.
How many times did this happen to you? You wrote new code, and after your hard work is done, you find that you broke features that used to work? And you had to waste more hours finding and fixing the bugs... This problem can get solved in most cases by using automated tests, especially unit tests.
No experience with automated testing is required. Very basic C++ knowledge is needed, nothing too sophisticated.
If you are completely new to Google Test, this course is just for you! If you already have some knowledge, look at the course content and see if you find some more advanced features that you didn't know about.
Google Test works on multiple platforms. You can use any platform you like. In this course, I am doing it on:
Windows (Visual Studio)
Linux (CMake or compiling directly from the command line)
Other platforms will be added if you suggest them to me
There are slides and hands-on coding examples in each section. I recommend that you try the examples and play around with them.
Here's a brief presentation of the course content:
Optional videos: Setting up on Windows (Visual Studio) and on Linux (CMake and GCC)
Basics of unit testing:
Unit test structure
Assertions - numeric, string, exceptions
Reusing code between tests (test fixtures, SetUp() and TearDown())
Parameterized tests: generate multiple tests by writing one single function.
Mocking - replacing real objects with fakes that you can control):
Basics
Setting the behaviour of mocks
Matchers