pytest框架使用示例

  1. 模块:pytest
    安装:pip3 install pytest
    导入模块:import pytest
  2. 基本使用方法:
    2.1说明:
    2.1.1函数名必须以test开头
    2.1.2类名必须以Test开头,类中的方法需要以test开头
    2.1.2.1类中不能有构造方法
    2.1.3使用assert进行断言
    2.1.4脚本名必须以test开头
    2.2作用于函数
import pytest


def test_1():
    assert 1 == 1


def atest_1():
    print(123)
    assert 1 == 1


def test_1():
    assert 12 > 100


if __name__ == '__main__':
    # pytest.main(['-s', __file__])
    pytest.main(['-v', 'test_pytest/test_1_func.py'])
    pytest.main(['-q', 'test_pytest/test_1_func.py'])

2.3. 作用于类

import pytest


class Test_Add:
    # def __init__(self):
    #     pass

    def test(self):
        assert 12 > 100

    def atest(self):
        assert 12 > 100


if __name__ == '__main__':
    pytest.main(['-v', __file__])
	2.4. 用例执行

方式
脚本:pytest.mian([参数1,参数2,参数。。。])

命令行
pytest 参数 脚本
pytest 参数 脚本
python -m pytest 参数脚本

参数
-s,
-v,
-q,
-k,通过关键字匹配脚本、函数名、类名、方法名
-x,如果测试执行过程中有fail的用例,则测试立即停止
–maxfile=n,当fail用例达到指定的数量,则退出测试
-m,对用例进行标记,执行指定的用例

1.在项目根目录下新建文件pytest.ini
2.在pytest.ini文件中添加标记

[pytest]
markers = a
          b
          c
          smoke_case

c. 使用装饰器标记测试用例@pytest.mark.xxx

@pytest.mark.a
def test_1():
    assert 1 == 1


@pytest.mark.b
def test_2():
    print(123
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值