当前位置:网站首页>Pytest -- write and manage test cases
Pytest -- write and manage test cases
2022-07-03 06:44:00 【Xiaojing, who likes dumplings】
Test file directory
Test cases should be separated from source code , Put it alone in a tests Folder
tests The folder is located in the root directory of the project 
The general test case directory is like this
conftest contain hook Functions and fixture
pytest.ini, preservation pytest Configuration under this project
assert
There are two kinds of assertions : The first one is , stay assert Add any expression after
assert
The second kind : Assertion of exception type ,with pytest.raise()
def test_div_one():
with pytest.raises(ZeroDivisionError):
result=my_div(1,0)
Further more , If you want to assert the information thrown by the exception , have access to value.args[0] To get exception information
def test_div_two():
with pytest.raises(ZeroDivisionError) as exceptInfo:
my_div(1,0)
excepted_msg=exceptInfo.value.args[0]
assert excepted_msg=='division by zero'
Manage test cases
(1) By using marker( Mark ) Group test cases
For example, select some test cases as smoke test , You can use decorators pytest.mark.smoke To mark these test cases
When it's running , adopt -m Option to specify which tags to run
pytest -m “smoke”
Indicates that the run is marked smoke Test cases for
-m You can also use expressions later , You can add and or not etc.
such as pytest -m “smoke and get” Run marked as smoke and get Test cases for
(2) Skip test cases
pytest Built in some tags ,skip,skipif xfail
skip and skipif Allow skipping test cases that you do not want to run
Simply skip a test case , Use pytest.mark.skip(reason=" xxxx")
If you want to judge whether to skip the use case according to a certain condition , Use pytest.mark.skipif(condition,reason), condition Is the condition , When the condition is true when , Skip the test case ,reason It's the reason
Parametric test cases
If you use multiple sets of data to test the same test case , have access to @pytest.mark.parametrize(argnames,argvalues) The decorator achieves the purpose of batch transmission of parameters
The first parameter is str,List[str],Tuple[str]
The second parameter is a list of values
@pytest.mark.parametrize("a",[1,5,7])
def test_parmetrized(a):
print(a+1)
@pytest.mark.parametrize(["a","b"],[(1,2),(3,4)])
def test_parametrized_one(a,b):
print(a+b)
Running results :
C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 2 items
test_my_add.py::test_parametrized_one[1-2] 3
PASSED
test_my_add.py::test_parametrized_one[3-4] 7
PASSED
@pytest.mark.parametrize(("a","b","c"),[("one","two","three")])
def test_paramtrized_two(a,b,c):
print(a+b+c)
Running results :
C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 1 item
test_my_add.py::test_paramtrized_two[one-two-three] onetwothree
PASSED
@pytest.mark.parametrize("a,b,c",[(1,2,3),(4,5,6)])
def test_parametrized_three(a,b,c):
print(a+b+c)
Running results :
C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 2 items
test_my_add.py::test_parametrized_three[1-2-3] 6
PASSED
test_my_add.py::test_parametrized_three[4-5-6] 15
PASSED
Run a subset of tests
Run a single directory :pytest tests/func
Run a single test file / modular pytest tests/func/test_add.py
Run a single test function pytest tests/func/test_add.py::test_one
Run a single test class pytest tests/func/test_add.py::TestAdd
Run a single test method in the test class pytest tests/func/test_add.py::TestAdd::test_one
边栏推荐
- Operation principle of lua on C: Foundation
- 【无标题】5 自用历程
- 2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
- Judge whether the date time exceeds 31 days
- ssh链接远程服务器 及 远程图形化界面的本地显示
- Install VM tools
- Pytorch exercise items
- 如何迁移或复制VMware虚拟机系统
- 10万奖金被瓜分,快来认识这位上榜者里的“乘风破浪的姐姐”
- SSH link remote server and local display of remote graphical interface
猜你喜欢

Reinstalling the system displays "setup is applying system settings" stationary

Local rviz call and display of remote rostopic

剖析虚幻渲染体系(16)- 图形驱动的秘密

Scroll view specifies the starting position of the scrolling element

SSH link remote server and local display of remote graphical interface

SQL implementation merges multiple rows of records into one row

Golang operation redis: write and read hash type data

VMware virtual machine C disk expansion

Ruoyi interface permission verification

Personally design a highly concurrent seckill system
随机推荐
每日刷題記錄 (十一)
Chapter 8. MapReduce production experience
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
Selenium - 改变窗口大小,不同机型呈现的宽高长度会不一样
Oracle Database Introduction
YOLOV2学习与总结
Unittest attempt
The win7 computer can't start. Turn the CPU fan and stop it
[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)
Judge whether the date time exceeds 31 days
Modify MySQL password
Learning notes -- principles and comparison of k-d tree and IKD tree
卡特兰数(Catalan)的应用场景
【无标题】5 自用历程
Yolov3 learning notes
Time format record
堆排序和优先队列
(翻译)异步编程:Async/Await在ASP.NET中的介绍
Understand software testing
The difference between CONDA and pip