
For testing professionals, the most widely used unit testing tool is definitely pytest.
pytest is a very popular and mature, fully featured Python testing framework, suitable for unit testing, UI testing, and interface testing. It is similar to the unit testing framework unittest, but pytest is more concise and efficient.
Many testers feel that pytest is the best framework for testing after learning unittest and pytest, because pytest has many advantages:
-
Simple and flexible, easy to get started.
-
Supports parameterization.
-
Can mark tests with features and attributes.
-
pytest has many third-party plugins and can be customized for extensions, such as pytest-selenium (integrates Selenium), pytest-html (generates HTML test reports), pytest-rerunfailures (re-executes failed cases), etc.
-
Using skip and xfail can handle unsuccessful test cases.
-
Can distribute tests across multiple CPUs using the xdist plugin.
-
Allows using assert directly for assertions, without needing to use self.assert *.
-
Convenient for use in continuous integration tools.
sudo pip install pytest
and press enter, as shown in the screenshot below:
pytest -V
or pytest --version
to check if pytest is installed successfully, as shown in the screenshot below:
test
under /home/shiyanlou/
, then use VS Code to open the test
folder. Next, create a new py
file in the test
folder and name it test_example.py
. The content of test_example.py
is as follows:import pytest
def add(a, b):
return a + b
def test_add1():
print("The result of add(2, 3) is: {}".format(add(2, 3)))
assert add(2, 3) == 5
def test_add2():
assert add(2, 3) == 6
if __name__ == "__main__":
pytest.main()
pytest.main()
in the py
file and using the run button in the upper right corner of VS Code, or running the command in the command line. In this experiment, we will run it in the command line.cd /home/shiyanlou/test/
to enter the test
folder, then type pytest
and press enter, as shown in the screenshot below:
-
It shows the platform on which it is running, the Python version, and the execution root directory. -
The collected test cases, collected 2 items
indicates that a total of two test cases were detected. -
The executed test files and the results of the test cases in the test files, where .
indicates a passed test case, andF
indicates a failed test case. -
[100%]
indicates the overall progress of running all test cases. -
If a test case fails, it will display the specific test case and highlight where the error occurred. -
At the end, it will give a simple statistic of the overall situation, for example, 1 failed, 1 passed in 0.09s
.
SQOOP has launched“Practical Project of Pytest Automation Framework,”which will guide you through a systematic learning of Pytest through 11 courses, gradually mastering the Pytest framework, PyMySQL, logging, and OpenPYXL, as well as data-driven models and keyword-driven models in automation testing.
Demonstration
Results of the data-driven model practical demonstration:
Results of the keyword-driven model practical demonstration:


Long press the QR code for direct access to the course link:

What you will learn:

Long press the QR code for direct access to the course link:
Target Audience
-
Employed Individuals: Software testers
-
Students: Those who want to understand automated testing
-
Others: Individuals interested in the Pytest framework
Long press the QR code for direct access to the course link:
Exclusive for fans
Know your enemy and know yourself, and you can fight a hundred battles without danger.
More interview questions sorting and explanation
Will be released irregularly in the group
Also various soft test knowledge
VIP resources, material packs, interview experiences, open classes
Follow our public account for event benefits…..
Long press the image to scan the code to join the group↓↓↓
Currently, more than 100,000 people have followed and joined us.
Long press the QR code
Follow the 【SQC Online Course】 video account