Google C ++ unit test framework (gtest) series tutorial 2-assertion and Function Testing

Source: Internet
Author: User

Introduction

In one of the Google C ++ unit test series tutorials-getting started, this article describes how to compile and TestCodeGenerate an executable file. Let's take a look at the statements and frameworks provided by gtest to help us compile unit test code.

Assertions

1. assertion type

An asserted statement is used to determine whether a condition is true. It is the most basic unit of gtest code. Gtest provides two types of assertions:

    1. Assert _ * series. When the checkpoint fails, the test function is terminated;
    2. When the checkpoint fails, the test function is not terminated and continues to be executed.


When we use an assertion statement, we generally choose the exact CT _ * series for the following two reasons:

    1. When the series of assertions fail, the test function is not terminated, and subsequent checkpoints in the function can be executed;
    2. The test function may contain calls for memory application and release. Assert _ * series assertions may cause subsequent memory release calls to fail and cause memory leakage.

2. Instance

When an assertion fails to be judged, gtest prints the source file and row number of the assertion and provides the failure information. Let's look at an example of a Boolean check:

//The isprime (INT) function determines whether the input parameter is a prime number.
Expect_false (isprime (1));
//...

If the isprime (INT) function is incorrectly implemented and 1 is also considered as a prime number, the test result will contain the following prompt:

 
My_test.cc:15: Failure
Value of: isprime (1)
Actual:True
Expected:False

Gtest provides multiple check types, such as Boolean check, numeric data check, and string check. For more details about how to use these check types, see here.

Test Functions

1. Test ()

In gtest, we use macro test () to define our test function, using the following method:

Test (test_case_name, test_name ){
... Test body...
}

The test body can contain any valid C ++ statements and the asserted statements mentioned above.

The first parameter of test () is the name of the test case, and the second parameter is the name of the test instance in the test case. The names of the two parameters must be valid symbols of C ++ and cannot contain underscores (_). The full name of a test consists of the test case name and test Instance name.

2. Instance

Simply put, it may be hard to understand the usage of test () and the differences between test_case_name and test_name. Let's look at an instance using test.

 
IntFactorial (IntN );//Returns the factorial of N

Suppose factorial (int n) is our tested function. Its function is to return the factorial of N, that is, N !, The test function is as follows:

  //   tests factorial of 0.   
test (factorialtest, handleszeroinput) {
expect_eq ( 1 , factorial ( 0 );
}< br> /// tests factorial of positive numbers.
test (factorialtest, handlespositiveinput) {
expect_eq ( 1 , factorial ( 1 );
expect_eq ( 2 , factorial ( 2 );
expect_eq ( 6 , factorial ( 3 ));
expect_eq ( 40320 , factorial ( 8 );
}

Factorialtest is the name of our test case. Of course, the name of the test case is not necessarily xxxtest. You only need to specify the object to be tested. handleszeroinput and handlepositiveinput are the name of the test instance, the two instances belong to the same factorialtest test case. You can set the name of the test instance at will, but the naming method that can reflect the test function is better.

Compile and execute the test codeProgramThe execution result is as follows:

Running main () from gtest_main.cc
[=========] Running 2 Tests from 1 Test Case .
[----------] Global test environment Set -Up.
[----------] 2 Tests from factorialtest
[Run] factorialtest. handleszeroinput
[OK] factorialtest. handleszeroinput ( 0 MS)
[Run] factorialtest. handlespositiveinput
[OK] factorialtest. handlespositiveinput ( 0 MS)
[----------] 2 Tests from factorialtest ( 1 MS total)

[----------] Global test environment tear-down
[=========] 2 Tests from 1 Test Case Ran .(1 MS total)
[Passed] 2 Tests.

It can be seen that gtest outputs the test results in units of test cases (testcase). In a test case, it lists the test instances (tests) it contains, "Test Case name. the test Instance name is the full name of a test, such as factorialtest. handleszeroinput.

Summary

This article introduces the assertions and test () Methods of gtest. assertions constitute the basic statements of the gtest test code. Test () can be used for function testing and test cases (testcase) it can contain multiple test instances (tests). This hierarchical structure allows us to manage multiple test instances of a test object. The next section will introduce the test fixture to see how gtest provides us for coding and managing test cases :)

Reference: googletest Project

Google open-source C ++ unit testing framework-Google test series by coderzh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.