python学习-pytest(二)-conftest.py

一、conftest特点:

1、可以跨.py文件调用,有多个.py文件调用时,可让conftest.py只调用了一次fixture,或调用多次fixture

2、conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件

3、不需要import导入 conftest.py,pytest用例会自动识别该文件,放到项目的根目录下就可以全局目录调用了,如果放到某个package下,那就在改package内有效,可有多个conftest.py

4、conftest.py配置脚本名称是固定的,不能改名称

5、conftest.py文件不能被其他文件导入

6、所有同目录测试文件运行前都会执行conftest.py文件

二、’conftest用法:

conftest文件实际应用需要结合fixture来使用,fixture中参数scope也适用conftest中fixture的特性,这里再说明一下

1、fixture源码详解

fixture(scope=‘function‘,params=None,autouse=False,ids=None,name=None):
fixture里面有个scope参数可以控制fixture的作用范围,scope:有四个级别参数"function"(默认),"class","module","session

params:一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它。
autouse:如果True,则为所有测试激活fixture func可以看到它。如果为False则显示需要参考来激活fixture
ids:每个字符串id的列表,每个字符串对应于params这样他们就是测试ID的一部分。如果没有提供ID它们将从params自动生成
name:fixture的名称。这默认为装饰函数的名称。如果fixture在定义它的统一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽,解决这个问题的一种方法时将装饰函数命令"fixture_<fixturename>"然后使用"@pytest.fixture(name=‘<fixturename>‘)"。

2、fixture的作用范围

fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function

-function:每一个函数或方法都会调用

-class:每一个类调用一次,一个类中可以有多个方法

-module:每一个.py文件调用一次,该文件内又有多个function和class

-session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module

function默认模式@pytest.fixture(scope=‘function‘)或 @pytest.fixture()

3、conftest结合fixture的使用

conftest中fixture的scope参数为session,所有测试.py文件执行前执行一次

conftest中fixture的scope参数为module,每一个测试.py文件执行前都会执行一次conftest文件中的fixture

conftest中fixture的scope参数为class,每一个测试文件中的测试类执行前都会执行一次conftest文件中的fixture

conftest中fixture的scope参数为function,所有文件的测试用例执行前都会执行一次conftest文件中的fixture

三、conftest应用场景

1、每个接口需共用到的token

2、每个接口需共用到的测试用例数据

3、每个接口需共用到的配置信息

....
四、代码实例

#多个.py文件只调用1次fixture

import pytest
# conftest.py
@pytest.fixture(scope=‘session‘)
def get_token():
    token = ‘[email protected]‘
    print(‘获取到token:%s‘ % token)
    return token

import pytest
# test02.py
class Test(object):
    def test2(self,get_token):
        token = ‘[email protected]‘
        print("【执行test02.py-Test类-test2用例,获取get_token:%s】" %get_token)
        assert get_token == token

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

import pytest
#test03.py
class Test(object):
    def test3(self,get_token):
        token = ‘[email protected]‘
        print("【执行test03.py-Test类-test3用例,获取get_token:%s】" %get_token)
        assert get_token == token
    def test4(self,get_token):
        token = ‘[email protected]‘
        print("【执行test03.py-Test类-test4用例,获取get_token:%s】" %get_token)
        assert get_token == token

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

"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test02.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items

test02.py 获取到token:[email protected]22
【执行test02.py-Test类-test2用例,获取get_token:[email protected]】
.
test03.py 【执行test03.py-Test类-test3用例,获取get_token:[email protected]】
.【执行test03.py-Test类-test4用例,获取get_token:[email protected]】
.

============================== 3 passed in 0.30s ==============================

Process finished with exit code 0
#多个.py文件只调用多次fixture

import pytest
# conftest.py
@pytest.fixture()
def get_token():
    token = ‘[email protected]‘
    print(‘获取到token:%s‘ % token)
    return token

import pytest
# test02.py
class Test(object):
    def test2(self,get_token):
        token = ‘[email protected]‘
        print("【执行test02.py-Test类-test2用例,获取get_token:%s】" %get_token)
        assert get_token == token

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

import pytest
#test03.py
class Test(object):
    def test3(self,get_token):
        token = ‘[email protected]‘
        print("【执行test03.py-Test类-test3用例,获取get_token:%s】" %get_token)
        assert get_token == token
    def test4(self,get_token):
        token = ‘[email protected]‘
        print("【执行test03.py-Test类-test4用例,获取get_token:%s】" %get_token)
        assert get_token == token

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

"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items

test02.py 获取到token:[email protected]22
【执行test02.py-Test类-test2用例,获取get_token:[email protected]】
.
test03.py 获取到token:[email protected]22
【执行test03.py-Test类-test3用例,获取get_token:[email protected]】
.获取到token:[email protected]22
【执行test03.py-Test类-test4用例,获取get_token:[email protected]】
.

============================== 3 passed in 0.04s ==============================

Process finished with exit code 0

参考原文链接:https://blog.csdn.net/qq_36502272/article/details/102975467

报错问题:

如果方法名称没有写正确在.py文件里就不会调用到conftest.py的方法

原文地址:https://www.cnblogs.com/zhaocbbb/p/12543447.html

时间: 2024-08-28 17:42:51

python学习-pytest(二)-conftest.py的相关文章

python学习(二)百度爬虫0.1

参照着网上的爬虫案例(点我),先做了一个demo,基本的爬虫项目创建,以及数据抽取,数据分析,数据保存等等过程基本上有所掌握. 我的需求是需要检索指定的百度贴吧,根据指定的关键字库,搜索出含有关键字的链接,并抽取出来,用于后续告警. 因此,基于需求,分如下步骤: 第一:基于Scrapy创建爬虫项目: 第二:新建TieBaSpider爬虫: 第三:新建外部关键字库dictionary.txt文件,贴吧地址配置url.txt文件: 第一步参考晚上案例. 从第二步开始,编写爬虫,同时创建实例对象以及创

Python学习(二):入门篇:python中流程控制与函数编写

python中流程控制与函数编写 Last Eidt 2014/5/2 转载请注明出处http://blog.csdn.net/jxlijunhao 一,流程控制 1)布尔逻辑 Python中利用True来表示逻辑真,False来逻辑假 not :非 and:与 or   :或 ==  :逻辑等 >>> False==True False >>> False==False True >>> not False True >>> Fal

python学习-pytest(五)-pytest常用方法

最近工作中用到了pytest,总结一些用法: 1. 安装: pip install pytest 2. 运行: pytest 默认只能识别以test_ 开头的文件和测试用例,如果pytest后面不带文件名,则默认执行当前目录下所有以test_ 开头的文件. 执行某个文件里所有以 test 开头的用例:pytest test_demo.py # test_demo.py def test_01(): assert 1+1 == 2 def test_02(): assert 1+3 == 4 执行

用命令访问D:\python学习\wendjia教程\aa.py

d:                                -----------切换到D盘 cd python学习\wendjia教程         -----------找到D盘的文件夹 python aa.py                      -----------找到文件夹中的aa.py文件

Python学习笔记&lt;二&gt;:列表、元组、字典

1 列表和元组 可以将列表和元组当成普通的"数组",它能保存任意数量任意类型的Python 对象.和数组一样,通过从0 开始的数字索引访问元素,但是列表和元组可以存储不同类型的对象. 列表和元组有几处重要的区别.列表元素用中括号( [ ])包裹,元素的个数及元素的值可以改变.元组元素用小括号(( ))包裹,不可以更改(尽管他们的内容可以).元组可以看成是只读的列表.通过切片运算( [ ] 和 [ : ] )可以得到子集,这一点与字符串的使用方法一样. 举例如下: >>>

python学习-pytest(一)

学习pytest第一步 一.安装 pytest不是python默认的package,需要手动安装. pytest支持python 2.6--3.5之间的版本,同时可以在windows.unix系统上安装 安装方式: pip install pytest 另外,可能会遇到一下问题: Could not find a version that satisfies the requirement pytest (from versions: )No matching distribution foun

Python学习(二) 登陆接口

浅谈Python学习的第一个小程序,用户登陆接口的开发 代码已实现功能: 1.用户登陆平台需要验证用户名和密码信息是否正确 2.允许用户尝试登陆三次,三次后强制退出登陆端. 3.同一账户连续登陆三次失败写入黑名单.管理员手动解黑后可正常登陆. 待实现功能: 1.实现黑名单用户30分钟自动解黑 2.调用DB用户数据,当前使用的文本列表 3.实现用户登陆验证码功能 4.暂时就想到这些没实现的功能,以后再补充,hehe 编写逻辑: 1.读取用户列表和黑名单列表 2.临时用户变量,记录用户登陆的临时元组

[Python学习] 专题二.条件语句和循环语句的基础知识

        前面讲述了"专题一.函数的基础知识",而这篇文章讲述的Python的条件语句和循环语句的基础知识.主要内容包括: 1.条件语句:包括单分支.双分支和多分支语句,if-elif-else 2.循环语句:while的使用及简单网络刷博器爬虫 3.循环语句:for的使用及遍历列表.元组.文件和字符串 前言: 语句块         在讲诉条件语句.循环语句和其他语句之前,先来补充语句块知识.(前面讲函数时已经用到过) 语句块并非一种语句,它是在条件为真(条件语句)时执行或执行

Python学习(二)—— Python入门

一.第一句Python代码 1. 打开python shell --> 2. 输入   print("hello world!"), Enter回车 3.至此,我们的第一个python程序就执行完了,这个程序的作用就是向控制台输出“hello world!” python内部执行过程如下: 二.创建代码文件,执行python程序 1. 创建文件a1.py并输入如下代码 # !/usr/bin/python print("hello world!") 2. 程序