See Python unit tests from open source projects

Source: Internet
Author: User
Tags stub

I think that in the past when I developed the program, apart from the documentation, the unit test might be another thing that I wanted others to write, but I didn't want to write anything. However, as the development process increased, as well as the increase in the number of Bug changes, I found that UT in a great extent to my advantage, although the result is probably my Dev time will increase 20-40% around, but, compared to a period of time after a sudden a Bug, so you can not touch the mind , or suddenly one after the Bug after you turn the test to mention, write UT happiness and pride is obviously higher.

As far as I am concerned, I think there are several advantages to writing unit tests:

    1. Help reduce the coupling of your code so that you can more easily write UT
    2. Clarify the module dependencies of the code so that you can know what to Mock in UT and what to Stub.
    3. Keeping the interface clean and clear, UT is programming for the interface, and both Input and ouput need to be clear
    4. UT is a self explanatory document that can be used by others to learn how to use your interface using UT.

What I need to say here is that, in general, it's hard for me to do Test first, which is the so-called completion of the UT code before writing the implementation code. Of course, this is not to say no, in the current experience, this is not a good thing, a very important reason is the project cycle of the control, if you UT first, in case you are not enough time to implement Logical Code? UT does not allow your entire Project to run. So, in general, most of the projects I go through are Run first, and then UT to ensure that the current functionality is normal and that the correctness of the functionality will not be compromised in future maintenance and updates.

Test method

Students who have learned the perfect software engineering system know that software testing is a very important part of software engineering, and even for a project, testers are much more involved than Developer, I just graduated from that meeting, the test personnel participation can be said to be through the whole process, From demand to acceptance release, this entire process has the participation of testers, and Developer, may participate in the "Turn test" environment is almost over, so the test project is a very complex subject.

Test coverage

Because testing is very complex, there are many methodologies and practices. In the case of UT, we can have several different test angles for the code. For example, the coverage angle, we have the statement overlay, branch coverage, conditional coverage, path coverage and loop coverage, and test content, we will sub-module testing, data structure testing, path testing, error handling testing and boundary testing and so on. For so many tests, I actually found that most of the open source projects are not very strict with these theories, because it may be said that a random theory in practice can make people crazy.

In fact, in some of the popular open source projects I've seen, it's basically a statement overlay, and it doesn't reach 100%, so it's more about UT testing with the main function as the normal goal. Here are the test results for some open source projects:

?

In addition, for the increase of UT is built on the basis of issue, also said that when a user mentioned a issue, Maintener think this issue is a problem, and will affect my project, then will develop and develop the corresponding patch fix it, and to fill the UT, This situation is also more common, so that gradually UT coverage will slowly go up.

Test method

In testing, our code may have a lot of dependencies, such as module dependencies, component dependencies, and so on, in order to solve these dependencies, we always have some way to deal with, there are two frequently used techniques: stubs and Mock.

I used to like to talk about an object Mock-off, meaning that an object is replaced with a custom simulation class, so that we can customize the behavior and output of the class, but I found that this is actually a stub in the test, so-called stub is the module and the component that simulates the test code call. Thus customizing the invoked behavior and output; In contrast, the function of a Mock is to verify that the template or component has not been invoked, and a common example is whether the mail delivery service has been invoked, the output log content, and so on. For more details on stubs and mocks I recommend Martin folwer this article: Mocks aren ' t stubs.

Test tools

In Python, the UnitTest framework of the class XUnit itself is very simple to use, such as the following is a very simple test case:

?

It's already easy to use, but Python's little buddy still thinks he's too wordy and slow, so you'll find Pytest is a popular library.

Pytest

Pytest as a unit test framework, the use of a variety of methods, both can be similar to Python's own unittest, but also can be very simple to write UT a function, not only the development efficiency will be higher, and the execution efficiency can be higher, other advantages are not verbose introduced, The official website is listed: Pytest.

As for how simple and convenient, you save the following code to the test_sample.py file, and then execute the command under the corresponding directory path pytest

?

After execution you should find:

?

Yes, you will find that it is so simple to execute. However, many students are still dissatisfied with this, because many Python projects not only adapt to a version of Python, so there will be more than one Python version of the test (Python's another hole, 2.6/2.7/3.x/3.5 incompatible).

Tox

To satisfy a Python project that can work in multiple versions of Python, many people use TOX for compatibility testing in different environments, so Tox's function is environment management and test run. For more versatile usage and details of Tox, please refer to Tox official website: tox.

Tox generally have a Tox.ini file, such as a simple example:

?

Then execute the tox command-line tool and it will find the various environments on your machine and test it, and the final result will be something like this:

?

Summary

Unit testing is a habit and a responsibility. Through unit testing, we can tell others that my code is work, but also give others a sense of trust, can let others believe you write code. At the same time, writing unit tests is a trivial matter, we have to deal with dependencies, consider Test case, but these processes can help us to better think about our projects and software, so that the structure of the software and the quality of the code to improve a step.

See Python unit tests from open source projects

Related Article

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.