知识点1:Pytest的命名原则 1、文件名以 test_.py 文件和_test.py 2、测试函数以test_ 开头 3、以 Test 开头的类,不能包含 init 方法 4、以 test_ 开头的类里面的方法 5、所有的包 pakege 必项要有__init__.py 文件 知识点2:项目环境设置  
基础例子:命名一个文件test_01.py
import pytest
def add(x):
return x + 2
def out(x):
return x-1
def test_add():
assert out(2) == 1
def test_add1():
assert add(2) == 4
if __name__ == '__main__':
pytest.main(["-s","test_1.py"])
运行结果  类 易错点:类中不允许使用构造方法 init()
class Test_01(object):
def test_add(self):
assert 1 == 1
def test_add1(self):
assert 3 == 3
数据驱动 比如第一次aa传11,bb=22;第二次aa传33,bb=44 那么就可以用数据驱动,只写一个test就可以实现2条用例的效果 pytest的数据驱动用的是装饰器@pytest.mark.parametrize
class Test_01(object):
@pytest.mark.parametrize("aa,bb", [[11, 22], [33, 44]])
def test_01(self, aa, bb):
print()
print(f"aa:{aa}")
print(f"bb:{bb}")

fixture 前置后置 (1)前置:yield 前的步骤 (2)后置:yield 后的步骤
@pytest.fixture()
def fix():
print()
print("这是 fixture 前置")
yield
print("这是 fixture 后置")
class TestInterface(object):
def test_01(self, fix):
print("test_01 用例步骤")
assert 1 == 1
 fixture 配合 parametrize
data = [10, 20, 30]
@pytest.fixture()
def editData(request):
yield request.param + 1
@pytest.mark.parametrize("editData", data, indirect=True)
def test_1(editData):
print()
print(editData)

|