pytest的conftest使用


項目test下的目錄結構

 

全局conftest.py

import pytest
# 當autouse=False時,測試用例需要傳入參數,為True時,不需要傳入參數
@pytest.fixture(scope="session",autouse=True)
def start():
    print('\n打開首頁')
    # flag = 1
    # yield flag
    # print('退出登錄')

suite1/conftest.py

import pytest
# 當autouse=False時,測試用例需要傳入參數,為True時,不需要傳入參數
@pytest.fixture(scope="module")
def open_web1():
    print('\n打開頁面1')

suite1/test_1.py

import pytest
def test_03(start,open_web1):
    print('測試用例3操作')
def test_04(start,open_web1):
    print('測試用例4操作')

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

suite1/test_2.py

import pytest
def test_05(start,open_web1):
    print('測試用例5操作')
def test_06(start,open_web1):
    print('測試用例6操作')

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

suite2/conftest.py

import pytest
# 當autouse=False時,測試用例需要傳入參數,為True時,不需要傳入參數
@pytest.fixture(scope="function")
def open_web2():
    print('\n打開頁面2')

suite2/test_3.py

import pytest
def test_07(start,open_web2):
    print('測試用例7操作')
def test_08(start,open_web2):
    print('測試用例8操作')

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

suite2/test_4.py

import pytest
def test_09(start,open_web2):
    print('測試用例9操作')
def test_10(start,open_web2):
    print('測試用例10操作')
if __name__ == '__main__':
    pytest.main(["-s", "test_4.py"])

執行測試

 

 

 


免責聲明!

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



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