pytest的一些高阶用法

前言

之前一篇文章pytest的一些实用插件实践记录了一些实用的插件,这边记录一点pytest的高阶用法。

fixture

首先什么是fixture?

  • 1.为了测试用例的执行,进行一些初始化数据的操作
  • 2.实现了unittest中的setUp、tearDown功能

它的有点是什么?
实践下来,大致总结了几点:

  • 1.比unittest中的setUp、tearDown调用方便
  • 2.支持不同级别的fixture,默认是function级别
  • 3.调用方式灵活,支持直接函数名调用、装饰器调用、autouse

1.函数名直接调用

先看代码

@pytest.fixture()
def pre():
    print("beore test ")
    yield
    print("after test")

class Test1(object):
    def test_1(self,pre):
        print("tihs is test1")
        assert 1==1

    def test_2(self):
        //未传入pre
        print("this is test2")
        assert 1+2 ==3

其中test_2方法没有传入pre参数,执行脚本

pytest -sq demo_fixture.py

最终结果如下:


before.png

可以看出在test_1执行之前先执行 print("beore test ") ,结束后 执行 print("after test "),而test_2 由于没有传入pre参数,所以没有执行pre的方法

备注:

这边yield实现 setup和teardown的功能
即:
yield之前的语句,在class/function/module之前执行
yield之后的语句,在class/function/module执行完成之后执行

2.装饰器调用

代码如下:

import pytest

@pytest.fixture()
def pre():
    print("beore test ")
    yield
    print("after test")

class Test1(object):

    @pytest.mark.usefixtures('pre')
    def test_1(self):
        //装饰器调用
        print("tihs is test1")
        assert 1==1

    def test_2(selfs,pre):
        //函数名调用
        print("this is test2")
        assert 1+2 ==3

class Test2(object):
    def test_3(self):
        print("this is test3")
        assert  3+1 ==4

直接后结果如下:


image.png

可以看出,test_1 和 test_2 都执行了 pre里的方法,而test_3未调用,所以未执行

3.autouse调用

代码如下:

import pytest

@pytest.fixture(scope="class",autouse=True)
def preclass():
    print("beore class ")
    yield
    print("after class")

@pytest.fixture(scope="module",autouse=True)
def premodule():
    print("beore module ")
    yield
    print("after module")

@pytest.fixture(scope="function",autouse=True)
def pretest():
    print("beore function ")
    yield
    print("after function")

class Test1(object):

    def test_1(self):
        print("this is test1")
        assert 1==1

    def test_2(self):
        print("this is test2")
        assert 1+2 ==3

class Test2(object):
    def test_3(self):
        print("this is test3")
        assert  3+1 ==4

    def test_4(self):
        print("this is test4")
        assert  3+2 ==5

fixture默认的作用范围是function,同时也支持module/class/package/session级别


scope.png

上面我定义了三个级别的fixture,分别是module、class、function,可以预期的是 module方法只执行1次,class执行2次,function执行4次,结果如下:


autouse.png

这边对于平时接口测试或者UI自动化测试中,常用的login操作、数据库连接操作等非常实用。

4.fixture传入参数

需要注意的是,fixture传入参数,只能传入列表类型

params: an optional list of parameters which will cause multiple invocations of the fixture function and all of the tests using it.

代码如下:

import pytest

test_user_data = [{"user": "admin1", "passwd": "111111"},
                  {"user": "admin1", "passwd": "11234"}]

@pytest.fixture(scope="module",params=test_user_data)
def login(request):
    return request.param

def test_login(login):
    print(login["user"])

执行结果如下:


image.png

Demo代码地址请见github

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 初次接触测试框架的你,肯定希望能更快速的编写自己的测试代码,那么我们开始吧! 1.Pytest介绍 pytest是...
    白习习_c942阅读 12,047评论 0 15
  • 在pytest中加入fixture的目的是提供一个固定的基准,使测试能够可靠、重复地执行,pytest的fixtu...
    何小有阅读 14,608评论 1 17
  • 一. fixture介绍 fixture是pytest的一个闪光点,pytest要精通怎么能不学习fixture呢...
    Yating_Yang阅读 34,028评论 6 39
  • 这篇文章接着上一篇《探索pytest的fixture(上)》的内容讲。 使用fixture函数的fixture 我...
    何小有阅读 6,709评论 0 3
  • Startup 单元测试的核心价值在于两点: 更加精确地定义某段代码的作用,从而使代码的耦合性更低 避免程序员写出...
    wuwenxiang阅读 13,415评论 1 27

友情链接更多精彩内容