前言

一个测试工程下是可以有多个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. GitHub在线创建文件夹

    点击New files按钮,然后输入含有slash字符(“/”)的文件名即可.也就是建立一个含有路径(目录)的文件,即会自动产生新文件夹. 点击Upload files按钮,然后直接把本地的文件夹(内 ...

  2. 【51nod】1655 染色问题

    题解 首先每个颜色出现的次数应该是一样的 \(\frac{C_{n}^{2}}{n} = \frac{n - 1}{2}\) 所以n如果是偶数那么就无解了 然后我们需要让每个点连颜色不同的四条边 只要 ...

  3. Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting

    G - GCD Counting 思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦. 终判被卡了MLE.....  需要每次清空一下子树的map... #inc ...

  4. java把html标签字符转换成普通字符(反转换成html标签)

    package net.jasonjiang.web; import org.junit.Test; import org.springframework.web.util.HtmlUtils; /* ...

  5. Java 中线程安全问题

    不好意思,一个国庆假期给我放的都不知道东西南北了,放松,很放松,差一点就弃更了,感谢那些催更的小伙伴们! 虽然没有更新,但是日常的学习还是有的,以后我尽量给大家分享一些通用知识,非技术. 但是本期还是 ...

  6. 《Playing hard exploration games by watching YouTube》论文解读

    论文链接 油管链接 一.摘要   当环境奖励特别稀疏的时候,强化学习方法通常很难训练(traditionally struggle).一个有效的方式是通过人类示范者(human demonstrato ...

  7. JAVAEE——宜立方商城03:Nginx负载均衡高可用、Keepalived+Nginx实现主备

    1 nginx负载均衡高可用 1.1 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务, ...

  8. python中 .write 无法向文件写入内容

    问题代码如下 links = open("new") out = open("out.txt","w+") for link in link ...

  9. MongoDB 记录

    查询操作: db.stu.find() //查询所有数据 db.stu.findOne() //查询一个数据 db.stu.find().pretty() //查询之后,格式化显示 db.stu.fi ...

  10. 【原创】自己动手写的一个查看函数API地址的小工具

    C开源代码如下: #include <stdio.h> #include <windows.h> #include <winbase.h> typedef void ...