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配置......

猜你喜欢

转载自www.cnblogs.com/dearddu/p/12531301.html
今日推荐