当前位置:网站首页>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
边栏推荐
- A letter to graduating college students
- 简易密码锁
- Pdf files can only print out the first page
- Read blog type data from mysql, Chinese garbled code - solved
- Chapter 8. MapReduce production experience
- Yolov2 learning and summary
- Dbnet: real time scene text detection with differentiable binarization
- Modify MySQL password
- Judge whether the date time exceeds 31 days
- 论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》
猜你喜欢

YOLOV3学习笔记

100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners

(翻译)异步编程:Async/Await在ASP.NET中的介绍

Local rviz call and display of remote rostopic

第8章、MapReduce 生产经验

The dynamic analysis and calculation of expressions are really delicious for flee
![[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle](/img/f8/0e3fbfd13bf06291a73200552ff17a.png)
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle

DBNet:具有可微分二值化的实时场景文本检测

Scroll view specifies the starting position of the scrolling element

每日刷題記錄 (十一)
随机推荐
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
UNI-APP中条件注释 实现跨段兼容、导航跳转 和 传参、组件创建使用和生命周期函数
Yolov3 learning notes
【无标题】
Luogu problem list: [mathematics 1] basic mathematics problems
Time format record
YOLOV2学习与总结
学习笔记 -- k-d tree 和 ikd-Tree 原理及对比
表达式的动态解析和计算,Flee用起来真香
Introduction to software engineering
Pytorch exercise items
vmware虚拟机C盘扩容
Une exploration intéressante de l'interaction souris - pointeur
Daily question brushing record (11)
Understand software testing
Push box games C #
New knowledge! The virtual machine network card causes your DNS resolution to slow down
【5G NR】UE注册流程
Various usages of MySQL backup database to create table select and how many days are left
Journal quotidien des questions (11)