在 PyCharm 中禁用自动单元测试

Disable Automatic unittesting in PyCharm

PyCharm 自动检测文件何时以 test_ 开头,并尝试将它们 运行 作为单元测试。很贴心,但是好像有什么东西坏了,在任何测试中我运行,我得到

/Users/peter/projects/plato/venv/bin/python2.7 "/Applications/PyCharm CE.app/Contents/helpers/pycharm/utrunner.py" /Users/peter/projects/plato/utils/tools/test_sampling.py::::test_samplers_not_broken true
Testing started at 10:58 AM ...
Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pycharm/utrunner.py", line 140, in <module>
    all.addTest(testLoader.makeTest(getattr(module, a[2])))
AttributeError: 'TestLoader' object has no attribute 'makeTest'

Process finished with exit code 1

我只想 运行 它们作为普通 python 文件,但 PyCharm 不允许。 Eclipse 提供选项 运行 通常或 运行 作为单元测试。这很烦人!我该怎么做?

here(在编辑器上方工具面板上的设置按钮附近指向下方的小灰色箭头) 选择'Edit Configuration'

您可以在此处选择 pycharm 应该 运行 进行哪些测试以及何时进行。可能有设置 All in folder 按钮,选择另一个选项,如 script

您还应该检查文件的底部,在 if __name__ == '__main__'

之后

也许你有代码可以在你运行这个文件

时自动启动测试

在我的例子中,我将配置设置为 > 测试:Class

一旦更改为脚本,它就可以正常工作而不会出现任何错误。

我的临时解决方案是在页面底部添加一个块。

if __name__ == "__main__": pass

在字段块的左侧,将有一个按钮,用于 运行 在不进行测试的情况下启用脚本。

此外,您可以跟踪此问题 here

注: 如果文件中的 filesfunctionstest_ 开头,则 PyCharm 会尝试 运行 脚本作为默认 Unittests.

UPD 31/01/2018: 我试图捕捉一个他如何为我工作的例子

此外,您始终可以使用 if (Shift+Alt+F10)

查看选项而不用阻塞

对我来说,将 Settings -> Tools -> Python Integrated tools -> Testing 设置为 Unittests 有效。