Automated testing framework - getting started with pytest framework

Today I will tell you about the pytest framework.

Today's article will introduce the following aspects:

1. First introduce the pytest framework

2. Take everyone to install the Pytest framework

3. Points to pay attention to when using the pytest framework

4. How to run pytest

5. Commonly used plugins in the pytest framework

0 1. Introduction to pytest framework

pytest is a third-party unit test framework for python, which is more concise and efficient than the built-in unittest, supports a very rich plug-in, and is compatible with the unittest framework. This allows us to not need to rewrite the code when the unittest framework is migrated to the pytest framework.

Advantages of pytest framework

1. Simple and flexible, easy to use

2. Support parameterization

3. It can support simple unit testing and complex functional testing, and can also be used for automated testing such as selenium/appium, and interface automated testing (pytest+request)

4. pytest has many third-party plug-ins, and can customize extensions,

02. Installation of pytest framework

Installation of pytest framework

pip install pytest

Precautions for using the pytest framework

.py test files must start with test_ (or end with _test)

Test classes must start with Test and cannot have an init method

Test methods must start with test_

Assertions must use assert

03. pytest operation mode

1. If no parameters are provided, pytest will look for test files starting with test_ or ending with _test in the current directory and subdirectories, and test functions starting with test_ in the file

2. When specifying the test file at runtime

1. At this time, the directory must be switched to the directory where the test file is located, otherwise pytest cannot search for the test and will not execute it.

2. When executing the function in the test file, it will search for the test function starting with test_, and the function that does not meet this rule will not be executed

3. If it contains a test class, when the test file is allowed, it will search for a class whose class name starts with Test, and the function name in the test class must also meet the requirements before it will be executed

4. The way of running can be run on the command line or on the main method

For example: such as: specify to run the test_01 module in the pythonDemo directory

    ......

if __name__ == "__main__":

pytest.main(["pythonDemo/test_01.py"])

Or command line mode:

pytest  pythonDemo/test_01.py

In fact, there are many ways to run pytest, and there are many parameters that can be used to achieve different effects.

04. Commonly used plug-ins and installation of pytest framework

The pytest framework provides a lot of plug-ins, which can realize different functions. Today, I will give you an example of the most commonly used plug-ins.

pytest-xdist

When there are a lot of use cases, the running time will also become very long. If you want to shorten the running time of the script, you can use multiple processes to run it. Multi-cpu distribution.

pytest-rerunfailures

When testing, short-term network fluctuations will cause the use case to fail. At this time, the problem can be solved by retrying the use case. (rerun after a use case fails

pytest-html

is a pytest plugin for generating XML/HTML test reports. (HTML test report)

pytest-assume

The assert assertion is available in pytest, but the assertion after a failure will no longer be executed; pytest-assume can realize multiple verification of multiple assertions.

pytest-repeat

There will be some occasional bugs during the test process. For this kind of problem, we will repeatedly execute this use case many times, and finally reproduce the problem; the pytest-repeat plug-in can play this role: Repeated execution of test cases

pytest-ordering

pytest is executed in alphabetical order by default (lowercase English--->uppercase English--->0-9 numbers)

The order between the use cases is that the files are sorted according to the ASCLL code, and the use cases in the files are executed from top to bottom

setup_module->setup_claas->setup_function->testcase->teardown_function->teardown_claas->teardown_module

But you can customize the execution order of use cases through the third-party plug-in pytest-ordering

Alright, let me introduce to you today


If the article is helpful to you, remember to like, bookmark, and add attention. I will share some dry goods from time to time...

END Supporting Learning Resources Sharing

Finally:  In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free 【保证100%免费】

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

è¿éæå¥å¾çæè¿°

How to obtain the full set of information:

è¿éæå¥å¾çæè¿°

Guess you like

Origin blog.csdn.net/IT_LanTian/article/details/131248577