Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

A detailed introduction to the Python-pytest test framework


May 31, 2021 Article blog


Table of contents


Hello everyone, I'm a little editor who keeps moving on the bald road.

 A detailed introduction to the Python-pytest test framework1

Come today and talk to the little guys who want to get started on the Python test but are still wandering outside the door, talking about pytest, taking the little book, and remembering it carefully.

Let's start with a test framework like pytest:

Pytest is a veteran of the Python test framework and is well-established, with six main features:

  • Simple and flexible, easy to get started, rich documents;
  • Support parameterization, can control the test cases to be tested in fine grain;
  • Can support simple unit tests and complex functional testing, can also be used for automated testing such as selenium / appnium, interface automation testing (pytest-requests);
  • pytest has many third-party plug-ins and can customize extensions, such as pytest-selenium (integrated selenium), pytest-html (perfect html test report generation), pytest-rerunfailures (failed case repetition), pytest-xdist (multi-CPU distribution), etc.;
  • Skip and xfail processing of test cases;
  • Can be well combined with CI tools, such as jenkins

So how do I use pytest? Don't worry, and listen to me one by one.

Step 1: Installation and easy to use

Installation:

pip install pytest

Simple 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 execute the pytest command and pytest will run the . current directory and all the names under its subdirectory are "test_ py" or "test.py" file.

 A detailed introduction to the Python-pytest test framework2

In the above code we used the assert statement to validate the test expectations, and there is an assertion reflection mechanism in pytest that intelligently reports the intermediate value of the assert expression, which returns a failure report because input_number (2) does not return 4.


Step 2: Profile

The pytest profile can change the way pytest works, it is a fixed file pytest .ini file, reads configuration information, and runs the specified way.

[pytest]

# Add command line parameters

addopts = -s

# 文件 文件 搜 路 路径

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 options, which are used when we enter instructions in cmd to execute use cases, such as when I want to test the build report and the instructions are longer

pytest -s —html=report.html

With so much input at a time, it's not easy to remember, so you can add it to the pytest .ini

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

This way the next time I open cmd and enter pytest directly, it will be able to take these parameters with me by default


testpaths

By default, pytest will go into the directory and file under the current directory to collect the test case (test_ function at the beginning). B ut a lot of times we just want to search for a fixed folder, such as the scripts folder in the project directory. I n this case, we can think about this feature through the configuration file.

testpaths = ./scripts

python_files

pytest defaults to looking for a py file at the beginning of test, which we can use to modify if we want to specify a file or some regular file name

python_files = test_*.py

python_classes

pytest defaults to a class that starts with Test, and if we want to specify a class or some regular class name, we can use this parameter to modify it

python_classes = Test*

python_functions

pytest defaults to a function that starts with test, and if we want to specify a function or some regular function name, we can use this parameter to modify it

python_functions = test_*


Step 3: Assertion

Assertion is a error-free mechanism that verifies that the code meets the coder's expectations. During development, coders should make reasonable use of assertion mechanisms for function parameters and intercode execution results to ensure that program defects are discovered as much as possible during the test phase.

Simply put, an assertion is a check of a hypothetical condition.

Assert's expected results are actual results

def test_cut(self):

a = 5

b = 5

cut_num = a - b

assert 10 == cut_num

The 10 after assert is an expected value, cut_num is the actual value, and the pytest framework determines for itself whether the relationship between the two is equivalent, and when the condition is established, the assertion succeeds and the script passes. When the condition is not true, the assertion fails and the script fails.


summary:

As a veteran of the Python test framework, pytest is used for a wide range of purposes, such as fixture, as well as a wide variety of third-party plug-ins.

Here's a quick introduction to pytest that the editor-in-chief explained to you today. I f you learn more about it. We can combine the above to do basic automated testing.

Recommended lessons: Python Automation Management, Python Automation Office.