当前位置:网站首页>Pytest's collection use case rules and running specified use cases
Pytest's collection use case rules and running specified use cases
2022-07-06 09:00:00 【Automated test seventh uncle】
Preface
Hello, guys , Today I will explain it to you pytest How to collect the use cases we have written ? How can we run individual use cases or batch use cases ? Now I'll answer for you one by one !

One 、Pytest Collect use case principles
First of all, we will create our project according to the following directory structure
[pyttest Rules for searching test cases ]
|[ Test case catalog 1]
| |__init__.py
| |test_ Test module 1.py
| |test_ Test module 2.py
|[ Test case catalog 2]
| |__init__.py
| |test_ The test case 1.py
| | The test case .py
|test_ Test module .py
| The test case 2.py Two 、 Code instance
# test_ Test module 1.py
def test_testFunc1():
print('\n I'm a test case ! in test_testFunc1')
assert 1 == 1
def func1():
print(' I'm not a test case ')
assert 1 == 1# test_ Test module 2.py
class TestClass1(object):
def test_class_func1(self):
print('\n I'm a test case in a class in test_class_func1')
assert 1 == 1
def class_func1(self):
print(' I'm a normal function in a class !')# test_ The test case 1.py
class TestClass2(object):
def test_class_func2(self):
print('\n I'm a test case in a class in test_class_func2',)
assert 1 == 1
def class_func2(self):
print(' I'm a normal function in a class !')
def test_testFunc2():
print('\n I'm a test case in test_testFunc2!')
assert 1 == 1
def func2():
print(' I'm not a test case ')
assert 1 == 1# The test case .py
def test_testFunc3():
print('\n I'm a test case ! in The test case .py')
assert 1 == 1
def func3():
print(' I'm not a test case ')
assert 1 == 1# test_ Test module 3.py
def test_testFunc4():
print('\n I'm a test case ! in test_testFunc4')
assert 1 == 1
def func4():
print(' I'm not a test case ')
assert 1 == 1
class TestClass3(object):
def test_class_func3(self):
print('\n I'm a test case in a class in test_class_func3')
assert 1 == 1
def class_func3(self):
print(' I'm a normal function in a class !')# The test case 2.py
def test_testFunc5():
print('\n I'm a test case ! in test_testFunc5')
assert 1 == 1
def func5():
print(' I'm not a test case ')
assert 1 == 1Let's use cmd Command to execute this project , See how many use cases are valid ? open cmd Switch to the root of the project to execute the command pytest -v
D:\pytest Search test case rules >pytest -v
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 6 items
test_ Test module 3.py::test_testFunc4 PASSED [ 16%]
test_ Test module 3.py::TestClass3::test_class_func3 PASSED [ 33%]
Test case catalog 1/test_ Test module 1.py::test_testFunc1 PASSED [ 50%]
Test case catalog 1/test_ Test module 2.py::TestClass1::test_class_func1 PASSED [ 66%]
Test case catalog 2/test_ The test case 1.py::TestClass2::test_class_func2 PASSED [ 83%]
Test case catalog 2/test_ The test case 1.py::test_testFunc2 PASSED [100%]
========================== 6 passed in 0.59 seconds ===========================
The running result shows that there are 6 Use cases passed, And a detailed list of which 6 strip , So the use cases we wrote above are not just 6 strip , So why only run 6 Striped cloth ? Combining the above code structure with our execution results , We should be able to find such laws
Pytest Will start from the directory we are currently running to find all directories , Looking to test_ The beginning of the file and all of the files with test_ The functions that start with and start with Test Classes and classes that start with test_ The first function is the test case . That's why it only works 6 Test cases !
3、 ... and 、Pytest Run the specified test case
We still use the above project as a demonstration (cdm Switch to the root of the project )
3.1 Run all use cases in the specified directory
We specify the run test case Directory 1 All the use cases in it (pytest -v Test case catalog 1)
D:\pytest Search test case rules >pytest -v Test case catalog 1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 2 items
Test case catalog 1/test_ Test module 1.py::test_testFunc1 PASSED [ 50%]
Test case catalog 1/test_ Test module 2.py::TestClass1::test_class_func1 PASSED [100%]
========================== 2 passed in 0.05 seconds ===========================
# This will only search and specify all the uses under the specified directory 3.2 Run all use cases in the specified file
We specify the run test_ Test module 1.py(pytest -v Test case catalog 1/test_ Test module 1.py )
D:\pytest Search test case rules >pytest -v Test case catalog 1/test_ Test module 1.py
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
Test case catalog 1/test_ Test module 1.py::test_testFunc1 PASSED [100%]
========================== 1 passed in 0.09 seconds ===========================
# Run all use cases under the specified file 3.3 Run the test class in the specified file
We specify the run test_ Test module 2.py Test class in Testclass1(pytest -v Test case catalog 1/test_ Test module 2.py::TestClass1)
D:\pytest Search test case rules >pytest -v Test case catalog 1/test_ Test module 2.py::TestClass1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
Test case catalog 1/test_ Test module 2.py::TestClass1::test_class_func1 PASSED [100%]
========================== 1 passed in 0.05 seconds ===========================
# Run all tests in the specified test class with 3.4 Run the specified test case function
We specify the run test_testFunc1(pytest -v Test case catalog 1/test_ Test module 1.py::test_testFunc1)
D:\pytest Search test case rules >pytest -v Test case catalog 1/test_ Test module 1.py::test_testFunc1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
Test case catalog 1/test_ Test module 1.py::test_testFunc1 PASSED [100%]
========================== 1 passed in 0.03 seconds ===========================summary
Collect use case rules : Search all to test_ The first test file , With Test The first test class , With test_ The first test function
Execute use case rules : from -v We should be able to find the execution information of the parameter output , Run the use cases in the specified directory Use command pytest Catalog / Catalog that will do ; Run the specified file using pytest Catalog / file that will do ; Run the specified class or function Use command pytest Catalog / file :: Class name :: Function name perhaps pytest Catalog / file :: Function name
Search case rules are also our named case files , Test class , Rules for testing functions ; Execute the specified test case to remember the rules .
Finally, that's all for today's article. My favorite friends can like comments, collect and pay attention , Your support is the motivation of the author .

边栏推荐
- Leetcode: Jianzhi offer 03 Duplicate numbers in array
- 在QWidget上实现窗口阻塞
- LeetCode:498. Diagonal traversal
- Navicat Premium 创建MySql 创建存储过程
- LeetCode:剑指 Offer 03. 数组中重复的数字
- LeetCode:34. Find the first and last positions of elements in a sorted array
- BMINF的後訓練量化實現
- CUDA implementation of self defined convolution attention operator
- Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
- 一篇文章带你了解-selenium工作原理详解
猜你喜欢

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

KDD 2022论文合集(持续更新中)

LeetCode:498. 对角线遍历

LeetCode:236. 二叉树的最近公共祖先

Excellent software testers have these abilities
![[sword finger offer] serialized binary tree](/img/e2/25c9322da3acda06c4517b0c50f81e.png)
[sword finger offer] serialized binary tree
![[OC foundation framework] - string and date and time >](/img/75/e20064fd0066810135771a01f54360.png)
[OC foundation framework] - string and date and time >

Nacos 的安装与服务的注册

Esp8266-rtos IOT development

ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
随机推荐
LeetCode:39. Combined sum
Philosophical enlightenment from single point to distributed
Cesium draw points, lines, and faces
Mise en œuvre de la quantification post - formation du bminf
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
What is an R-value reference and what is the difference between it and an l-value?
超高效!Swagger-Yapi的秘密
LeetCode:394. String decoding
LeetCode:34. Find the first and last positions of elements in a sorted array
MySQL uninstallation and installation methods
LeetCode:41. 缺失的第一个正数
Simple use of promise in uniapp
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
Variable length parameter
ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
Li Kou daily question 1 (2)
BMINF的后训练量化实现
什么是MySQL?MySql的学习之路是怎样的
LeetCode:387. 字符串中的第一个唯一字符