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

Answered

Hi, 

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__
self.runTests()
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
test(result)
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
test(result)
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
test(result)
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
result.stopTest(self)
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 ;-) 

Cheers, 

Niko

0
12 comments

Hello.

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

0
Avatar
Permanently deleted user

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. 

0

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.

0
Avatar
Permanently deleted user

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

@numba.njit
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__':
  unittest.main()

```

Fails here:

.../config/plugins/python-ce/helpers/pycharm/teamcity/unittestpy.py

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

 

 

 

0

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

0
Avatar
Permanently deleted user

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

0
Avatar
Permanently deleted user

I take it, back, it still fails.

0
Avatar
Permanently deleted user

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!

 

0
Avatar
Permanently deleted user

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

0

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 `

buffer=JB_DISABLE_BUFFERING

`

but a better solution is needed

0

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.

0
Avatar
Permanently deleted user

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.

0

Please sign in to leave a comment.