标签:函数声明 方式 test 数据库 span rdo html 参数设置 import
Pytest的fixture功能灵活好用,支持参数设置,便于进行多用例测试,简单便捷,颇有pythonic。
如果要深入学习pytest,必学fixture。
fixture函数的作用:
import pytest
@pytest.fixture()
def before():
print ‘\nbefore each test‘
def test_1(before):
print ‘test_1()‘
def test_2(before):
print ‘test_2()‘
assert 0
import pytest
@pytest.fixture()
def before():
print(‘\nbefore each test‘)
@pytest.mark.usefixtures("before")
def test_1():
print(‘test_1()‘)
@pytest.mark.usefixtures("before")
def test_2():
print(‘test_2()‘)
class Test1:
@pytest.mark.usefixtures("before")
def test_3(self):
print(‘test_1()‘)
@pytest.mark.usefixtures("before")
def test_4(self):
print(‘test_2()‘)
@pytest.mark.usefixtures("before")
class Test2:
def test_5(self):
print(‘test_1()‘)
def test_6(self):
print(‘test_2()‘)
@pytest.fixture(scope="function", autouse=True)
import time
import pytest
@pytest.fixture(scope="module", autouse=True)
def mod_header(request):
print(‘\n-----------------‘)
print(‘module : %s‘ % request.module.__name__)
print(‘-----------------‘)
@pytest.fixture(scope="function", autouse=True)
def func_header(request):
print(‘\n-----------------‘)
print(‘function : %s‘ % request.function.__name__)
print(‘time : %s‘ % time.asctime())
print(‘-----------------‘)
def test_one():
print(‘in test_one()‘)
def test_two():
print(‘in test_two()‘)
import pytest
@pytest.fixture(params=[1, 2, 3])
def test_data(request):
return request.param
def test_not_2(test_data):
print(‘test_data: %s‘ % test_data)
assert test_data != 2
标签:函数声明 方式 test 数据库 span rdo html 参数设置 import
原文地址:https://www.cnblogs.com/zhuochong/p/10620486.html