Pytest framework, let you see what is the rich and handsome of the unit testing industry!

Pytest is a powerful unit testing framework in the python language, used to manage and organize test cases, and can be used in unit testing and automated testing. Unittest is also a unit testing framework in the python language, but it has limited functions and is not as flexible as pytest.

Just like: Apple computer mac air and mac pro are the same. They all have the same functions, but are easy to use and better to use.

This article contains the following content points:

01. A simple example of pytest

02, pytest installation

03, the characteristics of pytest, the difference with unittest

04, how pytest automatically recognizes use cases

05. In the pytest framework, the running sequence of the use cases

1. Pytest copy example

It is very simple to write a use case in pytest. Here is a simple example:

Pytest framework, let you see what is the rich and handsome of the unit testing industry

 

The results of the operation are as follows:

Pytest framework, let you see what is the rich and handsome of the unit testing industry

 

Second, the installation of Pytest

Installation command: pip install pytest

Third, the characteristics of Pytest, different from Unittest

The characteristics of pytest are as follows:

◆ Automatically identify test cases (in unittest, TestSuite needs to be introduced to actively load test cases.)

◆ Simple assertion expression: assert expression is sufficient (in unittest, self.assert*)

◆ There are fixtures at the test session, test module, test class, and test function level (unittest is a fixture at the test class and test function level)

◆ There are very rich plug-ins, currently 600+, such as allure plug-ins (unittest not available)

◆ The test case does not need to be encapsulated in the test class (unittest requires a custom class and inherits TestCase)

So how does pytest automatically identify test cases? When we write pytest use cases, what rules need to be followed?

Fourth, how does Pytest automatically identify use cases?

The recognition rules are as follows:

◆  Search root directory: By default, test cases are collected from the current directory, that is, in which directory the pytest command is run, search from which directory;

◆  Search rules:

1) Search for files: files that meet the naming rules test_*.py or *_test.py

2) Rules for identifying use cases in documents satisfying 1):

Function names beginning with test_;

Among the test classes starting with Test (without init function), the function starting with test_

Example: In the D:\pycharm_workspace directory, create a python project named study_pytest.

Under the project, create a python package named TestCases.

In the package, create a test case file: test_sample_1.py.

The contents of the file are as follows:

Pytest framework, let you see what is the rich and handsome of the unit testing industry

 

According to the search rules defined above, you need to jump to the project directory, and then execute the command: pytest -v.

The execution results are as follows:

Pytest framework, let you see what is the rich and handsome of the unit testing industry

 

Let us happily add in the second test file: test_sample_2.py

The content is as follows:

Pytest framework, let you see what is the rich and handsome of the unit testing industry

 

Run the command again: pytest -v

Get the following results:

Pytest framework, let you see what is the rich and handsome of the unit testing industry

 

Through the execution of multiple use case files, you can see the execution order of the use cases.

Five, the order of execution of use cases in Pytest

Principle: The use cases in the py files that are searched first are executed first.

In the same py file, according to the code order, the first searched use case is executed first.

I share some of my favorite information, I hope it can help everyone

This information is organized around [software testing]. The main content includes: python automated testing exclusive video, Python automated detailed information, a full set of interview questions and other knowledge content. For friends of software testing, it should be the most comprehensive and complete preparation warehouse. This warehouse has accompanied me through a lot of bumpy roads, and I hope it can also help you. Pay attention to WeChat public account: Programmer Erhei, you can get it directly
 

Guess you like

Origin blog.csdn.net/m0_52668874/article/details/114897344