no output in console for unittests in pycharm 2017

2019-03-18 07:23发布

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']

3条回答
干净又极端
2楼-- · 2019-03-18 08:05

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

查看更多
祖国的老花朵
3楼-- · 2019-03-18 08:08

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)
    
查看更多
Evening l夕情丶
4楼-- · 2019-03-18 08:10

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)

查看更多
登录 后发表回答