关于 pytest 标记执行的问题

pytest.main(['test_shop_v2.py', '-s', '-m', 'shop_list', '--alluredir', report_path,'--clean-alluredir'])为什么没有选择shop_list标签来执行而是全部都执行了呢

关于 pytest 标记执行的问题

您遇到的问题是因为 -m 参数的使用方式可能有误。在您当前的命令中:

pytest.main(['test_shop_v2.py', '-s', '-m', 'shop_list', '--alluredir', report_path,'--clean-alluredir'])

'-m', 'shop_list' 这部分在参数列表中是被正确传递的,但如果所有测试都被执行了,可能有以下几个原因:

可能的原因和解决方案

  1. 标记未正确定义

    • 确保您的测试用例中确实使用了 @pytest.mark.shop_list 装饰器
    • 示例:
      @pytest.mark.shop_list
      def test_example():
          assert True
      
  2. 标记注册问题

    • 在 pytest.ini 或 conftest.py 中注册自定义标记
    • 示例 pytest.ini:
      [pytest]
      markers =
          shop_list: tests related to shop listing
      
  3. 参数传递格式问题

    • 尝试将 -m 和标记表达式合并为一个参数:
      pytest.main(['test_shop_v2.py', '-s', '-m=shop_list', '--alluredir', report_path, '--clean-alluredir'])
      
  4. 标记表达式语法

    • 如果需要更复杂的标记选择,可以使用表达式如 -m "shop_list"
  5. 检查是否有其他配置覆盖

    • 检查是否有其他 pytest 配置或命令行参数覆盖了您的标记选择

调试建议

  1. 首先运行以下命令查看所有可用标记:

    pytest --markers
    
  2. 运行以下命令查看哪些测试会被选中:

    pytest test_shop_v2.py -m shop_list --collect-only
    
  3. 确保您的测试文件命名符合 pytest 的发现规则(以 test_ 开头或结尾)

如果以上方法都不能解决问题,请检查您的测试文件内容,确认标记是否正确应用。

posted @ 2025-05-16 11:01  Haloiwuhan  阅读(19)  评论(0)    收藏  举报