https://docs.pytest.org/en/stable/index.html
pytest 快速使用
编写基本的测试函数
命令行执行参数
fixture传递参数
fixture设置运行时
parametrize参数化功能
fixture与parametrize联合使用
获取测试用例的执行状态
pytest-order插件

pytest快速入门

安装

  1. pip install pytest


基本用例

  1. def add(x):
  2. return x+1;
  3. def test_x():
  4. assert add(2)==3


执行
在命令行使用pytest命令执行
Pytest 专项练习 - 图1

编写测试函数

  1. # test_demo.py
  2. class TestDemo:
  3. def test_one(self):
  4. assert True
  5. def test_two(self):
  6. assert True
  7. def test_three(self):
  8. assert True


编写测试函数时需要注意:
1文件名以 test_ 开头
2类名以 Test 开头
3类中的测试方法以 test 开头

Pytest 专项练习 - 图2
执行参数
命令行参数
●—collect-only 搜集要运行的测试用例
Pytest 专项练习 - 图3
●-k 匹配表达式
Pytest 专项练习 - 图4
●-m 自定义标记执行

  1. import pytest
  2. class TestDemo():
  3. @pytest.mark.mark1
  4. def test_one(self):
  5. 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
Pytest 专项练习 - 图5

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
Pytest 专项练习 - 图6

C:userszengyPychamojectststmostestt
venV
testcases
AAAMMMPMPPEEtestsessionstarts
pLatformwin32—Python.962
rootdin:c:usesizengyPcharmojec
colLectedGitems
sPackagetestcases>




test_one


Pytest 专项练习 - 图7
C:LSRSZAYYAMRROCTAYSTMPYESTK”OTOOSTTLOCOLCOLY
nlytestcases
venv)
MPLMELMMEAPMW-PWWEETestsessionstarts
atformWin32—Python9
phootdin:c:usersizengyPycharmrojcttm
Collecteduitems/2deseLectd2se
Packagetestcases>

TestDemo>


2/4testscollected(2deselected)in0.01s-
Pytest 专项练习 - 图8
VEnYC:sRSzYHYtMPYestI-COLeCTMYTeSTCASES
testsessionstarts—-
Python3.9.1,tst-6.2.2,p-1.03
platformwin32
rootdir:c:luserizengyPycharmprojectsemo
3deselected
coilecteduitems
1selected



Pytest 专项练习 - 图9
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
Pytest 专项练习 - 图10
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
Pytest 专项练习 - 图11
assert2—3
assert2二三3
-3
testcasesltest_demo.py:11:Assertionerror
shorttestsummaryinfo
FAILEDtestcases/testdemom
testcases/test.demopymoe
FAILEDte
failed,
passedin0.07s
Pytest 专项练习 - 图12

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
Pytest 专项练习 - 图13

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
Pytest 专项练习 - 图14