目录
前言
- 在自动化测试中,想要按自定义顺序执行测试用例,怎么办呢?这时就需要一个第三库( pytest-ordering)来实现。
pytest用例执行顺序
- 不同文件的执行顺序:按照目录文件名顺序执行
- 同一文件下的执行顺序:按照用例顺序从上到下执行
顺序执行
# -*- coding: utf-8 -*-
# @Time : 2021/10/23
# @Author : 大海
# @File : test_28.py
import pytest
# 同一个文件中,按照用例顺序从上到下执行
class TestHomepage(object):
def test_2(self):
print('这是用例2')
def test_1(self):
print('这是用例1')
if __name__ == '__main__':
pytest.main(['-s','test_28.py'])
自定义顺序
- 安装依赖包:pip install pytest-ordering
# -*- coding: utf-8 -*-
# @Time : 2021/10/23
# @Author : 大海
# @File : test_29.py
import pytest
# 使用装饰器@pytest.mark.run(order=num),按num数值顺序执行
class TestHomepage(object):
@pytest.mark.run(order=2)
def test_2(self):
print('这是用例2')
@pytest.mark.run(order=1)
def test_1(self):
print('这是用例1')
if __name__ == '__main__':
pytest.main(['-s', 'test_29.py'])