Pycharm 如果存在打印语句,单元测试将无限期挂起
Pycharm hangs on unittests indefinitely if there is a print statement present
我建立了一个全新的 PyCharm 项目来测试它。几个小时前,我可以毫无问题地在所有地方打印语句。截至目前,任何项目中的任何打印语句都会导致整个过程永远旋转,最终被我的 OS 使用代码 137 SIGKILL 9 命令自动关闭。
以下代码是我能想到的最精简的演示:
import unittest
def hellothere():
pass
class TestTest(unittest.TestCase):
def setUp(self):
stuff = hellothere()
print(stuff)
def test(self):
pass
值得注意的是,将 stuff = hellothere()
更改为 stuff = str(hellothere())
并删除打印作品,并返回而不是打印作品。
我尝试在终端中进行设置,但是当我在终端中 运行 时,我得到 ValueError: no such test method in <class '__main__.TestTest'>: runTest
.
dmesg 报告 low swap: killing largest compressed process with pid 6093 (python2.7) and size 1051 MB
但我在其中找不到任何其他相关信息。
我正在使用下面的 pycharm 版本信息:
和python 2.7.
奇怪的行为,也许你简化的例子没有你的原始代码有的东西?
我试过这段代码:
import unittest
def hellothere():
pass
class TestTest(unittest.TestCase):
def setUp(self):
self.stuff = hellothere()
print("this is", self.stuff)
# fixed: you need test_ for test discovery
def test_smth(self):
pass
assert self.stuff is None
if __name__ == '__main__':
unittest.main()
它同时作为 python test_abc.py
和 python -m unittest test_abc
使用,结果如下:
this is None
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
必须有其他原因使测试进入无限循环。我会尝试 运行 在控制台中进行测试,而不 pycharm 进行进一步检查。
更新:以下是问题的OP解决方案,与虚拟环境有关。我们仍然不知道是什么让单元测试中的 print()
窒息,但学会了避免它。
我的教训是,像 PyCharm 这样的重量级 IDE 尝试在命令行中复制任何问题。此外,IntelliJ 支持似乎非常灵敏 on similar issues.
I just figured it out. The virtual environment that was being used in my project interpreter was just copied over when I created a new project which is why creating a new project didn't fix it. I created a new interpreter with a new virtual environment, and problem solved!
我建立了一个全新的 PyCharm 项目来测试它。几个小时前,我可以毫无问题地在所有地方打印语句。截至目前,任何项目中的任何打印语句都会导致整个过程永远旋转,最终被我的 OS 使用代码 137 SIGKILL 9 命令自动关闭。
以下代码是我能想到的最精简的演示:
import unittest
def hellothere():
pass
class TestTest(unittest.TestCase):
def setUp(self):
stuff = hellothere()
print(stuff)
def test(self):
pass
值得注意的是,将 stuff = hellothere()
更改为 stuff = str(hellothere())
并删除打印作品,并返回而不是打印作品。
我尝试在终端中进行设置,但是当我在终端中 运行 时,我得到 ValueError: no such test method in <class '__main__.TestTest'>: runTest
.
dmesg 报告 low swap: killing largest compressed process with pid 6093 (python2.7) and size 1051 MB
但我在其中找不到任何其他相关信息。
我正在使用下面的 pycharm 版本信息:
和python 2.7.
奇怪的行为,也许你简化的例子没有你的原始代码有的东西?
我试过这段代码:
import unittest
def hellothere():
pass
class TestTest(unittest.TestCase):
def setUp(self):
self.stuff = hellothere()
print("this is", self.stuff)
# fixed: you need test_ for test discovery
def test_smth(self):
pass
assert self.stuff is None
if __name__ == '__main__':
unittest.main()
它同时作为 python test_abc.py
和 python -m unittest test_abc
使用,结果如下:
this is None
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
必须有其他原因使测试进入无限循环。我会尝试 运行 在控制台中进行测试,而不 pycharm 进行进一步检查。
更新:以下是问题的OP解决方案,与虚拟环境有关。我们仍然不知道是什么让单元测试中的 print()
窒息,但学会了避免它。
我的教训是,像 PyCharm 这样的重量级 IDE 尝试在命令行中复制任何问题。此外,IntelliJ 支持似乎非常灵敏 on similar issues.
I just figured it out. The virtual environment that was being used in my project interpreter was just copied over when I created a new project which is why creating a new project didn't fix it. I created a new interpreter with a new virtual environment, and problem solved!