18

I have created unitests using import unittest. When I want to run a specific test and I put a breakpoint then go to the console and try to eval expressions there's no return value as if the stdout is no longer the console screen.

I have never installed teamcity but strangely I do get messages when running the unittest. VERY STRANGE. I thought that maybe the captureStandardOutput='true' (emphasized on last line, below) is the cause of the problem but I can't even find where to change the param to test it.

C:\Users\selas\AppData\Local\Continuum\Anaconda3\python.exe "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2017.1\helpers\pydev\pydevd.py" --multiproc --qt-support --client 127.0.0.1 --port 59641 --file "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2017.1\helpers\pycharm\_jb_unittest_runner.py" --target tests.test_model.FigurationDBTesting.test_printFigurationPerBoundary
pydev debugger: process 8932 is connecting
Connected to pydev debugger (build 171.3780.115)

teamcity[enteredTheMatrix timestamp='...']
Launching unittests with arguments python -m unittest tests.test_model.FigurationDBTesting.test_printFigurationPerBoundary
teamcity[testSuiteStarted timestamp='...' locationHint='python://tests' name='tests' nodeId='1' parentNodeId='0']
teamcity[testSuiteStarted timestamp='...' locationHint='python://tests.test_model' name='test_model' nodeId='2' parentNodeId='1']
teamcity[testSuiteStarted timestamp='...' locationHint='python://tests.test_model.FigurationDBTesting' name='FigurationDBTesting' nodeId='3' parentNodeId='2']

teamcity[testStarted timestamp='...' >!> captureStandardOutput='true' <!< locationHint='python://tests.test_model.FigurationDBTesting.test_printFigurationPerBoundary' name='test_printFigurationPerBoundary' nodeId='4' parentNodeId='3']
Zach Young
  • 10,137
  • 4
  • 32
  • 53
bach
  • 369
  • 3
  • 10
  • You can try to edit ```helpers/pycharm/teamcity/unittestpy.py``` (in the PyCharm.app package on my Mac, not sure where it is for Windows). I changed it to ```captureStandardOutput='false'``` and the debugger starts with my modified value, but it's the same result :( – Zach Young Jun 28 '17 at 16:29

4 Answers4

24

Looks like the official fix for this bug, PY-22505, is to add the new JB_DISABLE_BUFFERING environment variable to your unit test configurations (no value required, per screenshot), but only for 2017.1.3 or greater.

This screenshot shows adding the env var for the Defaults config, so all new configs will inherit it. You could also add this individually to already-saved Run/Debug configurations:

Setting env var for all future ad-hoc tests

With that env var in place, I can now:

  1. add a breakpoint
  2. right-click any test or class and choose the 'Debug Unittests for ...' menu option
  3. hit breakpoint, go to debug console
  4. inspect my runtime and get the printouts (note captureStandardOutput='true'):

    ...
    ##teamcity[testStarted timestamp='...' captureStandardOutput='true' locationHint='python</Users/zyoung/PycharmProjects/Foo/test/unit_tests>://test_distance.Foo.testMatchRatio_050' name='testMatchRatio_050' nodeId='3' parentNodeId='2']
    import sys; print('Python %s on %s' % (sys.version, sys.platform))
    Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47) 
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    
    >>> print(self)
    testMatchRatio_050 (test_distance.Foo)
    
Zach Young
  • 10,137
  • 4
  • 32
  • 53
  • @KarelMacek Which version of PyCharm do you have? – Zach Young Aug 29 '17 at 16:20
  • In fact, I did not add that variable to already existing tests. – Karel Macek Sep 18 '17 at 06:26
  • This works for me: `PyCharm 2017.2.3` `Build #PY-172.3968.37, built on September 1, 2017` `JRE: 1.8.0_152-release-915-b11 x86_64` `JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o` `Mac OS X 10.12.6` – joeb1415 Sep 21 '17 at 01:11
  • Is there a way to set this setting globally? ( Works for me in 2017.2.4 ) – Pekka Dec 29 '17 at 03:58
  • @Pekka, did you see the paragraph and the screenshot regarding **Defaults**? I think that will address your "global" needs. – Zach Young Dec 30 '17 at 20:20
  • 1
    @ZacharyYoung it seems that the defaults in run configurations are defaults by project. So I have to set this setting in all projects separately. tbh it's not so big deal but I was looking for a place to set this globally for multiple projects. – Pekka Dec 31 '17 at 05:51
0

Could this be because you use pycharm 2017.1 combined with a python environment that has teamcity-messages in it? That combi crashes, see https://youtrack.jetbrains.com/issue/PY-23926

Upvote that ticket if that's the case please (making an account took me 1 min).

ikku100
  • 809
  • 1
  • 7
  • 16
0

I had the issue running pytests for django in the Pycharm debugger. Adding -s as a parameter did the trick for me. As seen in the Pycharm bugreport: PY-22505 from Zach's answer.

smoquet
  • 321
  • 3
  • 11
-1

Pycharm unit test interactive debug command line doesn't work

use pytest

(Run > Edit Configurations > Defaults > Python tests > py.test > add -s to the options field------>(Additional Arguments).)

settings default run tests by pytest:

(Preferences > Tools > Python Intergrated Tools > default test runner)

likaiguo.happy
  • 2,138
  • 1
  • 15
  • 9