https://docs.pytest.org/en/stable/index.html
pytest 快速使用
编写基本的测试函数
命令行执行参数
fixture传递参数
fixture设置运行时
parametrize参数化功能
fixture与parametrize联合使用
获取测试用例的执行状态
pytest-order插件
pytest快速入门
安装
pip install pytest
基本用例
def add(x):
return x+1;
def test_x():
assert add(2)==3
执行
在命令行使用pytest命令执行
编写测试函数
# test_demo.py
class TestDemo:
def test_one(self):
assert True
def test_two(self):
assert True
def test_three(self):
assert True
编写测试函数时需要注意:
1文件名以 test_ 开头
2类名以 Test 开头
3类中的测试方法以 test 开头
执行参数
命令行参数
●—collect-only 搜集要运行的测试用例
●-k 匹配表达式
●-m 自定义标记执行
import pytest
class TestDemo():
@pytest.mark.mark1
def test_one(self):
assert True
●-x 失败后结束运行
●-v 显示执行的详细信息
●—maxfail=num 最大失败数 当超过最大失败次数 就不再往下执行
●-s 代码中的打印输出
●—lf
●—ff
●—tb=no 不显示具体的失败信息
fixture的基本使用
订单id的参数传递。
fixture设置运行级别
●session 所有用例执行之前和之后的操作。
●module 整个py文件执行之前和之后的操作。
●class 整个类执行之前和之后的操作。
●function 函数执行之前和之后的操作。
参数化功能
设置多条数据,执行完成后生成多条测试用例。
fixture与参数化功能联合使用
从fixture中获取数据。
获取测试用例的执行结果
conftest.py
testcases\test_result.py
插件:pytest-order
https://pypi.org/project/pytest-order/
指定测试用例的执行先后顺序,如果要在其中插入一条记录,可以使用before或者after指定顺序。
Python复制代码
1
pipinstallpytest
Python复制代码
1
2
3
4
5
6
defadd(x):
returnx+1
deftest_x():
assertadd(2)==3
venv)C:usersznyocesmotesttestasests.
MMMPMPMPPPLPPPLP-E-EEtestsessionstarts
电APAAIPI-AAAAAAAAAAAAA
PLatformwin32—Pthon3.962
rootdin:c:usersizengyPycharmrojcem
colLectedlitem
testcasesltest1.py
[100%]
0.02s
passed
Python复制代码
1
2
3
4
5
6
7
8
9
10
11
12
test_demo.py
classTestDemo:
deftest_one(self):
assertTrue
deftest_two(self):
assertTrue
deftest_three(self):
assertTrue
c:usersizengyPycharmrojectsytestemoptesttestcase
venv
22:2222222222282-0———testsessionstarts
platformwin32—Python3.9.-62
Footdin:c:uzm
collecteduitems
25%]
testcasesltest1.py
[100%]
testcasesltest-demo.Py
:M22NPA84passedin0.07s
C:userszengyPychamojectststmostestt
venV
testcases
AAAMMMPMPPEEtestsessionstarts
pLatformwin32—Python.962
rootdin:c:usesizengyPcharmojec
colLectedGitems
sPackagetestcases>
test_one
C:LSRSZAYYAMRROCTAYSTMPYESTK”OTOOSTTLOCOLCOLY
nlytestcases
venv)
MPLMELMMEAPMW-PWWEETestsessionstarts
atformWin32—Python9
phootdin:c:usersizengyPycharmrojcttm
Collecteduitems/2deseLectd2se
Packagetestcases>
2/4testscollected(2deselected)in0.01s-
VEnYC:sRSzYHYtMPYestI-COLeCTMYTeSTCASES
testsessionstarts—-
Python3.9.1,tst-6.2.2,p-1.03
platformwin32
rootdir:c:luserizengyPycharmprojectsemo
3deselected
coilecteduitems
1selected
C:usersizengyPycharmojectstesmstx-
venv)
M-testsessionstarts
m39Lpet622
ptatformwin32—Python3.9.1,
exe
cachedir:.pytest-cache
pootdin:c:usezemojc
coiLectedGitens
testcases/test1.pletestxPASSED
25%]
50%]
testcases/test_dempy:TesDemo_onAILED
Terminal:Local
assert2—3
assert23
+2
-3
testcasestest_demo.py:11:Assertionerror
AAAAAAAAAEAAAAAAMEAAAAAAAATATAT
shorttestsummaryinfo
FAILEDtestcases/test.demo.pyTestemot_oe
-assert19-2
FAILEDtestcases/test_demo.py:festDemo:tstwo
-assert
生
failed,
deseLectedin0.07s
assert2—3
assert2二三3
-3
testcasesltest_demo.py:11:Assertionerror
shorttestsummaryinfo
FAILEDtestcases/testdemom
testcases/test.demopymoe
FAILEDte
failed,
passedin0.07s
eXE
cachedirg.pvtest_cache
rootdI:C:Usezaj
colLected3items
testcases/testdatayttxixm
PASSED
testcasestestdata.py::test_ddt[-]
PASSED
testcases/test.data.pytst.txiw-xwg
PASSED
passedin0.04s
testsessionstarts
platformwin32
Python3.9.,tt-6.2.2
c:usersizengyycharmpojecthoc
hon.exe
cachedir:.pytest_cache
rootDI:C.usezengychamrojcc
plugins:order-0.10.0
collectedditems
25%]
testcase.py::testfirstPASSED
50%]
testcase.py::testfooPASSED
75%]
testcase.py::test_thirdPASSED
[100%]
testcase.py::test_barPASSED
in0.04s
passed