pytest文檔25-conftest.py作用范圍


前言

一個測試工程下是可以有多個conftest.py的文件,一般在工程根目錄放一個conftest.py起到全局作用。
在不同的測試子目錄也可以放conftest.py,作用范圍只在該層級以及以下目錄生效。

conftest層級關系

在web_conf_py項目工程下建兩個子項目baidu、blog,並且每個目錄下都放一個conftest.py和__init__.py(python的每個package必須要有__init__.py)

web_conf_py是工程名稱

├─baidu
│  │  conftest.py
│  │  test_1_baidu.py
│  │  __init__.py
│  
│          
├─blog
│  │  conftest.py
│  │  test_2_blog.py
│  │  __init__.py
│   
│  conftest.py
│  __init__.py
        

案例分析

web_conf_py工程下conftest.py文件代碼案例

# web_conf_py/conftest.py
import pytest

@pytest.fixture(scope="session")
def start():
    print("\n打開首頁")

baidu目錄下conftest.py和test_1_baidu.py

# web_conf_py/baidu/conftest.py
import pytest

@pytest.fixture(scope="session")
def open_baidu():
    print("打開百度頁面_session")


# web_conf_py/baidu/test_1_baidu.py

import pytest

def test_01(start, open_baidu):
    print("測試用例test_01")
    assert 1

def test_02(start, open_baidu):
    print("測試用例test_02")
    assert 1

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

運行test_1_baidu.py結果可以看出,start和open_baidu是session級別的,只運行一次

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py\baidu, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 2 items

test_1_baidu.py 
打開首頁
打開百度頁面_session
測試用例test_01
.測試用例test_02
.

========================== 2 passed in 0.01 seconds ===========================

blog目錄下conftest.py和test_2_blog.py代碼

# web_conf_py/blog/conftest.py
import pytest

@pytest.fixture(scope="function")
def open_blog():
    print("打開blog頁面_function")


# web_conf_py/blog/test_2_blog.py

import pytest

def test_03(start, open_blog):
    print("測試用例test_03")
    assert 1

def test_04(start, open_blog):
    print("測試用例test_04")
    assert 1

def test_05(start, open_baidu):
    '''跨模塊調用baidu模塊下的conftest'''
    print("測試用例test_05,跨模塊調用baidu")
    assert 1

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

運行結果可以看出,start起到全局作用,blog目錄下的open_blog是function級別,每個用例調用一次。
test_05(start, open_baidu)用例不能跨模塊調用baidu模塊下的open_baidu,所以test_05用例會運行失敗

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py\blog, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 3 items

test_2_blog.py 
打開首頁
打開blog頁面_function
測試用例test_03
.打開blog頁面_function
測試用例test_04
.E

=================================== ERRORS ====================================
__________________________ ERROR at setup of test_05 __________________________
file E:\YOYO\web_conf_py\blog\test_2_blog.py, line 11
  def test_05(start, open_baidu):
E       fixture 'open_baidu' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, metadata, monkeypatch, open_blog, pytestconfig, record_property, record_xml_attribute, record_xml_property, recwarn, start, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

E:\YOYO\web_conf_py\blog\test_2_blog.py:11
====================== 2 passed, 1 error in 0.02 seconds ======================

---------------------------------pytest結合selenium自動化完整版-------------------------

全書購買地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

作者:上海-悠悠 QQ交流群:874033608

也可以關注下我的個人公眾號:yoyoketang


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM