laitimes

pytest测试框架pytest-random-order插件随机执行用例顺序

author:Talk to Korey

Pytest provides a variety of plug-ins to extend its functionality, and this chapter introduces the pytest-random-order plugin, which randomly sets the order in which pytest test cases are run, and gives some control over randomness.

pytest测试框架pytest-random-order插件随机执行用例顺序

Official Documentation: https://pytest-cov.readthedocs.io/en/latest/index.html

Adaptation Notes:

python >= 3.5

pytest-random-order安装

Use the pip command to install: pip install pytest-random-order (installed in the python environment where the pytest project runs, the python installation directory or the virtual environment directory, you can refer to the previous article to view the running environment pycharm and configure the pytest running environment)

Install via pycharm: Open the settings and install the plug-in as shown in the figure below (Windows system)

pytest测试框架pytest-random-order插件随机执行用例顺序

pytest-random-order运行

You don't need to import it when you use it, you can add parameters directly to the pytest run command. The main parameters are as follows:

  • --random-order: randomizes the execution order of all test cases in the entire test session. By default, all test functions, methods, and classes are included.
  • --random-order-bucket:设置重新排序的范围,可以是class、module、package、global,parent, grandparent。 前面四个比较好理解,剩下两个parent和grandparent指的是用例的父节点和祖父节点(比如class内的用例父节点是class本身,模块内的函数动用例父节点是模块本身)。
  • --random-order-seed: Specifies a random number to ensure that the same random order is obtained each time the number is used.

The following is an introduction to the three parameters in the source code:

pytest测试框架pytest-random-order插件随机执行用例顺序

执行带上参数--random-order

Design a few examples: The expectation is that all use cases will be executed in a shuffled order.

pytest测试框架pytest-random-order插件随机执行用例顺序

Result of the 1st execution:

pytest测试框架pytest-random-order插件随机执行用例顺序

Result of the 2nd execution:

pytest测试框架pytest-random-order插件随机执行用例顺序

The actual results showed that the order of execution was different each time, which was as expected.

执行带上参数--random-order-bucket

Set up two python files, and the use cases inside are the same as above.

1) With the parameter --random-order-bucket=module, the expected result is that the execution order of the use cases in each file is random, and the execution order of the files remains unchanged.

pytest测试框架pytest-random-order插件随机执行用例顺序

2)带上参数--random-order-bucket=class,执行时预期结果是不同类中的用例随机。

pytest测试框架pytest-random-order插件随机执行用例顺序

3)带上参数--random-order-bucket=package,执行时预期是整个目录内的用例是随机的

pytest测试框架pytest-random-order插件随机执行用例顺序

4)带上参数--random-order-bucket=parent,执行时预期是按用例父节点来随机执行用例顺序。

For example, we add the print condition to the source code, and print the parent node and the grandfather node.

pytest测试框架pytest-random-order插件随机执行用例顺序

以参数--random-order-bucket=parent举例,执行后结果如下:

The parent node of the use case in class is the class itself, and the parent node of the function use case of the file is the file itself, so the parent node of the file is the upper-level folder.

pytest测试框架pytest-random-order插件随机执行用例顺序

执行带上参数--random-order-seed

Again, use the test cases defined above

1)参数--random-order-seed=1000时,结果如下;

pytest测试框架pytest-random-order插件随机执行用例顺序

2)参数--random-order-seed=2000时,结果如下;

pytest测试框架pytest-random-order插件随机执行用例顺序

3) When we use --random-order-seed=1000 again, the result of re-execution is as follows:

pytest测试框架pytest-random-order插件随机执行用例顺序

The results show that the random order is the same as in the first execution.

Encouragement: Eastern Han Dynasty Bangu "Hanshu Mei Cheng Biography": "The pipe of Mount Tai pierces the stone, and the unipolar is broken and dry." The drill of water is not stone, and the saw of Sophi wood is gradually becoming so. ”

----- refers to the continuous dripping of water droplets, which can drip through stones;

----- metaphor for perseverance, and the collection of small forces can also achieve extraordinary achievements.

---- thank you readers for reading and learning, thank you.

--- wish you all the best in your life!!

Read on