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 =====================================================
相關文章
相關標籤/搜索