Google Test is a C++ unit testing framework developed by Google. It allows you to write automated tests, check conditions, group tests, use fixtures, and more.
Dependencies: Compiler: Clang, GNU GCC, or MSVC
Via WinGet
winget install --id=Google.GoogleDrive -e
`
sudo apt install googletest libgtest-dev
sudo pacman -S googletest
#include <gtest/gtest.h>
constexpr auto sum = [](int a, int b){
return a + b;
};
TEST(sum_test, sum_positive) {
EXPECT_EQ(sum(2, 3), 5);
}
TEST(sum_test, sum_negative) {
EXPECT_EQ(sum(-2, -3), -5);
}
int main(int argc, char **argv){
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Compile:
g++ test.cpp -lgtest
After running ./a.out
:
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from sum_test
[ RUN ] sum_test.sum_positive
[ OK ] sum_test.sum_positive (0 ms)
[ RUN ] sum_test.sum_negative
[ OK ] sum_test.sum_negative (0 ms)
[----------] 2 tests from sum_test (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 2 tests.
EXPECT_EQ(x, y)
– Checks for equalityASSERT_TRUE(cond)
– Fails and stops execution if the condition is falseTEST_F
– Uses fixtures (setup/teardown)If we change the sum_positive
line to use a float
and a negative number: EXPECT_EQ(sum(2.f, -3), 5);
Failures will be reported:
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from sum_test
[ RUN ] sum_test.sum_positive
test.cpp:8: Failure
Expected equality of these values:
sum(2.f, -3)
Which is: -1
5
[ FAILED ] sum_test.sum_positive (0 ms)
[ RUN ] sum_test.sum_negative
[ OK ] sum_test.sum_negative (0 ms)
[----------] 2 tests from sum_test (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
[ FAILED ] 1 test, listed below:
[ FAILED ] sum_test.sum_positive
1 FAILED TEST
You can test all your functions, classes, and structs from any project, e.g., SFML, SDL, … or any other type of project.
For more information, visit the repository: https://github.com/google/googletest.