pytest-配置文件


pytest-配置文件

conftest.py

如果希望多個測試文件共享fixture,可以在公共目錄下新建一個conftest.py文件,將fixture放在其中

例:
目錄結構:

.
├── conftest.py
├── __init__.py
├── test_001.py
└── test_002.py
#conftest.py
import pytest
 
@pytest.fixture(scope="session",autouse="True")
def sess_scope():
    print('----------開始session-------------')
    yield
    print('----------結束session-------------')

@pytest.fixture(scope="module",autouse="True")
def mod_scope():
    print('----------開始module-------------')
    yield
    print('----------結束module-------------')

@pytest.fixture(scope="class",autouse="True")
def class_scope():
    print('----------開始class-------------')
    yield
    print('----------結束class-------------')

@pytest.fixture(scope="function",autouse="True")
def fun_scope():
    print('----------開始function-------------')
    yield
    print('----------結束function-------------')
#test_001.py
#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest 

def test_one():
    print("函數用例 1")

class Test_Class1():
    def test_two(self):
        print("類 1 用例 2")
    
    def test_three(self):
        print("類 1 用例 3")

def test_four():
    print("函數用例 4")
#test_002.py
#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest 

def test_1():
    print("函數用例 5")

class Test_Class2():
    def test_6(self):
        print("類 2 用例 6")
    
    def test_7(self):
        print("類 2 用例 7")

def test_4():
    print("函數用例 8")
==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 8 items                                                                            

test_001.py ----------開始session-------------
----------開始module-------------
----------開始class-------------
----------開始function-------------
函數用例 1
.----------結束function-------------
----------結束class-------------
----------開始class-------------
----------開始function-------------
類 1 用例 2
.----------結束function-------------
----------開始function-------------
類 1 用例 3
.----------結束function-------------
----------結束class-------------
----------開始class-------------
----------開始function-------------
函數用例 3
.----------結束function-------------
----------結束class-------------
----------結束module-------------

test_002.py ----------開始module-------------
----------開始class-------------
----------開始function-------------
函數用例 5
.----------結束function-------------
----------結束class-------------
----------開始class-------------
----------開始function-------------
類 2 用例 6
.----------結束function-------------
----------開始function-------------
類 2 用例 7
.----------結束function-------------
----------結束class-------------
----------開始class-------------
----------開始function-------------
函數用例 8
.----------結束function-------------
----------結束class-------------
----------結束module-------------
----------結束session-------------


===================================== 8 passed in 0.03s ======================================

特性:

conftest.py文件名固定,不可修改
conftest.py供其所在目錄及子目錄使用
conftest.py文件所在目錄必須存在__init__.py文件
conftest.py文件不能被導入。`import conftest`的用法時不允許的

pytest.ini

pytest.ini是pytest的主配置文件,可以改變pytest的默認行為。

更改默認命令行選項

[pytest]
addopts = -rsxX -l --tb=short -strict

注冊標記

[pytest]
markers = 
    smoke: run mark smoke
    get: run mark get

指定pytest的最低版本號

[pytest]
minversion = 3.0

指定pytest忽略某些目錄

[pytest]
norecursedirs = .* venv src *.egg dist build

指定測試目錄

[pytest]
testpaths=testpath

更改測試搜索規則

[pytest]
python_classes = *Test Test* *Suite
python_files=test_* *_test check_*
python_functions = test_* *_test check_*

禁用XPASS

[pytest]
xfail_strict = true


免責聲明!

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



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