Unittest Error; AttributeError: 'file' object has no attribute 'getvalue'



I am trying to check my code (python 2.7 and Pycharm 2017.3.3) with unittests and in one test I get a the following error: 



Traceback (most recent call last):
File "/home/vagrant/.pycharm_helpers/pycharm/_jb_unittest_runner.py", line 35, in <module>
main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
File "/usr/lib/python2.7/unittest/main.py", line 95, in __init__
File "/usr/lib/python2.7/unittest/main.py", line 232, in runTests
self.result = testRunner.run(self.test)
File "/home/vagrant/.pycharm_helpers/pycharm/teamcity/unittestpy.py", line 304, in run
return super(TeamcityTestRunner, self).run(test)
File "/usr/lib/python2.7/unittest/runner.py", line 151, in run
File "/usr/lib/python2.7/unittest/suite.py", line 70, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.7/unittest/suite.py", line 108, in run
File "/usr/lib/python2.7/unittest/suite.py", line 70, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.7/unittest/suite.py", line 108, in run
File "/usr/lib/python2.7/unittest/case.py", line 393, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.7/unittest/case.py", line 370, in run
File "/home/vagrant/.pycharm_helpers/pycharm/teamcity/unittestpy.py", line 260, in stopTest
output = sys.stdout.getvalue()
AttributeError: 'file' object has no attribute 'getvalue'



I don't know why it happens and could not find any solution or similar issue on that topic. I hope someone can help me out ;-) 





Does it happen to all tests or just some of them? Do you redirect unittest to file somehow or do you monkeypatch stdout?


I have only tested two unittests in our software so far, and one works perfectly fine, test complets with ok, and the other one runs through, doing what it is supposed to do, but then throws the mentioned error. Both unittests are built in a similar way: only one function with testdata is run.


I haven't touched stdout. With redirect unittest to file, do you mean to log the output? That I do not. 


Hi Nikodem Bienia! Is it possible to share the failing test code with us? You can create a ticket in our bug tracker https://youtrack.jetbrains.com/issues/PY with visibility level of pycharm-developers to ensure privacy.


This fails in the same way when run through IntelliJ's "Run Unittests for ..." feature.



import random
import unittest
import numba
import numpy as np

def is_contiguous(begin1, end1, begin2, end2):
  if begin1 > begin2: return is_contiguous(begin2, end2, begin1, end1)
  return end1 + 1 >= begin2

class RangeTests(unittest.TestCase):
  def test_contiguous(self):
    import sys
    print sys.stdout, type(sys.stdout) # TeamCity somethingthing
    assert not is_contiguous(3, 8, 0, 1)
    assert is_contiguous(3, 8, 1, 2)

if __name__ == '__main__':


Fails here:


def stopTest(self, test):
   test_id = self.get_test_id(test)

   if getattr(self, 'buffer', None):
   # Do not allow super() method to print output by itself
    self._mirrorOutput = False

     output = sys.stdout.getvalue()   << syst.stdout has no getvalue method





Hi Stuart! Which PyCharm version do you use? This code example works fine for me on 2018.1.4.


It failed for my with a new install of PyCharm 2018.1.4, them worked after updating the plugins.


I take it, back, it still fails.


Here's another example:

Also see https://youtrack.jetbrains.com/issue/PY-23940

Probably, you unittest runner shouldn't assume reload(sys) is not called, but instead cached the original sys.stderr, sys.stdout.


import sys

import unittest

class ReloadSysTest(unittest.TestCase):
def testsys(self):
import sys
reload(sys) # Reload does the trick!



Since setting JB_DISABLE_BUFFERING is a poor fix and solvable in your libraries, can we reopen this?


Same issue on :

PyCharm 2018.1.4 (Community Edition)
Build #PC-181.5087.37, built on May 24, 2018
JRE: 1.8.0_152-release-1136-b39 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6


It gets solved by resetting `



but a better solution is needed


Hi Alisa,

Please see https://youtrack.jetbrains.com/issue/PY-23940#focus=streamItem-27-2908495-0-0

The comment is pretty fresh, thus I suspect the information regarding plans to support it is correct.


This big is absolutely solveble without needed to define an environment variable to disable functionality. You shouldn’t expect environment variables be an acceptable solution for any app launched from a UI dock, or expect the user to define the variable for each test config.

Pycharm should simply keep a reference to the string IO it’s runner created. Where it is referenced at shut down, simply retrieve it instead of assuming sys.stdio is the object you assigned to it at the start and breaking every program that overrides it.


Please sign in to leave a comment.