Google Test é um framework C++ para testes unitários, desenvolvido pelo Google. Ele permite escrever testes automatizados, verificar condições, agrupar testes, fixtures, etc.
Dependências: Compilador: Clang, GNU GCC ou 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
Após rodar: ./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)
– Verifica igualdadeASSERT_TRUE(cond)
– Para execução se falharTEST_F
– Usa fixtures (setup/teardown)Se alterarmos a linha do sum_positive
para um float
e um número negativo: EXPECT_EQ(sum(2.f, -3), 5);
As falhas são emitidas:
[==========] 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
Você pode testar todas as suas funções, classes, structs de qualquer projeto, ex.: SFML, SDL, … e qualquer outro tipo de projeto.
Para mais informações acesse o repositório: https://github.com/google/googletest.