An essential testing framework for Python automation — pytest

Python is widely used in the testing circle, especially in the field of automated testing and test development. Among them, the commonly used testing frameworks in automated testing are unitest and pytest. This article will guide you to build and familiarize yourself with the use of pytest.

Since there is unittest, why use pytest?

This is because pytest has the following characteristics:

Easy to get started, rich in documentation, and there are many example references in the documentation
Support parameterization, you can control the test cases from the details

Able to support simple unit tests and complex functional tests

Support test skipping, failure case mark

Support retry on failure

Support test cases written by nose, unittest

Support for HTML reports

Continuous integration with jenkins is very convenient

Has many third-party plug-ins, and can be customized

         01 Install pytest 

cmd or terminal run

view version 

Quickly experience a  direct execution of Terminal in pycharm

Here we must remind everyone to pay attention to the execution rules of pytest. The rules are as follows:

If you directly type pytest to execute, it will search for the test_*.py or *_test.py file in the current directory and its subdirectories. After finding the file, find the function starting with test in the file and execute it

If you only want to execute a certain py file, use pytest xxx.py

02 pytest test case design principles

When writing use cases with pytest, the following rules need to be followed to be recognized and executed:

The file name must be named after *_test.py or test_*.py

Test functions must start with test

Test classes must start with Test and cannot contain __init__ methods

Methods starting with test_ in the test class

All packages must have an __init__.py file

03 pytest execution use case rules

1. Execute all use cases in a certain directory

Executing the pytest command in the first directory will find all use cases in the directory that meet the pytest use case rules and execute them

2. Execute a py file separately

Execute all use cases in the pyteststudy.py file that meet the pytest use case rules, and the file does not need to follow pytest's naming rules for files

3. Execute a function in a py file

Or a class, or a method in a class executes the test_add method in pyteststudy.py

 Execute the TestCase class in pyteststudy.py

Execute the test_one method under the TestCase class in pyteststudy.py

4. -s parameter

Indicates detailed printing, which can display the information printed in the script on the result

04 Pycharm runs Pytest

Of course, the above execution seems to be performed on the command line, so can we use pycharm to run ordinary code and right-click to run, which requires certain settings

The setting steps are as follows:

Click on File --> Settings

Enter integrated search in the settings pop-up box

Modify the default test runner to pytest

Then right click in the script 

Guess you like

Origin blog.csdn.net/2201_76100073/article/details/130365630