python - 如果存在打印语句,Pycharm 会无限期地挂起单元测试

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

我建立了一个全新的 PyCharm 项目来测试这一点。几个小时前,我可以毫无问题地在各处打印语句。截至目前,任何项目中的任何打印语句都会导致整个过程永远旋转,最终被我的操作系统使用代码 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 版本信息: enter image description here

和 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 支持似乎 react 非常灵敏 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!

关于python - 如果存在打印语句,Pycharm 会无限期地挂起单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56859990/

相关文章:

python Timedelta溢出

c# - nsubstitute 收到调用特定对象参数

python - 在 pandas 的元组列表中选择行

python - Django项目: track migrations

python - 在 IPython Notebook 中同时使用 Python 2.x 和 Python 3.x

xml - lxml.etree 将元素插入 element.text

java - 如何对自定义 Wicket 组件进行单元测试

c++ - 链接 UnitTest++ 失败

python - 拆分嵌套字典

c++ - Python C API 和 C++ 函数