04、调用fixture的三种方式

1、在测试用例中直接调用它,并当成参数使用
import pytest

@pytest.fixture()
def postcode():
	return "027"

def test_postcode(postcode):
	assert "027" == postcode

2、用fixture decorator调用fixture
import pytest


@pytest.fixture()
def before():
    print('\nbefore each test')


# 第一种是每个函数前声明.调用函数前先调用before函数
@pytest.mark.usefixtures("before")
def test_1():
    print('test_1()')


@pytest.mark.usefixtures("before")
def test_2():
    print('test_2()')


# 第二种是封装在类里,类里的每个成员函数声明
class Test1:
    @pytest.mark.usefixtures("before")
    def test_3(self):
        print('test_3()')

    @pytest.mark.usefixtures("before")
    def test_4(self):
        print('test_4()')

# 第三种是封装在类里在前声明
@pytest.mark.usefixtures("before")
class Test2:
    def test_5(self):
        print('test_5()')

    def test_6(self):
        print('test_6()')

运行结果:

╰ pytest -v -s ./test_data.py
======================================================= test session starts ========================================================
platform darwin -- Python 3.7.4, pytest-4.4.0, py-1.8.0, pluggy-0.13.0 -- /Users/zhouwanghua/Code/Leyan/python/robocop/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Darwin-18.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '4.4.0', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'bdd': '3.1.0', 'html': '1.20.0', 'metadata': '1.8.0'}}
rootdir: /Users/zhouwanghua/Code/Leyan/robocop, inifile: pytest.ini
plugins: bdd-3.1.0, html-1.20.0, metadata-1.8.0
collected 6 items                                                                                                                  

test_data.py::test_1 
before each test
test_1()
PASSED
test_data.py::test_2 
before each test
test_2()
PASSED
test_data.py::Test1::test_3 
before each test
test_3()
PASSED
test_data.py::Test1::test_4 
before each test
test_4()
PASSED
test_data.py::Test2::test_5 
before each test
test_5()
PASSED
test_data.py::Test2::test_6 
before each test
test_6()
PASSED
3、用autoues调用fixture

fixture decorator一个optional的参数是autouse, 默认设置为False。 当默认为False,就可以选择用上面两种方式来试用fixture。 当设置为True时,在一个session内的所有的test都会自动调用这个fixture。 权限大,责任也大,所以用该功能时也要谨慎小心。

import time
import pytest

@pytest.fixture(scope="module", autouse=True)
def mod_header(request):
    print('\n-----------------')
    print('module      : %s' % request.module.__name__)
    print('-----------------')

@pytest.fixture(scope="function", autouse=True)
def func_header(request):
    print('\n-----------------')
    print('function    : %s' % request.function.__name__)
    print('time        : %s' % time.asctime())
    print('-----------------')

def test_one():
    print('in test_one()')

def test_two():
    print('in test_two()')

从下面的运行结果,可以看到mod_header在该module内运行了一次,而func_header对于每个test都运行了一次,总共两次(原因见scope介绍)

╰ pytest -v -s ./test_data.py
======================================================= test session starts ========================================================
platform darwin -- Python 3.7.4, pytest-4.4.0, py-1.8.0, pluggy-0.13.0 -- /Users/zhouwanghua/Code/Leyan/python/robocop/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Darwin-18.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '4.4.0', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'bdd': '3.1.0', 'html': '1.20.0', 'metadata': '1.8.0'}}
rootdir: /Users/zhouwanghua/Code/Leyan/robocop, inifile: pytest.ini
plugins: bdd-3.1.0, html-1.20.0, metadata-1.8.0
collected 1 item                                                                                                                   

test_data.py::test_two 
-----------------
module      : test_data
-----------------

-----------------
function    : test_two
time        : Thu Sep 26 18:57:43 2019
-----------------
in test_two()
PASSED

===================================================== 1 passed in 0.03 seconds =====================================================
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值