pytest基础

一、pytest默认的用例规范
  1. 用例文件:以test开头
  2. 类名称:以Test开头的类会被当成测试用例类
  3. 方法名称:以test开头的方法会被当成测试用例方法
二、用例的前后置方法
  1. 方式一:
python 复制代码
# 用例级别:setup、teardown
# 类级别:setup_class、teardown_class
class TestRegister:
    def setup(self):
        print("setup:每个用例开始前都会执行" + '11')

    def teardown(self):
        print("teardown:每个用例结束后都会执行" + '222')

    def setup_class(self):
        print('这是类的前置方法')

    def teardown_class(self):
        print('这是类的后置方法')

    def test_register(self):
        assert 100 == 100

    def test_register01(self):
        assert 66 == 55
  1. 方式二:
python 复制代码
# 通过pytest.fixture
# 定义用例级别的前后置
@pytest.fixture(scope='function')
def setup_case():
    print('这是用例的前置方法')

    yield

    print('这是用例的后置方法')

# 定义类级别的前后置
@pytest.fixture(scope='class')
def setupclass_case():
    print('这是类的前置方法')

    yield

    print('这是类的后置方法')

调用:

python 复制代码
# 1、在用例方法的参数中,写上前后置的方法名
class TestRegister01:
    def test_register(self, setup_case, setupclass_case):
        assert 100 == 99

    def test_register01(self, setup_case):
        assert 66 == 66
# 2、在定义前后置方法的时候(设置参数:autouse=Ture),可以设置为自动执行(用的较少)
三、用例执行的顺行
  1. 同一文件中的用例:按照用例的顺序执行
  2. 不同文件的执行顺行:按照文件的ascii码来执行
四、用例打标签
  1. 在pytest.ini文件中使用markets这个配置项注册标签
  2. 给用例加标签
python 复制代码
class TestDemo:
	# 1、通过pytestmark = [pytest.mark.max]给测试类中的所有测试方法打上该标签
	# 2、通过@pytest.mark.标签名给测试用例方法打标签
	pytestmark = [pytest.mark.zm]
    @pytest.mark.max
    @pytest.mark.min
    def testlogin00(self):
        assert 100 == 100

    @pytest.mark.max
    def testlogin01(self):
        assert 100 == 100
  1. 通过标签选择用例执行
bash 复制代码
# 命令行执行被标签max标记的测试用例
python -m max
# 运行run文件
python.main(['-m', 'max'])
# 筛选多个标签
python -m not 'max'
python -m 'max' and 'min'
python -m 'max' or 'min'
  1. 内置的标签
python 复制代码
class TestDemo:
    num = 100
    # 被skip标签标记的用例不会被执行
    @pytest.mark.skip
    def testlogin00(self):
        assert 100 == 100
    # skipif中的判断条件成立,则被skipif标签标记的用例不会被执行。注意:reason参数必须要有,不然会报错
    @pytest.mark.skipif(num == 100, reason='num不等于100')
    def testlogin04(self):
        assert 100 == 100
五、用例运行的方式
  1. 运行方式
bash 复制代码
命令行:pytest 参数
pytest.main运行:pytest.main([参数列表])
  1. 通过标签筛选用例执行
bash 复制代码
命令行:pytest -m 标签名
pytest.main运行:pytest.main(['-m', '标签名'])
  1. 筛选执行的用例
bash 复制代码
# 1、执行指定用例文件 pytest 文件名.py
# 2、执行指定用例目录 pytest 目录名
# 3、执行指定用例类 pytest 文件名.py::类名
# 4、执行指定用例方法 pytest 文件名.py::类名::方法名
六、断言

使用assert进行断言

七、参数化(等同于unittest中的ddt)
python 复制代码
# 用例方法添加@pytest.mark.parametrize('item', cases)
class TestLogin:
    cases = [0, 7 , 1, 4, 5]
    @pytest.mark.parametrize('item', cases)
    def testlogin(self, item):
        print(item)
八、测试报告(集成allure报告平台)
  1. 下载allure 进行解压
  2. 将解压后的allure中的bin目录路径配知道环境变量
  3. 安装allure-pytest插件
  4. 执行用例时加上参数 --alluredir=报告数据存放的目录
  5. 在命令行启动allure服务,获取报告
bash 复制代码
allure serve 报告数据的路径
九、失败重试
  1. 安装pytest-rerunfailures
  2. 命令行执行
bash 复制代码
pytest test.py --reruns 3 reruns-delay 1 --alluredir=allure_reports/
  1. 通过pytest.main执行
python 复制代码
pytest.main(['test_reporter.py', '--reruns', '3', '--reruns-delay', '1', '--alluredir=allure_reports/'])
相关推荐
努力搬砖的咸鱼2 天前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
FINE!(正在努力!)4 天前
PyTest框架学习
学习·pytest
程序员杰哥5 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥5 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻5 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
天才测试猿6 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
not coder7 天前
Pytest Fixture 详解
数据库·pytest
not coder7 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂7 天前
(1)pytest简介和环境准备
pytest
not coder7 天前
Pytest Fixture 是什么?
数据库·oracle·pytest