0四、调用fixture的三种方式

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

import pytest

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

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

二、用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()')

运行结果:html

╰ 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

三、用autoues调用fixture

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

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介绍)session

╰ 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 =====================================================
相关文章
相关标签/搜索