fixture是pytest的一個閃光點,pytest要精通怎麼能不學習fixture呢?跟着我一塊兒深刻學習fixture吧。其實unittest和nose都支持fixture,可是pytest作得更炫。 fixture是pytest特有的功能,它用pytest.fixture標識,定義在函數前面。在你編寫測試函數的時候,你能夠將此函數名稱作爲傳入參數,pytest將會以依賴注入方式,將該函數的返回值做爲測試函數的傳入參數。 fixture有明確的名字,在其餘函數,模塊,類或整個工程調用它時會被激活。 fixture是基於模塊來執行的,每一個fixture的名字就能夠觸發一個fixture的函數,它自身也能夠調用其餘的fixture。 咱們能夠把fixture看作是資源,在你的測試用例執行以前須要去配置這些資源,執行完後須要去釋放資源。好比module類型的fixture,適合於那些許多測試用例都只須要執行一次的操做。 fixture還提供了參數化功能,根據配置和不一樣組件來選擇不一樣的參數。 fixture主要的目的是爲了提供一種可靠和可重複性的手段去運行那些最基本的測試內容。好比在測試網站的功能時,每一個測試用例都要登陸和退出,利用fixture就能夠只作一次,不然每一個測試用例都要作這兩步也是冗餘。html
把一個函數定義爲Fixture很簡單,只能在函數聲明以前加上「@pytest.fixture」。其餘函數要來調用這個Fixture,只用把它當作一個輸入的參數便可。 test_fixture_basic.pypython
import pytest @pytest.fixture() def before(): print '\nbefore each test' def test_1(before): print 'test_1()' def test_2(before): print 'test_2()' assert 0
下面是運行結果,test_1和test_2運行以前都調用了before,也就是before執行了兩次。默認狀況下,fixture是每一個測試用例若是調用了該fixture就會執行一次的。數據庫
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -s test_fixture_basic.py ============================= test session starts ============================= platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe cachedir: .cache metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}} rootdir: C:\Users\PycharmProjects\pytest_example, inifile: plugins: metadata-1.3.0, html-1.14.2 collected 2 items test_fixture_basic.py::test_1 before each test test_1() PASSED test_fixture_basic.py::test_2 before each test test_2() FAILED ================================== FAILURES =================================== ___________________________________ test_2 ____________________________________ before = None def test_2(before): print 'test_2()' > assert 0 E assert 0 test_fixture_basic.py:12: AssertionError ===================== 1 failed, 1 passed in 0.23 seconds ======================
能夠用如下三種不一樣的方式來寫,我只變化了函數名字和類名字,內容沒有變。第一種是每一個函數前聲明,第二種是封裝在類裏,類裏的每一個成員函數聲明,第三種是封裝在類裏在前聲明。在能夠看到3中不一樣方式的運行結果都是同樣。 test_fixture_decorator.pysession
import pytest @pytest.fixture() def before(): print('\nbefore each test') @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_1()') @pytest.mark.usefixtures("before") def test_4(self): print('test_2()') @pytest.mark.usefixtures("before") class Test2: def test_5(self): print('test_1()') def test_6(self): print('test_2()')
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()')
4、 fixture 返回值學習
在上面的例子中,fixture返回值都是默認None,咱們能夠選擇讓fixture返回咱們須要的東西。若是你的fixture須要配置一些數據,讀個文件,或者鏈接一個數據庫,那麼你可讓fixture返回這些數據或資源。測試
如何帶參數 fixture還能夠帶參數,能夠把參數賦值給params,默認是None。對於param裏面的每一個值,fixture都會去調用執行一次,就像執行for循環同樣把params裏的值遍歷一次。 test_fixture_param.py網站
import pytest @pytest.fixture(params=[1, 2, 3]) def test_data(request): return request.param def test_not_2(test_data): print('test_data: %s' % test_data) assert test_data != 2