Test Case
:Test Suite
라고 표현 함. International Software Testing Qualifications Board (ISTQB) 에서도 Test Suite
라고 표현) Test Suite
이라고 표현.Test
: 일반적인 특정 Test Casetest
는 assertions
sentence 로 이루어져 있음.test suite
은 여러 tests 로 구성. tests code 가 objects, subroutine 을 공용으로 사용한다면 text fixture
class 로 넣어도 됨.test program
은 여러개의 test suites
를 포함.ASSERT_*
: fatal failure and abort the functionEXPECT_*
: nonfatal failure. 일반적으로 선호 됨<<
operator 사용ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";
for (int i = 0; i < x.size(); ++i) {
EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
}
_
를 포함하고 있으면 안 됨.TEST(TestSuiteName, TestName) {
... test body ...
}
// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(Factorial(0), 1);
}
// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(Factorial(1), 1);
EXPECT_EQ(Factorial(2), 2);
EXPECT_EQ(Factorial(3), 6);
EXPECT_EQ(Factorial(8), 40320);
}
TEST()
대신 TEST_F()
를 사용.RUN_ALL_TESTS()
로 Run. tests 가 모두 성공이면 0
, 아니면 1
return.RUN_ALL_TESTS()
호출 시RUN_ALL_TESTS()
의 return 값을 무시하면 compile error 발생. Tets 성공 여부를 exit code 로 확인하기 때문에 main()
funciton 은 반드시 RUN_ALL_TESTS()
값을 return 받아야 함.RUN_ALL_TESTS()
는 한번만 호출해야 함. main
function 을 작성 할 필요 없고 gtest_main
, gtest
사용이면 되지만, test 수행 전에 fixture 나 test suite 으로 대응이 불가능하고 custom 하게 수행 해야 될 일이 있으면 main
작성 필요.main
함수는 RUN_ALL_TESTS()
를 반드시 return 해야 함.testing::InitGoogleTest()
함수는 GoogleTest flags 를 parsing 하고 삭제. RUN_ALL_TESTS()
실행 전에 반드시 calling 해야 flags 가 정상 작동 함.main
함수이 과하면, gtest_main
library 로 간단하게 테스트 진행 가능.#include "this/package/foo.h"
#include <gtest/gtest.h>
namespace my {
namespace project {
namespace {
// The fixture for testing class Foo.
class FooTest : public testing::Test {
protected:
// You can remove any or all of the following functions if their bodies would
// be empty.
FooTest() {
// You can do set-up work for each test here.
}
~FooTest() override {
// You can do clean-up work that doesn't throw exceptions here.
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
void SetUp() override {
// Code here will be called immediately after the constructor (right
// before each test).
}
void TearDown() override {
// Code here will be called immediately after each test (right
// before the destructor).
}
// Class members declared here can be used by all tests in the test suite
// for Foo.
};
// Tests that the Foo::Bar() method does Abc.
TEST_F(FooTest, MethodBarDoesAbc) {
const std::string input_filepath = "this/package/testdata/myinputfile.dat";
const std::string output_filepath = "this/package/testdata/myoutputfile.dat";
Foo f;
EXPECT_EQ(f.Bar(input_filepath, output_filepath), 0);
}
// Tests that Foo does Xyz.
TEST_F(FooTest, DoesXyz) {
// Exercises the Xyz feature of Foo.
}
} // namespace
} // namespace project
} // namespace my
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
https://zidarn87.tistory.com/?page=2
출처:
Google test tutorial : https://google.github.io/googletest/primer.html