理论:

测试用例的识别与运行:

安装:pip install pytest
https://docs.pytest.org/en/stable/
创建文件(直接运行是没有输出的因为使用的默认的unittest测试运行):

如何修改:

再次运行该文件:

如果需要再次使用python解释器则,需要添加一个python解释器

添加完后运行则使用python解释器:

pytest运行方式:
- 第一种使用Python解释器:
import pytest
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
if __name__ == '__main__':
pytest.main(['test_demo01.py'])
-
第二种使用pytest解释器:
在tools里面设置后,直接运行 -
第三种:控制台pytest 文件名 -v

c参数:
-
-k (test_b是方法,-k主要是执行指定的测试用例)
image.png

pytest参数化:
外部文件或者外部数据
第一种外部数据:
- 未参数化前:
def test_answer():
assert inc(3) == 5
- 参数化后
import pytest
def inc(x):
return x + 1
@pytest.mark.parametrize('a,b',[
(2,2),
(3,4)
])
def test_answer(a,b):
assert inc(a) == b
if __name__ == '__main__':
pytest.main(['test_demo01.py'])
pytest如何setup和teardown
fixtrue:
- test_login是需要在test_inc之前执行所以声明fixtrue后,传参数到test_inc中即可
import pytest
@pytest.fixture()
def test_login():
print("登录成功")
def test_inc(test_login):
print("test fuc")
- 被fixtrue的方法带返回值
import pytest
@pytest.fixture()
def test_login():
return "登录成功"
def test_inc(test_login):
print(test_login)
print("test fuc")
pytest命令参数详解:

网友评论