栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytest

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytest

1.安装pytest

pip install pytest
然后 pip list 进行查看

2.pytest入门案例

类名一定要以Test开头,里面的方法用小写的test开头

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

class TestAdd:

    def test_01(self):
        result = add(1, 2)
        print(result)

    def test_02(self):
        result = add(4, 2)
        print(result)
3.pytest断言
def add(x,y):
    return x+y

class TestAdd:

    def test_01(self):
        result = add(1, 2)
        print(result)
        # 断言 用assert关键字
        assert result == 3 # 判断相等
        assert result != 3 # 判断不相等
        assert result  # 判断为True
        assert False   # 判断为False
        assert "a" in "abc"  #判断包含
        assert "a" not in "abc"  #判断不包含
        assert result is None # 判断是否是空
        assert result is not None # 判断是否是空

    def test_02(self):
        result = add(4, 2)
        print(result)
pytest方法级别的fixture

就是在每一个方法前面加上setup和teardown的内容

import time


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

class TestAdd:

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

    def test_01(self):
        result = add(1, 2)
        print(result)


    def test_02(self):
        result = add(4, 2)
        print(result)

    def teardown(self):
        print("测试用例结束执行:", time.strftime("%Y-%m-%D %H-%m-%S"))

pytest配置文件

创建pytest.ini文件,tox.ini文件,setup.cfg中的一个

[pytest]# 这是pytest的配置文件
addopts = -s -v. #这是运行方式
testpaths = ./scripts# 这是运行文件的所在路径 只会运行这个文件下面的文件
python_files = test_*.py# 这是运行的文件
python_classes = Test*# 这是运行文件里面要运行的类
python_functions = test_*# 这是要运行类里面的所匹配的方法

运行直接选住项目右击运行或者在控制台输入pytest就可以

pytest类级别的fixture

setup_class和teardown_class

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 test_01(self):
        result = add(8, 2)
        print(result)


    def test_02(self):
        result = add(8, 3)
        print(result)

    def teardown_class(self):
        print("测试用例结束执行:", time.strftime("%Y-%m-%D %H-%m-%S"))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/869183.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号