【问题标题】:Why does PyCharm fail to run unit tests?为什么 PyCharm 无法运行单元测试?
【发布时间】:2018-02-16 01:33:43
【问题描述】:

在我的 PyCharm (2016.3.3) 项目中,我有一个名为“tests”的包文件夹,其中包含一些测试模块(使用unittest),例如'test_A.py','test_B.py.'

当我尝试在其中一个模块或整个“测试”包中的所有测试中运行测试时,由于AttributeError: 'module' object has no attribute 'test_A',我得到了nose_helper.failure.Failure(其他测试模块也是如此)。

当我在 PyCharm 之外运行模块时,它们可以正常工作。

有谁知道原因,如何解决?

这里有很多类似的问题,例如123 等,但这些都没有解决我的问题。

【问题讨论】:

    标签: python-2.7 unit-testing pycharm python-unittest


    【解决方案1】:

    不知道确切原因,但在我的 PyCharm (2016.3.3) 中,我能够通过编辑运行配置来解决问题:

    从菜单栏中选择 Run->Edit Configurations,然后选择您的配置,然后在“Unittests”下的“Configuration”选项卡中选中“Inspect only subclasses of unittest.TestCase”。

    要使其成为默认值:运行 -> 编辑配置,然后选择默认值->Python 测试->单元测试,然后再次选中相同的框。

    此答案基于ancient PyCharm support post

    --编辑--

    如果测试模块尝试导入不存在的类,例如,这也会发生(在 PyCharm 2018.3 中) (使用 django)from myapp.models import MyModelA, MyModelB 其中MyModelB 不存在。在这种情况下,修复导入应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-19
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2015-06-22
      • 2016-01-18
      • 2018-08-08
      相关资源
      最近更新 更多