Supercharge Your Innovation With Domain-Expert AI Agents!

Mock Method and device for unit test of C++

A unit test and sub-process technology, applied in the computer field, can solve problems such as joining and not being able to be implemented on the Linux platform

Inactive Publication Date: 2017-12-01
TRAVELSKY
View PDF3 Cites 1 Cited by
  • Summary
  • Abstract
  • Description
  • Claims
  • Application Information

AI Technical Summary

Problems solved by technology

[0013] Embodiments of the present invention provide a method and device for Mocking C++ unit tests, to at least solve the technical problems that must be “intrusively” added to the test code or cannot be implemented on the Linux platform when performing Mock processing in the prior art

Method used

the structure of the environmentally friendly knitted fabric provided by the present invention; figure 2 Flow chart of the yarn wrapping machine for environmentally friendly knitted fabrics and storage devices; image 3 Is the parameter map of the yarn covering machine
View more

Image

Smart Image Click on the blue labels to locate them in the text.
Viewing Examples
Smart Image
  • Mock Method and device for unit test of C++
  • Mock Method and device for unit test of C++
  • Mock Method and device for unit test of C++

Examples

Experimental program
Comparison scheme
Effect test

Embodiment 1

[0037] In this embodiment, a method of Mocking the C++ unit test is provided, figure 1 It is a flowchart of a method for Mocking a C++ unit test according to an embodiment of the present invention, such as figure 1 As shown, the process includes the following steps:

[0038] Step S102, before the mocked function is called, modify the first instruction of the mocked function to a jmp instruction;

[0039] Step S104, when the mocked function is called and executed, directly jump to the substitute function corresponding to the jmp instruction for execution.

[0040] Through the above steps, before referencing the Mocked function to run, Mock the function that needs to be shielded, modify it when the program is running, restore it to achieve Mock, and will not pollute the project code, which solves the need for Mock processing in the prior art Intrusive" testing code or technical problems that cannot be implemented on the Linux platform.

[0041] Optionally, the execution subje...

Embodiment 2

[0064] In this embodiment, a device for mocking a C++ unit test is also provided, and the device is used to implement the above embodiments and preferred implementation modes, and those that have already been explained will not be repeated. As used below, the term "module" may be a combination of software and / or hardware that realizes a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, implementations in hardware, or a combination of software and hardware are also possible and contemplated.

[0065] Figure 5 It is a structural block diagram of a device for Mocking a C++ unit test according to an embodiment of the present invention, such as Figure 5 As shown, the device includes:

[0066] The modification module 50 is used to modify the first instruction of the mocked function into a jmp instruction before the mocked function is called;

[0067] The jump module 52 is configured to directly jump to th...

Embodiment 3

[0073] This embodiment describes the application in detail in conjunction with specific examples:

[0074] This embodiment is implemented based on the ptrace system call in the Linux system, and can perform dynamic Mock processing on non-virtual functions at the assembly or binary level. In the whole process, before the child process calls the Mocked function, notify the parent process to modify the Mocked function; the first instruction of the modified Mocked function is the jmp instruction, that is, jump to the Mocked function; after running the Mocked function , restore the modified data.

[0075] The steps for using this technical solution include:

[0076] S11. Introduce this technical solution through source code, and integrate the source code into the unit test code of the project. That is, it is compiled together with the project code.

[0077] S12, before the unit test is run, the assemblemock::Setup function is generally called directly in the main function to ini...

the structure of the environmentally friendly knitted fabric provided by the present invention; figure 2 Flow chart of the yarn wrapping machine for environmentally friendly knitted fabrics and storage devices; image 3 Is the parameter map of the yarn covering machine
Login to View More

PUM

No PUM Login to View More

Abstract

The invention provides a Mock method and device for unit test of C++, wherein the method includes: before a Mock function is called, modifying a first command of the Mock function into a jmp command; when the Mock function is called for execution, directly jumping to a corresponding substitute function to the jmp command to carry out execution. By using the Mock method and device, the technical problem of the prior art is solved that there is a need for adding test codes in 'invasive' manner in Mock processing or Mock processing is unable to be implemented on a Linux platform.

Description

technical field [0001] The invention relates to the field of computers, in particular to a method and device for mocking a C++ unit test. Background technique [0002] Currently in the industry, C / C++ unit test development frameworks mainly include GoogleTest and UnitTest. Unit testing needs to focus on the current product / business code and shield the influence of external modules or the external environment. At this time, the Mock component is needed to provide shielding from the association outside the tested code. [0003] GoogleMock provides this function, but GoogleMock can only do Mock processing for virtual functions in C / C++, and cannot do Mock processing for non-virtual functions. [0004] Mock processing of non-virtual functions is just necessary in the C / C++ development process. Although there are products on the market that provide mock processing for C / C++ non-virtual functions, there are two weaknesses: [0005] 1. It is necessary to "intrusively" add test c...

Claims

the structure of the environmentally friendly knitted fabric provided by the present invention; figure 2 Flow chart of the yarn wrapping machine for environmentally friendly knitted fabrics and storage devices; image 3 Is the parameter map of the yarn covering machine
Login to View More

Application Information

Patent Timeline
no application Login to View More
IPC IPC(8): G06F11/36
Inventor 马静雨甘国操杨超其他发明人请求不公开姓名
Owner TRAVELSKY
Features
  • R&D
  • Intellectual Property
  • Life Sciences
  • Materials
  • Tech Scout
Why Patsnap Eureka
  • Unparalleled Data Quality
  • Higher Quality Content
  • 60% Fewer Hallucinations
Social media
Patsnap Eureka Blog
Learn More