其他分享
首页 > 其他分享> > 3:fixture和conftest.py文件

3:fixture和conftest.py文件

作者:互联网

一:conftest文件(必须与要运行的用例在同一包下,并且有__init__py文件)

import pytest

1:#那个用例执行传函数名
@pytest.fixture()
def login01():
print('登录')
2:#全部用例都自动执行
@pytest.fixture(autouse=True)
def login():
print('\n自动执行')


实例
@pytest.mark.smoke
def test_three(self,login01):
print('执行three方法')
a = "hello"
assert 'e' in a


3:配置@pytest.mark.标签(所有标签一致的都运行)
def pytest_configure(config):
marker_list=['smoke','smoke1'] #所有的标签列表--防止警告用
for markers in marker_list:
config.addinivalue_line(
'markers',markers
)


pytest -v test_01.py -m smoke

 

 

4:#参数化--前两个是变量,后面是对应的数据

@pytest.mark.parametrize('test_input,expected',[('3+5',8),('2+5',7),('7+5',30)])
def test_eval(test_input,expected):
assert eval(test_input)==expected

 

 

@pytest.mark.parametrize('test_input,expected',yaml.safe_load(open('./data.yaml')))
def test_03(test_input,expected):
print(yaml.safe_load(open('./data.yaml')))
print(test_input+expected)
assert test_input==expected

 

5:#参数组合
@pytest.mark.parametrize('x',[1,2])
@pytest.mark.parametrize('y',[8,9,10])
def test_foo(x,y):
print(f'测试数据组合x:{x},y:{y}')

 

 

6:#indirect=True,可以把传过来的参数当函数来调用执行

test_data=['li','jia']
@pytest.fixture(scope='module')
def login_1(request):
#默认接受并传入参数
user=request.param
print(f'用户名{user}')
return user

#indirect=True,可以把传过来的参数当函数来调用执行
@pytest.mark.parametrize('login_1',test_data,indirect=True)
def test_login(login_1):
a=login_1
print(f'测试用例的login的返回值为{a}')

 

 

7:#跳过
@pytest.mark.skip
def test_02():
print('执行02方法')
a = "hello"
assert 'e' in a

 

 

8:#功能不知道是否实现了,通过就是XPASS,失败XFAIL,先写着
@pytest.mark.xfail
def test_02():
print('执行02方法')
a = "hello"
assert 'h' in a

 

标签:py,fixture,mark,print,conftest,pytest,test,input,def
来源: https://www.cnblogs.com/vip01/p/15831569.html