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以及装饰器
发布评论