pycharm 2017 中的单元测试在控制台中没有输出 [英] no output in console for unittests in pycharm 2017

查看:86
本文介绍了pycharm 2017 中的单元测试在控制台中没有输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 import unittest 创建了 unitest.当我想运行特定测试并放置一个断点时,然后转到控制台并尝试评估表达式没有返回值,就好像标准输出不再是控制台屏幕一样.

我从未安装过teamcity,但奇怪的是我在运行unittest 时确实收到了消息.很奇怪.我认为可能 captureStandardOutput='true'(在最后一行强调,下面)是问题的原因,但我什至找不到更改参数的位置来测试它.

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 测试.test_model.FigurationDBTesting.test_printFigurationPerBoundarypydev 调试器:进程 8932 正在连接连接到 pydev 调试器(内部版本 171.3780.115)teamcity[enteredTheMatrix timestamp='...']使用参数启动单元测试 python -m unittest tests.test_model.FigurationDBTesting.test_printFigurationPerBoundaryteamcity[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']

解决方案

貌似官方已经修复了这个 bug,

有了那个环境变量,我现在可以:

  1. 添加断点
  2. 右键单击任何测试或类,然后选择'Debug Unittests for ...' 菜单选项
  3. 点击断点,进入调试控制台
  4. 检查我的运行时并获取打印输出(注意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']导入系统;print('Python %s on %s' % (sys.version, sys.platform))Python 2.7.13(v2.7.13:a06454b1afa1,2016 年 12 月 17 日,12:39:47)[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] 在达尔文>>>打印(自己)testMatchRatio_050 (test_distance.Foo)

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

解决方案

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:

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)
    

这篇关于pycharm 2017 中的单元测试在控制台中没有输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆