首页 > 其他分享 >pytest-fixture配置运行时(conftest.py)

pytest-fixture配置运行时(conftest.py)

时间:2022-10-26 18:13:46浏览次数:34  
标签:function ... py fixture conftest pytest test before

conftest.py

通过yield分隔执行前和执行后运行

session:整个用例执行前后                               

module:py文件执行前后                                  

class:类前后执行                                             

function:每个方法前后                               

 

 conftest.py 的文件名称是固定的, pytest 会自动识别该文件,我们可以理解成一个专门存放 fixture 的配置文件。

一个工程下可以建多个 conftest.py 文件,一般我们都是在工程根目录下设置的 conftest 文件,这样会起到一个全局的作用。 我们也可以在不同的子目录下放 conftest.py ,这样作用范围只能在该层级的子目录下生效。

 1 import pytest
 2 @pytest.fixture(scope='session',autouse=True)
 3 def session():
 4     print('before session...')
 5     yield
 6     print('after session...')
 7 
 8 @pytest.fixture(scope='module',autouse=True)
 9 def module():
10     print('before module...')
11     yield
12     print('after module...')
13 
14 @pytest.fixture(scope='class',autouse=True)
15 def classdemo():
16     print('before class...')
17     yield
18     print('after class...')
19 
20 
21 @pytest.fixture(scope='function',autouse=True)
22 def func():
23     print('before function...')
24     yield
25     print('after function...')

D:\tools\pycharm\pythonProject1>pytest -v 自动化 -s
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.8.0, pytest-7.1.3, pluggy-1.0.0 -- d:\tools\python3.8\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '7.1.3', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'html': '3.1.1', 'metadata': '2.0.2'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jdk
1.8.0_111'}
rootdir: D:\tools\pycharm\pythonProject1
plugins: html-3.1.1, metadata-2.0.2
collected 5 items

自动化/fixture/test_fixture.py::test_pay before session...
before module...
before class...
before function...
PASSEDafter function...
after class...
after module...

自动化/测试pytest参数/test_demo.py::TestDemo::test_one before module...
before class...
before function...
Hello test Demo one
PASSEDafter function...

自动化/测试pytest参数/test_demo.py::TestDemo::test_two before function...
FAILEDafter function...

自动化/测试pytest参数/test_demo.py::TestDemo::test_three before function...
PASSEDafter function...

自动化/测试pytest参数/test_demo.py::TestDemo::test_four before function...
FAILEDafter function...
after class...
after module...
after session...


================================================================================================================ FAILURES ================================================================================================================
___________________________________________________________________________________________________________ TestDemo.test_two ____________________________________________________________________________________________________________

self = <test_demo.TestDemo object at 0x00000000037436A0>

def test_two(self):
> assert False
E assert False

自动化\测试pytest参数\test_demo.py:9: AssertionError
___________________________________________________________________________________________________________ TestDemo.test_four ___________________________________________________________________________________________________________

self = <test_demo.TestDemo object at 0x0000000003743A60>

def test_four(self):
> assert False
E assert False

自动化\测试pytest参数\test_demo.py:15: AssertionError
======================================================================================================== short test summary info =========================================================================================================
FAILED 自动化/测试pytest参数/test_demo.py::TestDemo::test_two - assert False
FAILED 自动化/测试pytest参数/test_demo.py::TestDemo::test_four - assert False
====================================================================================================== 2 failed, 3 passed in 0.51s =======================================================================================================

D:\tools\pycharm\pythonProject1>

标签:function,...,py,fixture,conftest,pytest,test,before
From: https://www.cnblogs.com/yitian395/p/16829413.html

相关文章

  • pycharm设置文件模板
      模板样例:#!/usr/bin/python#-*-coding:UTF-8-*-"""@author:${USER}@Description:描述@file:${NAME}.py@time:${YEAR}/${MONTH}/${DAY}"""if__na......
  • python进程
    1、进程概念进程是一个执行中的程序,资源分配的最小单位。每个进程都拥有自己的地址空间、内存、数据栈以及其他用于跟踪执行的辅助数据。在单核CPU系统中的多进程,内存中可......
  • Python基础21
    今日内容概要第三方模块的下载与使用网络爬虫模块值requests模块网络爬虫实战之爬取链家二手房数据自动化办公领域之openpyxl模块今日内容详细第三方模块的下载与......
  • 转 python 自动监控表空间,并自动添加数据文件
    侯志清-江西南昌 python自动监控表空间,并自动添加数据文件#!/usr/bin/pythonimportosimporttimeimportlinecache#定义记录日志文件defrlog(log)......
  • python进程
    1、概念进程是一个执行中的程序,资源分配的最小单位。每个进程都拥有自己的地址空间、内存、数据栈以及其他用于跟踪执行的辅助数据。在单核CPU系统中的多进程,内存中可以有......
  • websocket接口自动化集成pytest测试框架
    websocket协议1、介绍WebSocket是一种在单个TCP通信的协议。WebSocket通信协议于2011年被IETF定为标准RFC6455,并由RFC7936补充规范。WebSocketAPI也被W3C定为标准。We......
  • python切片数组越界?
    1.在对list进行切片时,如x[9:12],若len(x)=10,只会返回x[9],而不会像其他语言直接数组越界错误。x=[iforiinrange(10)]print(x)foriinrange(0,10,3):print(x[i:i+3]......
  • Java开发笔记之Java开发笔记之Parallels Desktop提示This copy of Parallels Desktop
    使用学习版的ParallelsDesktop时候,win会出现以下提示;PD发现你用了学习版本,没有缴费,进行了“温馨提示”; 以上提示一般出现在安装ParallelsTools之后,打开PD虚拟机中的W......
  • 图像处理:opencv-python给图像加文字
    1.起因计算机视觉中常常会需要在图片上加文字,这样可以增强图像数据的可读性。2.工具opencv-python3.方法importmatplotlib.pyplotaspltfromPILimportImag......
  • python3 使用位图排序
    代码frombitmapimportBitMapa=[1,5,3,4,7,8,15,6,9]print(a)bm=BitMap(max(a))#print(dir(bm))print(bm.tostring())foriina:bm.set(i)print(bm......