• 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("
    打开首页")
    

    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:YOYOweb_conf_pyaidu, 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:YOYOweb_conf_pylog, 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:YOYOweb_conf_pylog	est_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:YOYOweb_conf_pylog	est_2_blog.py:11
    ====================== 2 passed, 1 error in 0.02 seconds ======================
    

    ---------------------------------pytest结合selenium自动化完整版-------------------------

    全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

    作者:上海-悠悠 QQ交流群:874033608

    也可以关注下我的个人公众号:yoyoketang

  • 相关阅读:
    基于ROS-Unity的移动机器人虚实交互场景构建方法的研究
    ROS连接ABB机械臂调试详细教程-ROS(indigo)和ABB RobotStudio 6.03.02-
    ROS#资讯汇总(ROS、C#、.NET和Unity3D)
    论文阅读:A review and comparison of ontology-based approaches to robot autonomy ALBERTO
    商业RDF三元组数据库AllegroGraph
    Qt编写的项目作品30-录音播放控件(雨田哥作品)
    Qt编写的项目作品29-RTSP播放器+视频监控(海康SDK版本)
    Qt编写的项目作品28-RTSP播放器+视频监控(mpv版本)
    Qt音视频开发23-通用视频控件
    Qt音视频开发22-通用GPU显示
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/9769242.html
Copyright © 2020-2023  润新知