zoukankan      html  css  js  c++  java
  • pytest文档64-内置 pytestconfig 动态添加和获取 pytest.ini 配置参数

    前言

    前面讲 pytestconfig 的时候,可以获取到 pytest.ini 里面的配置参数。
    我们在写项目自动化用例的时候,有一些配置参数希望能加到配置里面,如configid, productid,以及测试环境的base_url地址,和账号相关信息。

    addini的源码阅读

    addini有四个参数:name, help, type=None, default=None

        def addini(self, name, help, type=None, default=None):
            """ register an ini-file option.
    
            :name: name of the ini-variable
            :type: type of the variable, can be ``pathlist``, ``args``, ``linelist``
                   or ``bool``.
            :default: default value if no ini-file option exists but is queried.
    
            The value of ini-variables can be retrieved via a call to
            :py:func:`config.getini(name) <_pytest.config.Config.getini>`.
            """
            assert type in (None, "pathlist", "args", "linelist", "bool")
            self._inidict[name] = (help, type, default)
            self._ininames.append(name)
    

    动态添加配置信息

    前面一篇讲添加命令行参数,可以用 addoption 来添加命令行参数,这里我们是添加 pytest.ini 的配置信息
    adddini里面参数说明

    • 第一个'url' 是参数的名称
    • type 是类型,默认None,可以设置:None, "pathlist", "args", "linelist", "bool"
    • default 是设置的默认值
    • help 是设置帮助文档,方便查阅
    # conftest.py
    # 作者-上海悠悠 QQ交流群:717225969
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    def pytest_addoption(parser):
        parser.addoption(
            "--cmdopt", action="store", default="type1", help="my option: type1 or type2"
        )
        # 添加参数到pytest.ini
        parser.addini('url', type=None, default="http://49.235.92.12:8200/", help='添加 url 访问地址参数')
    
    
    # 获取 pytest.ini 配置参数
    @pytest.fixture(scope="session")
    def home_url(pytestconfig):
        url = pytestconfig.getini('url')
        print("
    读取到配置文件的url地址:%s" % url)
        return url
    

    参数用例传 home_url

    # test_y.py
    
    def test_h(home_url):
        print("用例:%s" % home_url)
    

    运行结果

    ============================= test session starts =============================
    platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0, pluggy-0.13.1
    rootdir: D:wangyiyunweb
    collected 1 item
    
    ..........wangyiyunweb	est_y.py 
    读取到配置文件的url地址:http://49.235.92.12:8200/
    用例:http://49.235.92.12:8200/
    .
    
    ========================== 1 passed in 0.02 seconds ===========================
    

    pytest.ini 配置 url地址

    如果有一天我们的测试环境发生了改变,这时候不需要去改代码,只需在 pytest.ini 配置一个环境地址

    [pytest]
    
    
    url = https://www.cnblogs.com/yoyoketang/
    

    重新运行,我们得到的结果是

    ============================= test session starts =============================
    platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0, pluggy-0.13.1
    rootdir: D:wangyiyunweb
    collected 1 item
    
    ..........wangyiyun	est_y.py 
    读取到配置文件的url地址:https://www.cnblogs.com/yoyoketang/
    用例:https://www.cnblogs.com/yoyoketang/
    .
    
    ========================== 1 passed in 0.02 seconds ===========================
    

    type参数的几种类型

    默认None,可以设置:None, "pathlist", "args", "linelist", "bool"

    • type=None 默认读的是字符串
    • type="pathlist" 可以设置多个路径,会自动拼接ini文件这一层目录
    • type="args" 多个参数
    • type="linelist" 可以是多个命令行参数
    • type="bool" bool值,设置1或0
  • 相关阅读:
    C# 中的var关键字
    sql中去掉换行符和回车符
    Linq之旅:Linq入门详解(Linq to Objects)
    c# for 和 foreach
    c# Dictionary
    ASP.NET Web.config学习
    c# 装箱与拆箱的概念
    c# List集合学习
    Index was out of range
    C# double保留四位小数
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/14047492.html
Copyright © 2011-2022 走看看