We also talk about testing private members in nunit.

Source: Internet
Author: User

When I saw muddle talking about testing non-public members in his blog, I suddenly thought that I had asked this question on csdn. At that time, I thought I would say, the test should also follow the OOP principle. I feel very touched and have a new view on this issue.

My current idea is that private members should not be tested.

First, nunit and other tools should be used to test External Interfaces. Especially in test-driven development, when you write test case, it actually forces you to stand on the user's standpoint, write a test case, that is, write a use case, which enables youProgramWith a deeper understanding, users obviously should not be aware of the Implementation Details of Private Members. If you also test Private Members, this effect of unit testing will be broken.

Second, Private Members will be called by public members. Therefore, the test on Private Members is implemented during the test of public members. If private members are already complex and must be tested separately, you should consider whether your design is too complicated. In many cases, private members have a test requirement because the design violates the single responsiblity principle. You need to separate private members that implement complex functions and write them into an independent class.

There is a saying in XP: trying to make your ownCodeWhen it becomes easier to test, a good design will emerge, so there will be a saying that testing is the design. When the classes you write are difficult to test, you should consider whether bad smell appears in your design.

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.