pytest 配置文件

使用配置文件后可以快速的使用配置的项来选择执行哪些测试模块

使用方法:

  1. 项目下新建scripts模块
  2. 将测试脚本文件放到scripts中
  3. pytest的配置文件放到自动化项目目录下
  4. 名称为pytest.ini
  5. 命令行运行时会使用该配置文件中的配置
  6. 第一行内容为[pytest]

配置文件参数说明

[pytest]   # 标识当前配置文件是pytest的配置文件

addopts = -s -v # 标识pytest执行时增加的参数

testpaths = ./scripts # 匹配搜索的目录

python_files = test_*.py # 匹配测试文件

python_classes = Test* # 匹配测试类

python_functions = test_* # 匹配测试方法

[pytest]
addopts = -s -v
testpaths = ./scripts
python_files = test_*.py
python_classes = Test*
python_functions = test_*

配置文件的命名:pytest.ini tox.ini setup.cfg

测试代码

import time


def add(x, y):
    return x+y


class TestAdd:
    # 添加类级别的初始化操作方法
    def setup_class(self):
        print("测试类开始执行时间:",  time.strftime("%Y-%m-%d %H:%M:%S"))

    # 添加类级别的销毁操作方法
    def teardown_class(self):
        print("测试类结束执行时间:", time.strftime("%Y-%m-%d %H:%M:%S"))

    def setup(self):
        print("测试用例开始执行时间:", time.strftime("%Y-%m-%d %H:%M:%S"))

    def test_add_01(self):
        result = add(1, 2)
        assert result == 3

    def test_add_02(self):
        result = add(2, 2)
        assert result == 4

    def teardown(self):
        print("测试用例结束时间:", time.strftime("%Y-%m-%d %H:%M:%S"))

结果

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值