【问题标题】:How to tell py.test to skip certain directories?如何告诉 py.test 跳过某些目录?
【发布时间】:2012-06-22 10:37:04
【问题描述】:

我尝试使用 setup.cfg 中的 norecursedirs 选项来告诉 py.test 不要从某些目录收集测试,但它似乎确实忽略了它。

[tool:pytest]
norecursedirs=lib/third

当我运行py.test 时,我确实看到它是如何从lib/third 内部获取测试的!

【问题讨论】:

  • 看来我有py.testpytest 他们都在运行测试并且是不同的野兽。奇怪,但pytest 是失败的,因为它没有从[pytest] 加载排除项。
  • pytest 来自 logilab。你想要py.test
  • 也可以试试nosecuredirs=lib/third/*
  • 在这里结束,因为我很好奇为什么我的本地开发中的 web 应用程序的 pytest 如此缓慢......原因是一些上传的资源目录具有嵌套的 yy/dd/mm 结构.. ..导致它真的很烂!谢天谢地,[pytest] norecursedirs = resources 中的 pytest.ini 有一个技巧!????
  • @ecatmur 我猜你的评论已经过时了?我认为今天应该使用pytest 而不是py.test

标签: python unit-testing pytest


【解决方案1】:

py.test --ignore=somedir 为我工作

【讨论】:

  • --ignore可以多次指定忽略多个目录
  • 如何跳过某些测试而不是目录
  • 您可以使用-k 选项作为下面显示的另一个答案。请参阅docs.pytest.org/en/latest/… 上的“通过关键字表达式运行测试”。标记在这里也可能会有所帮助。将您的测试标记为“慢”等,然后使用-k "not slow"
  • to use --ignore-glob 也可以忽略基于 Unix shell 样式通配符的测试文件路径。例如。 --ignore-glob=**/sandbox/* 将忽略任何 sandbox 文件夹中的所有文件。
【解决方案2】:

如果您有多个具有不同父级的目录,您可以指定不同的--ignore 参数:

py.test --ignore=somedir --ignore=otherdir --ignore=etcdir

  • 新选项:--ignore 将阻止收集指定路径。
    可以多次指定。

【讨论】:

    【解决方案3】:

    我解开了谜团:如果在一个可能的配置文件(pytest.initox.inisetup.cfg)中找到了 pytest 部分,pytest 将不会查找任何其他部分,因此请务必定义 py.test。单个文件中的测试选项。

    我建议使用setup.cfg

    【讨论】:

    • 为什么是setup.cfg?当前文档似乎推荐the other two
    • 我认为重要的是要提到,如果你想使用.ini 文件,你需要用pytest -c pytest.ini 指定它。出于某种原因,setup.cfg 的内容被自动拾取。
    • @therightstuff 对我来说,pytest.ini 的使用没有这些技巧。
    【解决方案4】:

    你可以使用

    py.test -k 'not third'
    

    排除所有“第三”目录内容。

    【讨论】:

      【解决方案5】:

      就我而言,问题是缺少通配符。以下对我有用:

      [tool:pytest]
      norecursedirs = subpath/*
      

      而只是 norecursedirs = subpath 没有。

      【讨论】:

      • 这里也一样。不过非常奇怪的行为。
      【解决方案6】:

      norecursedirs 应该可以工作。检查您是否有 pytest.ini 或其他 setup.cfg 文件。你是如何调用py.test的?

      【讨论】:

      • 我确实有 setup.cfg 和正确的 [pytest] 和 norecorsedirs 但它似乎被忽略了,而是它会查找所有文件。
      【解决方案7】:

      如果您使用 setup.cfg,则必须按照 http://doc.pytest.org/en/latest/example/pythoncollection.html 使用 [tool:pytest]

      #pytest.ini 的内容
      # 也可以在 tox.ini 或 setup.cfg 文件中定义,虽然 section
      # setup.cfg 文件中的名称应该是“tool:pytest”

      【讨论】:

        【解决方案8】:

        要使用pytest.ini(这是推荐的,除非您使用tox,在这种情况下tox.ini 会有意义),请使用pytest -c pytest.ini 调用pytest

        为了防止我的样本not_me_1.pynot_me_2.py被测试或linted,我的pytest.ini如下:

        [pytest]
        addopts = --ignore-glob=not_me_*
        
        [pycodestyle]
        ignore = not_me_* ALL
        
        

        【讨论】:

          猜你喜欢
          • 2016-04-11
          • 1970-01-01
          • 2012-11-09
          • 1970-01-01
          • 2015-08-22
          • 2018-09-04
          • 1970-01-01
          • 2017-07-20
          • 1970-01-01
          相关资源
          最近更新 更多