当前位置:网站首页>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
边栏推荐
猜你喜欢

Dbnet: real time scene text detection with differentiable binarization

New knowledge! The virtual machine network card causes your DNS resolution to slow down

How to migrate or replicate VMware virtual machine systems

Selenium - by changing the window size, the width, height and length of different models will be different

Create your own deep learning environment with CONDA

Push box games C #

表达式的动态解析和计算,Flee用起来真香

这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意

How to scan when Canon c3120l is a network shared printer

机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
随机推荐
Naive Bayes in machine learning
Cannot get value with @value, null
Pytorch exercise items
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
[leetcode] day93 - intersection of two arrays II
【无标题】5 自用历程
The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
Modify MySQL password
JMeter performance automation test
【类和对象】深入浅出类和对象
Climb movie paradise 2021 hot
Support vector machine for machine learning
pytorch练习小项目
Read blog type data from mysql, Chinese garbled code - solved
每日刷题记录 (十一)
Some thoughts on machine learning
Simple understanding of bubble sorting
Reinstalling the system displays "setup is applying system settings" stationary
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)