zoukankan      html  css  js  c++  java
  • pytest(二十三)--conftest.py作用范围

    前言

    一个测试工程下是可以有多个conftest.py的文件,一般在工程根目录放一个conftest.py起到全局作用。

    在不同的测试子目录也可以放conftest.py,作用范围只在该层级及以下目录生效。

    conftest层级关系

    在web_item_py项目工程下建两个子项目(包)QQ、UC,并且每个目录下都放一个conftest.py和__init__.py(python的每个package必须要有__init__.py)

    案例分析

     web_item_py工程下conftest.py文件代码如下:

    #web_item_py/conftest.py
    # coding:utf-8
    import pytest
    @pytest.fixture(scope="session")
    def begin():
        print("
    打开首页")
    

    QQ目录下conftest.py文件代码如下:

    #web_item_py/qq/conftest.py
    # coding:utf-8
    import pytest
    
    @pytest.fixture(scope="session")
    def open_baidu():
        print("打开百度页面_session")  

    QQ目录下test_1_qq.py代码如下;

    #web_item_py/qq/test_1_qq.py
    # coding:utf-8
    import pytest
    def test_q1(begin,open_baidu):
        print("测试用例test_q1")
        assert 1
    def test_q2(begin,open_baidu):
        print("测试用例test_q2")
        assert 1
    if __name__=="__main__":
        pytest.main(["-s","test_1_QQ.py"])  

    运行QQ目录下test_1_qq.py,结果可以看出,begin和open_baidu是session级别的,只运行一次。

     UC目录下conftest.py和test_1_UC.py代码如下

    #web_item_py/UC/conftest.py
    # coding:utf-8
    import pytest
    @pytest.fixture(scope="function")
    def go():
        print("打开搜狗页面")
    
    #web_item_py/UC/test_1_UC.py
    # coding:utf-8
    import pytest
    def test_u1(begin,go):
        print("测试用例u1")
        assert 1
    def test_u2(begin,go):
        print("测试用例u2")
        assert 2
    def test_u3(begin,open_baidu):
        print("测试用例u3")
        assert 2
    if __name__=="__main__":
        pytest.main(["-s","test_1_UC.py"]) 

    运行结果:begin起到全局作用,UC目录下的go是function级别,每个用例调用一次。

    test_u3(begin,open_baidu)用例不能夸模块调用QQ模块下的open_baidu,所以test_u3用例会运行失败。

     同时指向web_item_py工程下的所有用例

    越努力,越幸运!!! good good study,day day up!!!
  • 相关阅读:
    爱牛网站
    阿里云文档
    Linux下iptables屏蔽IP和端口号
    oracle 启动监听报错TNS-12547: TNS:lost contact
    http://www.opensymphony.com/
    jmx
    联想笔记本装win7
    包解析
    js为字符串编码
    JAVA版本号微信公众账号开源项目版本号公布-jeewx1.0(捷微)
  • 原文地址:https://www.cnblogs.com/canglongdao/p/13408967.html
Copyright © 2011-2022 走看看