前言

一个测试工程下是可以有多个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作用范围的更多相关文章

  1. pytest文档5-fixture之conftest.py

    前言 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录,用例3需要先登录. ...

  2. pytest文档4-fixture之conftest.py

    用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然这就无法用setup和teardown来实现了.fixture之conftest.py就是自定义测试用例的预置条件 1.firture相对于 ...

  3. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  4. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  5. pytest文档18-配置文件pytest.ini

    前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. ini配置文件 pytest里面有些文件是非test文件 py ...

  6. pytest文档24-fixture的作用范围(scope)

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

  7. pytest文档56-插件打包上传到 pypi 库

    前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...

  8. pytest文档55-plugins插件开发

    前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...

  9. pytest文档28-重复执行用例(pytest-repeat)

    前言 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来. 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例, ...

随机推荐

  1. Centos7配置vsftpd3.0.2

    1.安装vsftpd vsftp使用本地用户登陆方式 yum -y install vsftpd yum安装的版本3.0.2 2.配置vsftpd vim /etc/vsftpd/vsftpd.con ...

  2. Eclipse+Tomcat+Axis2+ADT开发环境配置

    一.安装Eclipse和Tomcat 1.安装Eclipse: 2.解压缩安装apache-tomcat-6.0.41 3.tomcat配置环境变量(4个) TOMCAT_HOME     D:\An ...

  3. jquery获取浏览器宽高

    满足获取各种高的需求 $(document).ready(function() { alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(documen ...

  4. USACO 4.3 Buy Low, Buy Lower

    Buy Low, Buy Lower The advice to "buy low" is half the formula to success in the stock mar ...

  5. loadrunner添加变量检查点

    LoadRunner设置登陆检查点 login() { lr_think_time(); web_url("passport2", "URL=http://test232 ...

  6. C#实例 Unity依赖注入使用

    Unity是一个轻量级的可扩展的依赖注入容器,支持构造函数,属性和方法调用注入.Unity可以处理那些从事基于组件的软件工程的开发人员所面对的问 题.构建一个成功应用程序的关键是实现非常松散的耦合设计 ...

  7. css盒子垂直居中

    首先父盒子包住子盒子 <body> <div class="outbox"> <div class="box"></d ...

  8. linux虚拟机更改时区

    第一种方法: cat  /etc/sysconfig/clock ZONE="Asia/Shanghai" UTC=true ARC=false rm   -f /etc/loca ...

  9. 8-15 Shuffle uva12174

    题意: 你正在使用的音乐播放器有一个所谓的乱序功能,即随机打乱歌曲的播放顺序.假设一共有s首歌,则一开始会给这s首歌随机排序,全部播放完毕后再重新随机排序.继续播放,依此类推.注意,当s首歌播放完毕之 ...

  10. C# 动态类型与动态编译简介

    关于C#的动态类型与动态编译的简介,主要是一个Demo. 动态类型 关键字: dynamic 这里有详细的介绍:[C#基础知识系列]专题十七:深入理解动态类型 动态类型的应用场景 可以减少强制转换(强 ...