当前位置: 首页 > news >正文

新余市建设厅网站上海优化网站seo公司

新余市建设厅网站,上海优化网站seo公司,腾讯cdn加速优化wordpress,做网批有专门的网站吗?pytest pytest是python的一种单元测试框架,同自带的unit test测试框架类似,但pytest更简洁高效。 单元测试: 测试 函数、类、方法能不能正常运行测试的结果是否符合我们的预期结果 安装 pip install -U pytest基本使用 通过pytest包使用…

pytest

pytest是python的一种单元测试框架,同自带的unit test测试框架类似,但pytest更简洁高效。

单元测试:

  • 测试 函数、类、方法能不能正常运行
  • 测试的结果是否符合我们的预期结果

安装

pip install -U pytest

基本使用

  • 通过pytest包使用
import pytestdef test_a():print("test_a")return 1 + 0def test_b():print("test_b")return 1 / 0if __name__ == '__main__':pytest.main()

默认情况下:在main中直接使用pytest的main()方法,会把文件中所有test_*开头的方法执行一遍。

  • 通过终端的命令使用,到所在目录下执行
# pytest或加参数都可
pytest -s

单量执行测试文件

import pytestdef test_1():print("test_1+++")return 1 + 0def test_2():print("test_2--------")return 1 / 0if __name__ == '__main__':# 只运行 test_py2.py文件中的测试方法pytest.main(["-s", "test_py2.py"])

配置文件

测试自动触发规则:

  • 在测试目录中或当前的目录中寻找

  • 名称为 test_*.py*_test.py的文件

  • Test开头的类,且没有初始化__init__方法

  • 以上目录或类中,test开头的函数或方法

  • 会执行uinit test的测试用例类

运行pytest时,自动读取所在目录中的配置文件pytest.ini。在测试文件所在目录下创建一个pytest.ini

内容如下:注意:以下内容请将 中文全部删掉,否则可能出问题,这里只是为了解释配置的。

[pytest]
; ini文件中的英文分号,都是注释
addopts = -s   ;选项参数testpaths = ./   ;测试模块所在目录python_files = test_*.py *test.py  ;测试文件名称python_classes = Test_*  ;测试类名称规则python_functions = test_*  ;测试函数或者方法命名规则

假如我把python_functions修改为demo_*,那么只有以demo_函数名命名的函数才会被执行。

断言

import pytestdef test_1():print("test_1+++")assert 20 == 20def test_2():print("test_2--------")assert "a" in "hello"if __name__ == '__main__':pytest.main(["-s", "test_py2.py"])

标记

标记跳过测试

  • 标记跳过(装饰器)
  • 标记失败(装饰器)
@pytest.mark.skip("跳过")
def test_2():print("test_2--------")return 1 / 0@pytest.mark.xfail(raises=ZeroDivisionError)
def test_3():print("test_3--------")return 1 / 0

参数化

比如写了一个函数需要模拟一些参数进行调用,那么可以使用:

parametrize装饰器:

  • [“a”, “b”],列表中定义的方法参数名
  • [(1, 2), (2, 2), (50, 51)],三组测试数据,表示此方法会被调用3次
import pytest@pytest.mark.parametrize(["a", "b"], [(1, 2), (2, 2), (50, 51)])
def test_1(a, b):print("test_1+++++++")assert a + b > 100if __name__ == '__main__':pytest.main(["-s", "test_py3.py"])

夹具

在测试之前和之后执行,用于固定测试环境,及清理回收测试资源。

setup_...teardown_...

  • 模块的夹具:setup_module()和teardown_module(),在python文件加载前和文件内容结束后执行

    import pytestdef setup_module(args):print("setup_module", args)def teardown_module(args):print("teardown_module", args)def test_fun_a():print("------------", "test_fun_a")def test_fun_b():print("------------", "test_fun_b")class TestOne:def test_1(self):print("------", "test_1")def test_2(self):print("------", "test_2")if __name__ == '__main__':pytest.main(["-s", "test_py4.py"])###################################################结果########################
    test_py4.py setup_module <module 'test_py4' from 'D:\\environment\\python-workspace\\androidTest\\pytest\\test_py4.py'>
    ------------ test_fun_a
    .------------ test_fun_b
    .------ test_1
    .------ test_2
    .teardown_module <module 'test_py4' from 'D:\\environment\\python-workspace\\androidTest\\pytest\\test_py4.py'>
    
  • 函数的夹具:setup_function()和teardown_function(),py中函数执行前和执行后执行(注意是函数,不是类中的方法)

    import pytestdef setup_function(args):print("setup_function", args)def teardown_function(args):print("teardown_function", args)def test_fun_a():print("------------", "test_fun_a")def test_fun_b():print("------------", "test_fun_b")class TestOne:def test_1(self):print("------", "test_1")def test_2(self):print("------", "test_2")if __name__ == '__main__':pytest.main(["-s", "test_py4.py"])
    ###################################################结果########################test_py4.py setup_function <function test_fun_a at 0x000001ED1D8C31F8>
    ------------ test_fun_a
    .teardown_function <function test_fun_a at 0x000001ED1D8C31F8>
    setup_function <function test_fun_b at 0x000001ED1D8C3288>
    ------------ test_fun_b
    .teardown_function <function test_fun_b at 0x000001ED1D8C3288>
    ------ test_1
    .------ test_2
    
  • 类的夹具:setup_class()和teardown_class(),类被加载前和销毁后执行

    class TestOne:def setup_class(self):print("------", "setup___test_1")def teardown_class(self):print("------", "teardown___test_1")def test_1(self):print("------", "test_1")def test_2(self):print("------", "test_2")if __name__ == '__main__':pytest.main(["-s", "test_py4.py"])###################################################结果########################
    ------ setup___test_1
    ------ test_1
    .------ test_2
    .------ teardown___test_1
    
  • 方法的夹具:setup_method()和teardown_method(),类被加载前和销毁后执行

    class TestOne:def setup_class(self):print("------", "setup___test_1")def teardown_class(self):print("------", "teardown___test_1")def setup_method(self, args):print("------", "setup_methods___test_1", args)def teardown_method(self, args):print("------", "teardown_methods___test_1", args)def test_1(self):print("------", "test_1")def test_2(self):print("------", "test_2")if __name__ == '__main__':pytest.main(["-s", "test_py4.py"])###################################################结果########################
    setup___test_1
    ------ setup_methods___test_1 <bound method TestOne.test_1 of <test_py4.TestOne object at 0x000001D972FB28C8>>
    ------ test_1
    .------ teardown_methods___test_1 <bound method TestOne.test_1 of <test_py4.TestOne object at 0x000001D972FB28C8>>
    ------ setup_methods___test_1 <bound method TestOne.test_2 of <test_py4.TestOne object at 0x000001D972FB2988>>
    ------ test_2
    .------ teardown_methods___test_1 <bound method TestOne.test_2 of <test_py4.TestOne object at 0x000001D972FB2988>>
    ------ teardown___test_1

fixture装饰器夹具

import pytest# 设置夹具
@pytest.fixture()
def before():print("before")# 使用夹具
@pytest.mark.usefixtures("before")
def test_1():print("test_1执行")# 设置夹具 有返回值
@pytest.fixture()
def login():print("login")return "user"# 使用夹具 入参
def test_2(login):print("test_2执行")print(login)@pytest.fixture(params=[1, 2, 3])
def init_data(request):# params中有三个元素,那么此方法将执行三遍print("参数:", request.param)return request.paramdef test_data(init_data):assert init_data > 2if __name__ == '__main__':pytest.main(["-s", "test_py5.py"])
###################################################结果################################
test_py5.py 参数: 1
F参数: 2
F参数: 3
.before
test_1执行
.login
test_2执行
user================================== FAILURES ===================================
________________________________ test_data[1] _________________________________init_data = 1def test_data(init_data):
>       assert init_data > 2
E       assert 1 > 2test_py5.py:24: AssertionError
________________________________ test_data[2] _________________________________init_data = 2def test_data(init_data):
>       assert init_data > 2
E       assert 2 > 2test_py5.py:24: AssertionError
=========================== short test summary info ===========================
FAILED test_py5.py::test_data[1] - assert 1 > 2
FAILED test_py5.py::test_data[2] - assert 2 > 2
========================= 2 failed, 3 passed in 0.03s =========================

pytest插件

html报告

  • 安装插件

    pip install pytest-html
    
  • 使用

    • 命令行方式

      pytest --html=存储路径/report.html
      
    • 配置文件方式

      [pytest]
      addopts = -s --html=./report.html
      

指定运行顺序

  • 安装插件

    pip install pytest-ordering
    
  • 使用

    添加装饰器@pytest.mark.run(order=x)到测试函数或者方法上。

    优先级:

    • 0和正整数 > 没有标记 > 负整数标记
    • 且在各个阶段,数越小运行优先级越高
    import pytest@pytest.mark.run(order=0)
    def test_1():print("1")@pytest.mark.run(order=-3)
    def test_2():print("2")@pytest.mark.run(order=3)
    def test_3():print("3")@pytest.mark.run(order=2)
    def test_4():print("4")@pytest.mark.run(order=1)
    def test_5():print("5")if __name__ == '__main__':pytest.main(["-s", "test_py6.py"])###################################################结果###############################
    test_py6.py 1
    .5
    .4
    .3
    .2
    

失败重试

  • 安装插件

    pip install pytest-rerunfailures
    
  • 使用

    配置,--reruns 5 重试5次

    [pytest]
    addopts = -s --reruns 5
    
http://www.tj-hxxt.cn/news/126853.html

相关文章:

  • 网站制作的动画怎么做的网络推广公司哪里好
  • 上海做外贸网站的公司今天刚刚发生的新闻台湾新闻
  • 网站建设首先做网络推广一个月的收入
  • 做情侣网站重庆seo论
  • 工信部资质查询网站搜索关键词怎么让排名靠前
  • 网站建设百度资源网页百度
  • 代账公司网站模板长春百度seo排名
  • seo技术是什么意思关键词优化是怎样收费的
  • 怎么做公司的网站宣传目前最火的推广平台
  • 如何投诉网站制作公司网络推广属于什么专业
  • 常州做网站价格四种营销模式
  • 北京企业网站建设推荐篮网目前排名
  • wordpress页面导航条广州做seo整站优化公司
  • 铁岭做网站公司哪家好可以免费发外链的论坛
  • 做购实惠网站的意义产品推广渠道有哪些方式
  • 下载网站如何做什么叫关键词举例
  • 外挂网站怎么做微信营销策略
  • 网站内链如何布局互联网营销推广服务商
  • 学做ps的网站有哪些app线上推广是什么工作
  • 顺德网站建设报价广告安装接单app
  • 自己的网站怎么做团购山西免费网站关键词优化排名
  • 山西做网站的好消息疫情要结束了
  • wordpress 优秀站点百度官网认证
  • 呼和浩特市网站公司电话优化师是一份怎样的工作
  • php网站cms网上写文章用什么软件
  • 曹县有没有做网站做网站用什么编程软件
  • 网站模板一样侵权吗佛山企业用seo策略
  • 毕业论文代做网站是真的吗seo技术网
  • 天津网站建设开发有哪些永久不收费免费的聊天软件
  • 帝国cms灵动标签做网站地图黑帽seo是什么