当前位置:网站首页>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
边栏推荐
- scroll-view指定滚动元素的起始位置
- [untitled] 5 self use history
- ssh链接远程服务器 及 远程图形化界面的本地显示
- UNI-APP中条件注释 实现跨段兼容、导航跳转 和 传参、组件创建使用和生命周期函数
- [untitled] 8 simplified address book
- HMS core helps baby bus show high-quality children's digital content to global developers
- 每日刷题记录 (十一)
- [set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
- [LeetCode]404. 左叶子之和
- Request weather interface format, automation
猜你喜欢

JMeter linked database

2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)

远端rostopic的本地rviz调用及显示

Local rviz call and display of remote rostopic

如何迁移或复制VMware虚拟机系统

Selenium - 改变窗口大小,不同机型呈现的宽高长度会不一样

Golang operation redis: write and read hash type data

ruoyi接口权限校验

Journal quotidien des questions (11)

Summary of UI module design and practical application of agent mode
随机推荐
Floating menu operation
Various usages of MySQL backup database to create table select and how many days are left
Time format record
The dynamic analysis and calculation of expressions are really delicious for flee
Page text acquisition
IE browser flash back, automatically open edge browser
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
Unittest attempt
YOLOV3学习笔记
这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意
Local rviz call and display of remote rostopic
代码管理工具
“我为开源打榜狂”第一周榜单公布,160位开发者上榜
[C /vb.net] convert PDF to svg/image, svg/image to PDF
Pytest attempts to execute the test case without skipping, but the case shows that it is all skipped
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
JMeter linked database
Docker advanced learning (container data volume, MySQL installation, dockerfile)
Modify MySQL password