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
原文地址:https://www.cnblogs.com/jingxindeyi/p/13348267.html