Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use gtest in Cracket +

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use gtest in Chammer Craft +, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

Google C++ Testing Framework (abbreviated as gtest, http://code.google.com/p/googletest/) is an open source gtest, Cumberplus unit test framework released by Google. It has been used in many open source projects and Google internal projects. Well-known examples include Chrome Web browser, LLVM compiler architecture, Protocol Buffers data interchange format and tools.

There is no shortage of excellent unit testing framework, but gtest still has obvious advantages. Compared with CppUnit, gtest needs to use more centralized header files and function macros and supports automatic registration of test cases. Compared with CxxUnit, gtest does not require the existence of external tools such as Python. Compared with Boost.Test, gtest is simpler and easier to use, and its practicality is not inferior. Wikipedia gives a list of unit test frameworks (http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks) for various programming languages.

I. basic usage

The current version of gtest is 1.5.0, and if you are compiling with Visual C++, the compiler version is required to be at least 7.1 (Visual C++ 2003). As shown in the following figure, its msvc folder contains Visual C++ project and project files, and the samples folder contains 10 usage examples.

In general, our unit test code only needs to include the header file gtest.h. All the structures, classes, functions, constants, etc. commonly used in gtest are accessed through the namespace testing, but gtest has packaged the simplest and most commonly used unit testing functions into macros with parameters, so the existence of namespaces can often be ignored in simple tests.

As gtest is called, the macro TEST defines an executable Test for a specific test case (Test Case). It takes the user-specified test case name (usually the name of the object under test) and the test name as parameters, and delineates a scope for populating the test macro statement and ordinary C++ code. A collection of TEST forms a simple test program.

Common test macros are shown in the following table. The difference between macros that begin with ASSERT_ and those that begin with EXPECT_ is that the former reports the test failure and immediately terminates the test program, while the latter continues to execute the test program after the report.

Assert macro

EXPECT macro

Function

ASSERT_TRUE

EXPECT_TRUE

Judge the truth

ASSERT_FALSE

EXPECT_FALSE

Give a leave

ASSERT_EQ

EXPECT_EQ

Equal

ASSERT_NE

EXPECT_NE

Unequal

ASSERT_GT

EXPECT_GT

Greater than

ASSERT_LT

EXPECT_LT

Less than

ASSERT_GE

EXPECT_GE

Greater than or equal to

ASSERT_LE

EXPECT_LE

Less than or equal to

ASSERT_FLOAT_EQ

EXPECT_FLOAT_EQ

Single-precision floating-point value equality

ASSERT_DOUBLE_EQ

EXPECT_DOUBLE_EQ

Double precision floating point values are equal

ASSERT_NEAR

EXPECT_NEAR

The floating point value is close (the third parameter is the error threshold)

ASSERT_STREQ

EXPECT_STREQ

C string equality

ASSERT_STRNE

EXPECT_STRNE

C string unequal

ASSERT_STRCASEEQ

EXPECT_STRCASEEQ

C string equality (ignore case)

ASSERT_STRCASENE

EXPECT_STRCASENE

C strings are not equal (ignore case)

ASSERT_PRED1

EXPECT_PRED1

Custom predicate function, (pred, arg1) (and _ PRED2,..., _ PRED5)

Write a simple test and try it. Suppose we implement an addition function:

/ / add.h # pragma once inline int Add (int I, int j) {return iTunj;}

The corresponding unit test program can be written as follows:

/ / add_unittest.cpp # include "add.h" # include TEST (Add, negative) {EXPECT_EQ (Add (- 1mairelle 2),-3); EXPECT_GT (Add (- 4Marelle 5),-6); / / intentional} TEST (Add, positive) {EXPECT_EQ (Add (1Mague 2), 3); EXPECT_GT (Add (4jue 5), 6);}

In the code, the test case Add contains two tests, positive and negative (this takes advantage of Visual C++ 2005's feature that allows identifiers to contain Unicode characters). The effect of compilation is as follows:

In the console interface, passed tests are represented in green and failed tests in red. Double horizontal lines separate different test cases, including the launch and result of each test using a single horizontal line and RUN. OK or RUN... FAILED marks. Failed tests print lines of code and reasons, and the test program displays statistical results for all use cases and tests. Readers are advised to try the differences in switching to ASSERT_ macros.

Each test macro can also use the

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report