Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python's pytest testing framework

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge about how to use Python's pytest testing framework. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

It can be said that pytest is a veteran of the Python testing framework, which is very mature and has the following six main characteristics:

Simple and flexible, easy to use, rich in documents

Support for parameterization and fine-grained control of the test cases to be tested

Can support simple unit testing and complex functional testing, but also can be used to do selenium / appnium and other automated testing, interface automation testing (pytest+requests)

Pytest has many third-party plug-ins and custom extensions, such as pytest-selenium (integrated selenium), pytest-html (perfect html test report generation), pytest-rerunfailures (failed case repeat execution), pytest-xdist (multi-CPU distribution), etc.

Skip and xfail processing of test cases

Can be well integrated with CI tools, such as jenkins

Step 1: installation and easy to use

Installation:

Pip install pytest

Easy to use:

Create a new test_sample.py file and enter the following code:

Def input_number (I):

Return I + 1

Def test_answer ():

Assert inc (2) = = 3

In the test_sample.py file, click the execute pytest command, and pytest will run all files named "test_.py" or "_ test.py" in the current directory and its subdirectories.

In the above code, we use the assert statement to verify the test expectation. There is an assertion reflection mechanism in pytest that intelligently reports the intermediate value of the assert expression. This test returns a failure report because input_number (2) does not return 4.

Step 2: configuration file

The pytest configuration file can change the way pytest runs. It is a fixed file pytest.ini file that reads configuration information and runs in the specified way.

[pytest]

# add command line parameters

Addopts =-s

# File search path

Testpaths =. / scripts

# File name

Python_files = test_*.py

# Class name

Python_classes = Test*

# method name

Python_functions = test_*

Addopts

The addopts parameter can change the default command line option, which is used when we enter instructions in cmd to execute the use case. For example, I want to finish testing and generate a report. The instruction is long.

Pytest-s-html=report.html

Each time you type so much, it's hard to remember, so you can add it to the pytest.ini.

Modify addopts =-s-html = report.html in the configuration file

So that next time I open cmd and type pytest directly, it can take these parameters by default.

Testpaths

By default, pytest will go to the directory and files in the current directory to collect test cases (functions that begin with test_). But most of the time we just want to search a fixed folder, such as the scripts folder under the project directory. In that case, we can think about this feature through the configuration file.

Testpaths =. / scripts

Python_files

By default, pytest will look for the py file that begins with test. If we want to specify a file or specify some regular file names, we can use this parameter to modify it.

Python_files = test_*.py

Python_classes

By default, pytest will find a class that begins with Test. If we want to specify a class or specify some regular class names, we can use this parameter to modify it.

Python_classes = Test*

Python_functions

By default, pytest looks for a function that begins with test. If you want to specify a function or specify some regular function names, you can use this parameter to modify it.

Python_functions = test_*

Step 3: assert

An assertion is a debugging mechanism used to verify that the code meets the expectations of the encoder. During development, coders should reasonably use the assertion mechanism for the parameters of the function and the execution results in the middle of the code to ensure that defects in the program are found in the testing phase as far as possible.

In a nutshell, an assertion is a test of certain assumptions.

Assert expected result = = actual result

Def test_cut (self):

A = 5

B = 5

Cut_num = a-b

Assert 10 = = cut_num

The 10 after assert is an expected value, and cut_num is the actual value. The pytest framework will determine whether the relationship between the two is equivalent (= =). When the condition is true, the assertion is successful and the script is approved. When the condition is not valid, the assertion fails and the script fails.

These are all the contents of this article entitled "how to use Python's pytest testing Framework". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report