pytest文档25-conftest.py作用范围
前言
一个测试工程下是可以有多个conftest.py的文件,一般在工程根目录放一个conftest.py起到全局作用。
在不同的测试子目录也可以放conftest.py,作用范围只在该层级以及以下目录生效。
conftest层级关系
在web_conf_py项目工程下建两个子项目baidu、blog,并且每个目录下都放一个conftest.py和__init__.py(python的每个package必须要有__init__.py)
web_conf_py是工程名称
├─baidu
│ │ conftest.py
│ │ test_1_baidu.py
│ │ __init__.py
│
│
├─blog
│ │ conftest.py
│ │ test_2_blog.py
│ │ __init__.py
│
│ conftest.py
│ __init__.py

案例分析
web_conf_py工程下conftest.py文件代码案例
# web_conf_py/conftest.py
import pytest
@pytest.fixture(scope="session")
def start():
print("\n打开首页")
baidu目录下conftest.py和test_1_baidu.py
# web_conf_py/baidu/conftest.py
import pytest
@pytest.fixture(scope="session")
def open_baidu():
print("打开百度页面_session")
# web_conf_py/baidu/test_1_baidu.py
import pytest
def test_01(start, open_baidu):
print("测试用例test_01")
assert 1
def test_02(start, open_baidu):
print("测试用例test_02")
assert 1
if __name__ == "__main__":
pytest.main(["-s", "test_1_baidu.py"])
运行test_1_baidu.py结果可以看出,start和open_baidu是session级别的,只运行一次
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py\baidu, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 2 items
test_1_baidu.py
打开首页
打开百度页面_session
测试用例test_01
.测试用例test_02
.
========================== 2 passed in 0.01 seconds ===========================
blog目录下conftest.py和test_2_blog.py代码
# web_conf_py/blog/conftest.py
import pytest
@pytest.fixture(scope="function")
def open_blog():
print("打开blog页面_function")
# web_conf_py/blog/test_2_blog.py
import pytest
def test_03(start, open_blog):
print("测试用例test_03")
assert 1
def test_04(start, open_blog):
print("测试用例test_04")
assert 1
def test_05(start, open_baidu):
'''跨模块调用baidu模块下的conftest'''
print("测试用例test_05,跨模块调用baidu")
assert 1
if __name__ == "__main__":
pytest.main(["-s", "test_2_blog.py"])
运行结果可以看出,start起到全局作用,blog目录下的open_blog是function级别,每个用例调用一次。
test_05(start, open_baidu)用例不能跨模块调用baidu模块下的open_baidu,所以test_05用例会运行失败
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py\blog, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 3 items
test_2_blog.py
打开首页
打开blog页面_function
测试用例test_03
.打开blog页面_function
测试用例test_04
.E
=================================== ERRORS ====================================
__________________________ ERROR at setup of test_05 __________________________
file E:\YOYO\web_conf_py\blog\test_2_blog.py, line 11
def test_05(start, open_baidu):
E fixture 'open_baidu' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, metadata, monkeypatch, open_blog, pytestconfig, record_property, record_xml_attribute, record_xml_property, recwarn, start, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
E:\YOYO\web_conf_py\blog\test_2_blog.py:11
====================== 2 passed, 1 error in 0.02 seconds ======================
---------------------------------pytest结合selenium自动化完整版-------------------------
全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
作者:上海-悠悠 QQ交流群:874033608
也可以关注下我的个人公众号:yoyoketang

pytest文档25-conftest.py作用范围的更多相关文章
- pytest文档5-fixture之conftest.py
前言 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录,用例3需要先登录. ...
- pytest文档4-fixture之conftest.py
用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然这就无法用setup和teardown来实现了.fixture之conftest.py就是自定义测试用例的预置条件 1.firture相对于 ...
- pytest文档7-pytest-html生成html报告
前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...
- pytest文档18-配置文件pytest.ini
前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. ini配置文件 pytest里面有些文件是非test文件 py ...
- pytest文档24-fixture的作用范围(scope)
fixture作用范围 fixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function fixture( ...
- pytest文档56-插件打包上传到 pypi 库
前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...
- pytest文档55-plugins插件开发
前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...
- pytest文档28-重复执行用例(pytest-repeat)
前言 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来. 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例, ...
随机推荐
- CVE-2012-1876Microsoft Internet Explorer Col元素远程代码执行漏洞分析
Microsoft Internet Explorer是微软Windows操作系统中默认捆绑的WEB浏览器. Microsoft Internet Explorer 6至9版本中存在漏 ...
- 集合栈计算机(UVa12096)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- 【洛谷】P4643 【模板】动态dp
题解 在冬令营上听到冬眠的东西,现在都是板子了猫锟真的是好毒瘤啊(雾) (立个flag,我去thusc之前要把WC2018T1乱搞过去= =) 好的,我们可以参考猫锟的动态动态dp的课件,然后你发现你 ...
- linux kernel.shmall shemax shemin解释
Linux X86-64操作系统,Oracle 10g数据库,由8G加到16G,把kernel.shmmax参数改到17179869184(16G)后,发现只要修改sga_max_size和s ...
- 《Android源码设计模式》--单例模式
No1: 懒汉单例模式优缺点分析 public class Singleton{ private static Singleton instance; private Singleton(){} pu ...
- python import 与 from ... import ...
import test test = 'test.py all code' from test import m1 m1 ='code'
- 学习 HMM
简介 HMM 中的变量可以分为两组. 第一组是状态变量 \(\{y_i,y_2,\cdots, y_n\}\), 其中 \(y_i \in \mathcal{Y}\) 表示第 \(i\) 时刻的系统状 ...
- 文件系统层级结构标准(FHS)
参考资料:FHS 简介 FHS目前发展到3.0版本,发布于2015年6月3日,由Linux基金会在负责维护.它规定了Linux的文件层级结构,使得各Linux发行版.软件开发商知道应该将哪些文件放在哪 ...
- Java 中的异常处理机制
生活中的异常: 不能够完整而顺利的完成一些工作 根据不同的异常进行相应的处理,而不会就此终端我们的生活 引出: 异常处理: 方式: 1.选择结构(逻辑判断)避免 demo:if逻辑处理异常 im ...
- Linux-CentOs7-svn安装及钩子配置
做个svn的教程 首先进入test目录下,新建一个svn目录,准备做svn测试cd /testmkdir svncd svn然后使用yum安装svn,这里就不使用编译安装了,这玩意只要能用就行,版本无 ...