C++ testing framework GoogleTest for beginners

Although developers are mainly responsible for the development tasks in the project, every developed function needs to pass the developer's self-test, so you often hear developers mention the topic of unit testing. So today I will take you to take a look at the famous Google C++ testing framework GoogleTest.

a brief introdction

Let’s take a look at how Google officially introduces this framework:

Googletest is a testing framework developed by the testing technology team according to Google's specific requirements and constraints. Whether you're working on Linux, Windows, or Mac, if you write C++ code, googletest can help you. It supports any kind of testing, not just unit testing.

Although Google developed this framework based on specific needs and constraints, it also specified that this framework can also be used for other purposes, such as being used as an emulator. Of course, this is far away and will not be mentioned for the time being.

When Google developed this framework, it had a basic set of design ideas.

They believe that tests should be independent and repeatable. It can be painful if the test you are doing is affected by the success or failure of other tests. GoogleTest isolates tests by running different tests on different objects. When one or more of the tests fails, the testing framework also allows the tester to continue running the test cases individually for rapid debugging. Tests should be well organized and reflect the structure of the code under test. There are many tests that can actually share data and subroutines, so Googletest provides the concept of test suites to group related tests, and all tests in the test suite can share data and subroutines. This pattern is actually very common and makes the tests easy to maintain, especially when you need to switch to a new code project, the testing process will be greatly simplified.

A technology company of the size of Google has a lot of internal C++ projects, which are cross-platform, so it has platform-independent requirements for the testing framework. This is exactly the case with GoogleTest, no matter which compiler you use or which platform you use, it works.

Of course, the testing process should provide as comprehensive information as possible to help developers understand the process of executing the software, and GoogleTest is no exception. Even if a test execution fails and is stopped during testing, the testing framework will continue other tests. If you don't want a test to stop due to failure, you can set the test to be non-fatal. A testing process like this can help developers find as many bugs as possible in a single run-edit-compile cycle. Is such a testing framework efficient?

Although the occurrence of bugs is annoying enough for developers, the tediousness of the self-testing process is even more annoying (why do your hair fall so fast). For example, in addition to testing specific content, developers also have to track the test list themselves. . Googletest can help you get out of here and become a cool coder.

Here's a question, have you ever used JUnit or PyUnit? If so, then Googletest is really easy for you. It is said that Googletest is developed based on the xUnit architecture. As for what the xUnit architecture is, I will talk about it again when I have the opportunity.

During the testing process of the testing framework, the basic testing operation is to use assertions to verify the behavior of the test code. A crash or assertion failure indicates that the behavior of the code under test is incorrect, otherwise it is correct.

First Experience

Let’s take a look at a simple test example (environment configuration and installation will be introduced in detail later)

#include <math.h>
#include <gtest/gtest.h>

int square(const int a)
{
    int b = a * a;
    if (b != b) {
        return -1;
    } else {
        return a * a;
    }
}

TEST(SquareTest, PositiveNos) { 
    ASSERT_EQ(0, square(0));
    ASSERT_EQ(36, square(6));
    ASSERT_EQ(324, square(18));
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

This test example is to test whether the function int square(const int a) behaves correctly.

After compiling and generating the executable file, run it

As you can see, there is a total of one test suite and one test above. First, start a global test environment and use the macro TEST() provided by the framework to create a test suite named SquareTest and a test named PositiveNos. Then, run all the tests contained in the test suite and the tests pass OK. After all tests are completed, uninstall the global test environment. Summarize all test results PASSED. The testing process ends.

The article is reproduced from: ENG Bajie

Original link: https://www.cnblogs.com/englyf/p/17274911.html

Guess you like

Origin blog.csdn.net/sdgfafg_25/article/details/131698094