前言

一个测试工程下是可以有多个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. 从一道简单的dp题中学到的...

    今天想学点动态规划的知识,于是就看了杭电的课件,数塔问题啊,LCS啊都是比较经典的动规了,然后随便看了看就开始做课后练习题... HDOJ 1421 搬寝室 http://acm.hdu.edu.cn ...

  2. 利用Octopress在github pages上搭建个人博客

    利用Octopress在github pages上搭建个人博客 SEP 29TH, 2013 在GitHub Pages上用Octopress搭建博客,需要安装ruby环境.git环境等.本人在Fed ...

  3. ASP.NET MVC之验证终结者篇

    有时候我觉得,很多人将一个具体的技术细节写的那么复杂,我觉得没有必要,搞得很多人一头雾水的,你能教会别人用就成了,具体的细节可以去查MSDN什么的,套用爱因斯坦的名言:能在网上查到的就不要去记,用的时 ...

  4. 【BZOJ】4894: 天赋

    题解 这道题是求一个有向图的外向生成树 入度矩阵对应着外向生成树,出度矩阵对应着内向生成树,知道了这个就可以求出基尔霍夫矩阵了,同时n - 1阶主子式一定要删掉根节点的一行一列 代码 #include ...

  5. Looksery Cup 2015 F - Yura and Developers 单调栈+启发式合并

    F - Yura and Developers 第一次知道单调栈搞出来的区间也能启发式合并... 你把它想想成一个树的形式, 可以发现确实可以启发式合并. #include<bits/stdc+ ...

  6. python之web框架(2):了解WSGI接口

    python之web框架(2):了解WSGI接口 1.什么是wsgi接口: wsgi:Web Service Gateway Interface.它不是模块,而只是一种规范,方便web服务器和各种框架 ...

  7. python3下django连接mysql数据库

    1.安装pymysql pip install pymysql 有一点需要注意,有的系统(比如ubuntu16.04)同时安装了python2和python3,而比较新的django需要在python ...

  8. Xiaoguang Tu's Home Page

    Xiaoguang Tu (涂晓光): CV: Ph.D. Candidate of School of Communication and Information Engineering, Univ ...

  9. 【知了堂学习笔记】java 正则表达式

    本文参考网络上面别人的博客知识产出 正则表达式基础 1.句号 假设你想要找出三个字母的单词,而且这些单词必须以“t”字母开头,以“n”字母结束.另外,假设有一本英文字典,你可以用正则表达式搜索它的全部 ...

  10. Python网络编程之socket应用

    1 引言 本篇主要对Python下网络编程中用到的socket模块进行初步总结.首先从网络基础理论出发,介绍了TCP协议和UDP协议:然后总结了socket中的常用函数:最后通过实际代码展示基本函数的 ...