欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Pytest运行脚本小结

程序员文章站 2024-02-27 13:19:21
...

run.py

import sys
import pytest
import os

ROOT_DIR=os.path.dirname(os.path.abspath(__file__))

def main():
    print(ROOT_DIR)
    if ROOT_DIR not in sys.path:
        sys.path.append(ROOT_DIR)
    # 执行用例
    # args = ['--reruns', '1', '--html=' + './report/' + HTML_NAME]
    # pytest.main(args)
    args = ['--env', 'pord']
    pytest.main()



if __name__ == '__main__':
    main()

pytest.ini:

[pytest]
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True
addopts = --env 222

conftest.py:

import pytest
import os
import yaml

@pytest.fixture(scope="session")
def env(request):
    config_path = os.path.join(request.config.rootdir,
                               "config",
                               request.config.getoption("environment"),
                               "config.yaml")
    with open(config_path,encoding='utf-8') as f:
        env_config = yaml.load(f.read(), Loader=yaml.SafeLoader)
    return env_config

def pytest_addoption(parser):
    parser.addoption("--env",
                     action="store",
                     dest="environment",
                     default="111",
                     help="environment: test or prod")


运行脚本添加了测试环境的选择项,默认为./config/111文件夹下,可以运行是指定args参数,也可以在pytest.ini里面添加。其优先级顺序为:指定args>pytest.ini>默认

相关标签: pytest pytest