【问题标题】:How do I get pytest to run all functions as test如何让 pytest 将所有功能作为测试运行
【发布时间】:2016-09-20 02:29:12
【问题描述】:

我环顾四周,找不到一个 pytest.ini 标志可以传入,该标志相当于

def test_one():
   # you run this by default

def two():
   # despite not having test in the name you should also run this

原因:当我对命名进行描述时,测试函数名称的长度会变得很长,坦率地说,这似乎很不干。

【问题讨论】:

  • 您尝试过使用装饰器吗?...真的不确定这是否可行以及 pytest 是否会将其视为测试用例。
  • @IronFist 你会推荐什么装饰器?
  • 我的意思是......制作你自己的包装......还是像我说的......我没有测试或尝试这个。
  • 但老实说,在测试函数名称的开头添加test_ 会使其更长吗?...我不确定您是否可以避免这种情况,因为这是一个要求 并记住具有更多描述性名称的函数,使调试更容易,代码更具可读性!

标签: python unit-testing pytest


【解决方案1】:

来自documentation

# content of setup.cfg
# can also be defined in in tox.ini or pytest.ini file
[pytest]
python_files=check_*.py
python_classes=Check
python_functions=*_check

*_check 替换为* 即可。

【讨论】:

    【解决方案2】:

    如下图所示,它会询问所有python文件的py.test集合测试。

    # content of pytest.ini
    [pytest]
    python_files = *.py
    

    你可以检查这部分文件,

    customizing test collection to find all .py files

    【讨论】:

      猜你喜欢
      • 2015-06-18
      • 2020-04-08
      • 1970-01-01
      • 2020-04-28
      • 2018-06-01
      • 1970-01-01
      • 2019-12-03
      • 2015-01-09
      • 2022-01-16
      相关资源
      最近更新 更多