Solve the problem in unit test of C ++ code-unverifiable and IO call

Source: Internet
Author: User

In C ++ProgramDuring unit testing, we often encounter two problems:

1. It is inconvenient to verify the test results because the test results are inaccessible for protection and private class members.

2. It is difficult to bypass connect, receive, and send to be accessed.

Here we will introduce two methods to solve the problem:

1. compile unit testCodeAdd the unit_test macro (the name can be another one you like) and define private as public, that is:

# Define private public

In this way, you can test the code to any member of the classification class. unit testing is to do a white-box test, which clearly shows the interior.

I used to like the method of user Meta class, but it is better to install and test the code in the code.

2. for I/O problems, you only need to inherit from the tested method, and then re-implement (if it is virtual, it is hidden) the corresponding method, to facilitate verification, this will solve the problem. the decorator mode in the design mode is very suitable here.

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.