前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

pytest

作者头像
打铁读书郎
发布2024-05-06 09:42:23
660
发布2024-05-06 09:42:23
举报

Pytest测试框架

  • 日志模块: pytest.ini 配置日志, requests封装logging模块可以实现
  • 通知机器人: 钉钉获取webhook链接, jenkins安装dingtalk插件, 配置webhook
  • 其他特性- 钩子函数: 应用场景**pytest_terminal_summary**实现测试结果收集
  • 辅助函数, 环境变量: fixture函数

命令行参数

代码语言:javascript
复制
# -x
python -m pytest    # 全部执行
python -m pytest  -x     # 1次失败停止
python -m pytest  --maxfail 2    # 2次失败停止
# -k
python -m pytest -k 'Login and not ft' # 执行Login相关的测试用例, 单不执行带有ft的
# -q
python -m pytest -q   # 控制条输出简化信息
# -v
python -m pytest -v    # 控制台输出详细信息
# 指定路径执行
python -m pytest .\testcases\trina_union\test_m2.py::Test1::test_m1
# -m
python -m pytest -m pre #只执行带有pre标记的
# --lf 
python -m pytest --lf    # 只运行上次失败的用例
# -n
python -m pytest -n 5    # 多线程执行, 依赖pytest-xdist
# --reruns
python -m pytest --reruns 2    # 失败重试, 依赖pytest-rerunfailures
# -s
python -m putest -s # 显示print打印内容
# --pdb
python -m pytest --pdb # 用例失败进入pdb模式, 可查看变量

# --html
python -m pytest .\testcases\ --html  ./reports/report.html # 生成html测试报告(静态简陋)

# --alluredir
pytest testcases --alluredir=./reports"    # 生成allure报告

Setup_teardown

代码语言:javascript
复制
# 函数级别的初始化和收尾
class Test1:
    def teardown_method(self):
        logging.info("TearDown----")

    def setup_method(self):
        logging.info('Test1---setup_method')
# 类级别的初始化和收尾
class Test1:

    def setup_class():
        logging.info("setup_class----")

    def teardown_class():
        logging.info("teardown_class------")
        
# 模块级别的初始化和收尾
def setup_module():
    logging.info("setup_module----")

def teardown_module():
    logging.info("teardown_module------")

fixture

  • 设置在conftest.py或者测试用例文件中
  • 用于setip, teardown及返回测试数据
代码语言:javascript
复制
# scope: 影响层级
# autouse: 自动生效,无需测试用例引用
@pytest.fixture(scope='function', autouse=True)
def f1():
    logging.info("------fixture setup-------")
    yield 'fixture'
    logging.info("------fixture teardown-------")

class Test1:
    def test_m2(self, f1):
        logging.info(f1)    # 通过fixture函数名获取 yield返回值 'fixture'

@pytest.mark.usefixtures('f2')    # 引用fixture的方式2
def test_m1(self):
    logging.info('Exe-----testm2')
    assert 1==1

# params: 数据驱动, 循环返回列表数据
@pytest.fixture(params=[1,2,3])
def fp(request):
    yield request.param
   
class Test2:
    def test_m1(self, fp):
        logging.info(fp)    # 执行三次, 分别获取到 1, 2, 3

装饰器

代码语言:javascript
复制
# 数据驱动parametrize
class Test1:
    @pytest.mark.parametrize("v1, v2",
                             [
                                 (1, 9),
                                 (2, 8),
                                 (3, 7)
                             ])
    def test_m2(self, f1, v1, v2):
        logging.info(f1)
        logging.info(f"v1:{v1},v2:{v2}")
        assert 1==1
# 跳过 skip
@pytest.mark.skip(reason='for test')
def test_m1(self):
    logging.info('Exe-----testm2')
    assert 1==1
    
# 条件跳过skipif
@pytest.mark.skipif(condition='check_db(1)')
def test_m1(self):
    logging.info('Exe-----test_m1')
    
# mark: 用例标记
@pytest.mark.pre
class Test2:
    def test_m1(self):
        logging.info('Exe-----test_m1')     
# xfail 预期失败用例
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-01-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Pytest测试框架
    • 命令行参数
      • Setup_teardown
        • fixture
          • 装饰器
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档