1、查看fixture的执行过程
pytest --setup-show
2、conftest
待测试程序
def bubble_sort(nums):
for i in range(len(nums)-1):
for j in range(len(nums)-i-1):
if nums[j]> nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
# return random.choice([nums,None,10])
return nums
conftest中的代码
@pytest.fixture(scope= "module")
def bubble_sort_001():
m = [[1, 2, 3, 4, 5, 10, 20, 30, 40, 50], [None], [10]]
n = bubble_sort([10, 1, 20, 2, 30, 3, 40, 4, 50, 5])
return m,n
用例中的代码
def test_bubble_sort(bubble_sort_002):
print("beginning")
assert bubble_sort_002[0] == bubble_sort_002[1]
print("ending")
3、confest中使用yield(相当于setup和teardown)
@pytest.fixture(scope= "module",params=[[1, 2, 3, 4, 5, 10, 20, 30, 40, 50], [None], [10]])
def bubble_sort_002(request):
m = request.param
n = bubble_sort([10, 1, 20, 2, 30, 3, 40, 4, 50, 5])
# return m,n
print("yield 之前 :配置环境变量")
yield m,n
print("yield 之后 :清除数据")
4、fixture参数化
@pytest.fixture(scope= "module",params=[[1, 2, 3, 4, 5, 10, 20, 30, 40, 50], [None], [10]])
def bubble_sort_002(request):
m = request.param
n = bubble_sort([10, 1, 20, 2, 30, 3, 40, 4, 50, 5])
# return m,n
print("yield 之前 :配置环境变量")
yield m,n
print("yield 之后 :清除数据")
网友评论