Python simulation data tools which are more useful

Source: Internet
Author: User

Today we recommend two basic Python simulation data tools: Mock and Pytest monkeypatch.

Why do you want to simulate data?

There are some parts of our application that need to rely on external libraries or objects. In order to get out of this part, we need to replace these external dependencies, so we use the simulation data. We simulate an external API to produce a specific behavior, such as returning the appropriate return value that matches the previous definition.

Simulation function

We have a function.py module:


Then let's look at how to use it in conjunction with a mock library:
What's going on here? 1-4 lines are for compatibility with Python 2 and the code introduced by the introduction, in Python 3 the mock is a standard library, and in Python 2 you need to manually install (PIP install mock).

In line 13th, the patch method that calls the mock of the square function, be sure to call the patch method where the function is used. For example, I will call the square (5) function in the program itself, so I use patches in __main__, for example Python tests/test_function.py. And if I use pytest, then I'll use Test_function.square to replace the __main__.square here.

On the 第18-19 line, I called the patch function for the square and cube methods because they will be used in the main function. The last two assert statements are to ensure that the mock is properly used.

You can also use monkeypatching to do the job:


You can see that I use monkeypatch.setattr to set the return value for a given function, and I also need to call the Monkeypatch function in the right place.

Simulation class

I have a module called square:


Here's how to use it:
In line 13th I patch the class square, while lines 15 and 16 represent an instance of mocking, first mocked_instance is a mock object, and for Mock.calculate_area I add return_ Value is 1. In line 23 I use the Magicmock method, which is also a standard mock class, except that it can retrieve the corresponding method from a given object. Finally I use Patch.object to simulate the methods in the Square class.

The code in Pytest is as follows:


The problem here is that test_mocking_class_methods works fine in Python 3, but not in Python 2. At present I have not thought too clearly this point, hoped you can help me!

All the examples can be found here.

By the way, we recommend a Python video tutorial: http://www.maiziedu.com/course/python/has a variety of Python basics and Python development projects, ideal for novice and Python programmers to see.

Python simulation data tools which are more useful

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.