简体   繁体   中英

Pycharm hangs on unittests indefinitely if there is a print statement present

I set up a brand new PyCharm project to test this in. As of several hours ago, I could have print statements all over the place with no problem whatsoever. As of right now, having any print statements within any project causes the whole process to spin forever, eventually being closed out automatically by my OS with the code 137 SIGKILL 9 command.

The following code is the most stripped down demonstration of this I could come up with:

import unittest

def hellothere():
    pass

class TestTest(unittest.TestCase):
    def setUp(self):
        stuff = hellothere()
        print(stuff)

    def test(self):
        pass

It's worth noting that changing stuff = hellothere() to stuff = str(hellothere()) and removing the print works, and returning instead of printing works as well.

I tried setting this up in terminal, but when I run this in terminal, I get ValueError: no such test method in <class '__main__.TestTest'>: runTest .

dmesg reports low swap: killing largest compressed process with pid 6093 (python2.7) and size 1051 MB but I can't find any other relevant information in there.

I'm using pycharm verion info below: 在此处输入图片说明

and python 2.7.

Strange behaviour, maybe you reduced example does not have something your original code does have?

I tried this code:

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()   

It works both as python test_abc.py and python -m unittest test_abc with following result:

this is None                                                            
.                                                                       
----------------------------------------------------------------------  
Ran 1 test in 0.001s                                                    

OK                                                                      

There has to be something else that makes the test go to infinite loop. I'd go with trying to run tests in console without pycharm for further inspection.

Update: below is OP solution to the problem, it is related to virtual envirnoments. We still do not know what choked the print() in unittests, but learnt to avoid it.

My lesson is that with heavyweight IDE like PyCharm try to replicate any problem in commandline. Also it seems IntelliJ support is quite responsive 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM