当前位置:网站首页>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
边栏推荐
猜你喜欢
Golang operation redis: write and read hash type data
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
Docker advanced learning (container data volume, MySQL installation, dockerfile)
Scripy learning
SQL实现将多行记录合并成一行
How to migrate or replicate VMware virtual machine systems
第8章、MapReduce 生产经验
Use selenium to climb the annual box office of Yien
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
随机推荐
2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
Application scenarios of Catalan number
Daily question brushing record (11)
23 design models
opencv鼠标键盘事件
Learning notes -- principles and comparison of k-d tree and IKD tree
Une exploration intéressante de l'interaction souris - pointeur
A letter to graduating college students
Derivation of variance iteration formula
Selenium - 改变窗口大小,不同机型呈现的宽高长度会不一样
机械观和系统观的科学思维方式各有什么特点和作用
Selenium ide installation recording and local project maintenance
使用conda创建自己的深度学习环境
Decision tree of machine learning
【无标题】8 简易版通讯录
Mysql
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
UNI-APP中条件注释 实现跨段兼容、导航跳转 和 传参、组件创建使用和生命周期函数
Summary of the design and implementation of the weapon system similar to the paladin of vitality
How to scan when Canon c3120l is a network shared printer