pytest以及装饰器

编程入门 行业动态 更新时间:2024-10-11 09:23:18

<a href=https://www.elefans.com/category/jswz/34/1767678.html style=pytest以及装饰器"/>

pytest以及装饰器

文章目录

  • 1 装饰器
  • 2 pytest
    • 2.1 pytest.ini

1 装饰器

这里我就不班门弄斧了。bilibili的这门50分钟的讲义很用心。有编程基础,很快就看明白了!
大概意思就是:一个函数上面,有@修饰符,那么这个函数就被装饰器装饰了。那么,这个函数会作为一个参数,传入到装饰器指定的函数中,所以这个函数真正执行时,实际上是一个增强版本的函数。
在第一次调用的时候做增强,之后每次使用,都是增强后的函数了。最常见的情况就是作为一个测时间的函数。

2 pytest

pytest和装饰器有什么关系呢?
用法也相当复杂。这里只介绍一种:

import pytest
def pointchange(point, change):x, y = pointx += changey += changereturn (x, y)
#fixture函数标记某函数是专门生产数据的函数
@pytest.fixture
def supply_point():return (1, 2)#pytest.mark用来对测试分类
#在其他测试函数,可以直接调用fixture函数生成的数据
#@pytest.mark.###    这个###可以在`pytest.ini`中自定义一些标记类型
#up和down就是自己定义的
@pytest.mark.up
def test_1(supply_point):assert pointchange(supply_point, 1) == (2, 3)@pytest.mark.up
def test_2(supply_point):assert pointchange(supply_point, 5) == (6, 7)@pytest.mark.up
def test_3(supply_point):assert pointchange(supply_point, 100) == (101, 102)@pytest.mark.down
def test_4(supply_point):assert pointchange(supply_point, -5) == (-4, -3)# 有一些测试,我们出于某种原因,可以在这次不进行测试。
@pytest.mark.skip
def test_5(supply_point):assert False == True, "This test is skipped"# 有一些测试,bug没修好,测试一定会失败,可以标注一下
@pytest.mark.xfail
def test_6(supply_point):assert False == True, "This test's output is muted"

2.1 pytest.ini

[pytest]
markers =up: mark a test as a webtest.down:down class

更多推荐

pytest以及装饰器

本文发布于:2024-02-07 02:12:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1752611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:pytest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!