zoukankan      html  css  js  c++  java
  • pytest08-在pytest中配置环境变量

    首先import os, 写入系统当前目录的环境变量

    然后用os.envtiron方法来获取。

    可以写到conftest.py中,放到根目录下,当成全局变量


    #命令行参数设置
    parser.addoption(
    "--cmdhost",
    action="store",
    default="http://xx.x.x:8080",
    help="my option: type1 or type2"
    )
    设置完成后可以在终端 pytest -h 来查看是否写入


    @pytest.fixture(scope='session',autouse=True)
    def host(request):
    '''获取命令行参数,给到环境变量'''
    os.environ['host'] = request.config.getoption('--cmdhost')

    ---case---

    #autouse=True:用例运行时自动加载,不需要传入
    @pytest.fixture(scope='session',autouse=True)
    def test_case():
      url = os.environ['host'] + path
      print url

    --------

    这种只能放到命令行去pytest运行, 在pycharm中if__main()方法运行是失败的,只有pytest用例才会生效
    所以自己去选择用这种还是用之前的ini配置......

    -----------------------多环境配置---------------------

  • 相关阅读:
    小程序排错(redis导致)
    linux系统删除-开头文件或目录
    nginx 反向代理
    memcache
    一次故障过程
    json.Decoder vs json.Unmarshal
    ssl中间证书
    作业10:String类
    Angular6如何引入jQuery-knob
    OpenJdk源码下载
  • 原文地址:https://www.cnblogs.com/dearddu/p/12531301.html
Copyright © 2011-2022 走看看