Pytest+Allure搭建接口框架(五)

前言:前面我们已经对pytest有个大致的了解了,现在我们开始深入的挖掘它一些其他功能吧!

三、进阶

A)玩转装饰器@pytest.fixture
主要作用:通常会被用于完成预置处理和重复操作。

@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)

常用参数:scope:被标记方法的作用域
function" (default):作用于每个测试方法,每个test都运行一次
"class":作用于整个类,每个class的所有test只运行一次
"module":作用于整个模块,每个module的所有test只运行一次
"session:作用于整个session,每个session只运行一次
params:(list类型)提供参数数据,供调用标记方法的函数使用
autouse:是否自动运行,默认为False不运行,设置为True自动运行

case1:@pytest.fixture(autouse=True)

import pytest


@pytest.fixture(autouse=True)
def must():
    print("======》first & must run")


class TestCase:

    def setup_class(self):
        print("=======>setup class")

    def teardown_class(self):
        print("=======>teardown class")

    def test_one(self):
        assert 0 == 1

    def test_twp(self):
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-s', 'test_report.py'])

结果如下:

test_report.py =======>setup class
======》first & must run
F======》first & must run
.=======>teardown class

你尝试下会发现标志了@pytest.fixture(autouse=True)会在每个测试函数运行前运行,且运行优先级高于setup/teardown函数而低于setup_class/teardown_class函数。

case2:@pytest.fixture() # 参数引用

import pytest


class TestCase:

    @pytest.fixture() 
    def para(self):
        print('=====>para func')
        return 1

    def test_one(self, para):  # 以变量形式传入
        a = para + 1
        assert a == 2


if __name__ == '__main__':
    pytest.main(['-s', 'test_report.py'])

运行结果如下:

test_report.py =====>para func
.

========================== 1 passed in 0.05 seconds ===========================

同时,被@pytest.fixture标记的函数也可以作返回值、函数引用... ...

case3:@pytest.fixture(scope="function")

import pytest


@pytest.fixture(scope='function', autouse=True)
def para():
    print('=====>para func')


class TestCase:
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        assert 2 == 2


def test_para():
    assert 0 != 0


if __name__ == '__main__':
    pytest.main(['-s', 'test_report.py'])

运行结果如下:

test_report.py =====>para func
F=====>para func
.=====>para func
F

建议大家使用上面的代码切换scope限制不同的区域,体验scope的效果。

case4:@pytest.fixture(params=[1,2,3])

import pytest


@pytest.fixture(params=[1, 2, 3])  # 注意此处必须是可迭代的参数
def data(request):
    print("=====>data")
    return request.param


class TestCas:
    def test_one(self, data):
        print("=====>test_one")
        assert data > 1


if __name__ == '__main__':
    pytest.main(['-s', 'test_report.py'])

运行结果为:

test_report.py =====>data
=====>test_one
F=====>data
=====>test_one
.=====>data
=====>test_one
.

进阶二、跳过待测试函数

主要作用:跳过标记的测试cases。

import pytest


a = 2


class TestCase:
    @pytest.mark.skip('跳过测试')
    def test_one(self):
        assert 1 == 2

    @pytest.mark.skipif(condition=a > 1, reason='若condition成立则跳过测试')
    def test_two(self):
        assert 2 == 2

    def test_para(self):
        assert 0 != 1


if __name__ == '__main__':
    pytest.main(['-s', 'test_report.py'])

运行结果为:

test_report.py ss.

进阶三、标记为预期失败的测试用例

主要作用:预先标记测试用例为失败的,方便局部执行。

import pytest


a = 2

class TestCase:
   @pytest.mark.xfail(condition=a > 1, reason='若condition成立则跳过测试')
   def test_two(self):
       assert 2 == 2

   def test_para(self):
       assert 0 != 1


if __name__ == '__main__':
   pytest.main(['-s', 'test_report.py'])

运行结果为:

test_report.py X.

进阶四、测试函数添加待测参数

主要作用:为待测试的case迭代多个参数,减少多数测试case的重复编写。

import pytest


class TestCase:
    @pytest.mark.parametrize('a', [1, 2])
    def test_two(self, a):
        print("====test data: %s" % a)
        assert 2 == 2

    def test_para(self):
        assert 0 != 1


if __name__ == '__main__':
    pytest.main(['-s', 'test_report.py'])

执行结果如下:

test_report.py ====test data: 1
.====test data: 2
.

总结

好了,pytest库就暂告一段落,接着我要开始重构我的测试框架了。期间时间可能稍微久一点,我还是会继续学习的~
加油!!!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容