首页 > 解决方案 > PyTest:如何使用特定标记运行特定脚本

问题描述

我想要实现的是运行 python 测试脚本,但我想控制哪些测试正在通过@pytest.mark夹具运行。到目前为止我所做的是:

  1. pytest.ini像这样在文件中定义标记:

    [pytest]

    markers=[omni,milestone,release]

  2. 在测试模块脚本中,我定义了 2 种测试方法:

    • test_1 用 :@pytest.mark.omni标记
    • test_2 标有:@pytest.mark.release标记我正在尝试以以下方式执行代码:在模块的末尾,我放置了这段代码:
if __name__ == "__main__":
        import pytest
        junit_path = os.path.join(tmp_folder, 'reports', 'Junit_p1_' + os.environ['COMPUTERNAME'] + '_' + time.strftime( "%d%m%Y_%H%M%S") + '.xml')
        exit_code = pytest.main(['-vv', '--cache-clear', 'p1.py', '--durations=0', '--junitxml', junit_path, '--tb' ,'long']) # full execution
        sys.exit(exit_code)

我通过调用调用模块

python p1.py

我正在尝试找出可以在pytest.main调用侧添加标记过滤器的位置,以便从测试中运行特定标记

标签: python-3.xpytest

解决方案


推荐阅读