| Fatal assertion | Nonfatal assertion | Verifies |
|---|---|---|
ASSERT_TRUE(condition); |
EXPECT_TRUE(condition); |
condition is true |
ASSERT_FALSE(condition); |
EXPECT_FALSE(condition); |
condition is false |
ASSERT_EQ(val1, val2); |
EXPECT_EQ(val1, val2); |
val1 == val2 |
ASSERT_NE(val1, val2); |
EXPECT_NE(val1, val2); |
val1 != val2 |
ASSERT_LT(val1, val2); |
EXPECT_LT(val1, val2); |
val1 < val2 |
ASSERT_LE(val1, val2); |
EXPECT_LE(val1, val2); |
val1 <= val2 |
ASSERT_GT(val1, val2); |
EXPECT_GT(val1, val2); |
val1 > val2 |
ASSERT_GE(val1, val2); |
EXPECT_GE(val1, val2); |
val1 >= val2 |
ASSERT_THAT(value, matcher); |
EXPECT_THAT(value, matcher); |
value matches matcher |
| Fatal assertion | Nonfatal assertion | Verifies |
|---|---|---|
ASSERT_THROW(statement, exception_type); |
EXPECT_THROW(statement, exception_type); |
statement throws an exception of the given type |
ASSERT_ANY_THROW(statement); |
EXPECT_ANY_THROW(statement); |
statement throws an exception of any type |
ASSERT_NO_THROW(statement); |
EXPECT_NO_THROW(statement); |
statement doesn't throw any exception |
-
TEST(TestSuiteName, TestName) -
TEST_F(TestFixtureName, TestName)
TEST(ClassUnderTestSuite, callMeShouldAlwaysReturn42) {
// GIVEN
ClassUnderTest cut{};
auto expected = 42;
// WHEN
auto result = cut.callMe();
// THEN
ASSERT_EQ(result, expected);
}struct ClassUnderTestFixture : public ::testing::Test {
// common data and helper functions
ClassUnderTest cut{};
}
TEST_F(ClassUnderTestFixture, callMeShouldAlwaysReturn42) {
// GIVEN
auto expected = 42;
// WHEN
auto result = cut.callMe();
// THEN
ASSERT_EQ(result, expected);
}Przetestuj algorytm std::sort().
W tym zadaniu ważne jest pokrycie jak największej liczby scenariuszy tego algorytmu.
Potrzebujemy 3 rzeczy:
- Klasę Fixture, dziedziczącą po
testing::TestWithParam<T>, gdzie T jest typem danych wejściowych, które będą dostarczane do testu - Scenariusz testowy
TEST_P - Generator
INSTANTIATE_TEST_SUITE_P, który wygeneruje przypadki testowe
class MyFixture : public testing::TestWithParam<std::pair<int, int>> {
// You can implement all the usual fixture class members here.
// To access the test parameter, call GetParam() from class
// TestWithParam<T>.
ClassUnderTest cut;
};
TEST_P(MyFixture, MyTestName) {
// Inside a test, access the test parameter with the GetParam() method
// of the TestWithParam<T> class:
auto [value, expected] = GetParam();
EXPECT_EQ(cut.myFunction(value), expected);
}
INSTANTIATE_TEST_SUITE_P(MyInstantiationName,
MyFixture,
testing::Values({1, 1},
{2, 1},
{3, 2}));Dzięki generatorom danych jeden scenariusz testowy może zostać uruchomiony na różnych danych testowych.
-
Range(begin, end [, step])- Yields values
{begin, begin+step, begin+step+step, ...}. The values do not include end. step defaults to 1.
- Yields values
-
Values(v1, v2, ..., vN)- Yields values
{v1, v2, ..., vN}.
- Yields values
-
ValuesIn(container),ValuesIn(begin,end)- Yields values from a C-style array, an STL-style container, or an iterator range [begin, end)
-
Bool()- Yields sequence
{false, true}.
- Yields sequence
-
Combine(g1, g2, ..., gN)- Yields all combinations (Cartesian product) as
std::tuplesof the values generated by the N generators.
- Yields all combinations (Cartesian product) as
(Jeśli starczy czasu lub dla chętnych)
Wykonaj zadania z repo fan_controller. Będziemy na nim pracować na lekcji Testowanie #3. Oprócz polecenia z README.md dopisz też te same testy we frameworku GTest.