【问题标题】:no output in console for unittests in pycharm 2017pycharm 2017 中的单元测试在控制台中没有输出
【发布时间】:2017-04-11 14:47:42
【问题描述】:

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

我从未安装过 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 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']

【问题讨论】:

  • 您可以尝试编辑helpers/pycharm/teamcity/unittestpy.py(在我的 Mac 上的 PyCharm.app 包中,不确定它在 Windows 的位置)。我将其更改为captureStandardOutput='false',调试器以我修改后的值开始,但结果相同:(

标签: pycharm teamcity


【解决方案1】:

看起来这个错误的官方修复,PY-22505,是将新的JB_DISABLE_BUFFERING 环境变量添加到您的单元测试配置中(不需要值,每个屏幕截图),但仅适用于 2017.1.3 或更大

此屏幕截图显示为 Defaults 配置添加 env var,因此所有新配置都将继承它。您还可以将其单独添加到已保存的运行/调试配置中:

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

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

【讨论】:

  • @KarelMacek 你有哪个版本的 PyCharm?
  • 事实上,我并没有将该变量添加到已经存在的测试中。
  • 这对我有用:PyCharm 2017.2.3Build #PY-172.3968.37, built on September 1, 2017JRE: 1.8.0_152-release-915-b11 x86_64JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.oMac OS X 10.12.6
  • 有没有办法全局设置这个设置? (在 2017.2.4 为我工作)
  • @ZacharyYoung 似乎运行配置中的默认值是项目的默认值。所以我必须在所有项目中分别设置这个设置。 tbh 这没什么大不了的,但我一直在寻找一个地方来为多个项目在全球范围内设置它。
【解决方案2】:

这可能是因为您将 pycharm 2017.1 与包含 teamcity-messages 的 python 环境结合使用?那个组合崩溃了,看 https://youtrack.jetbrains.com/issue/PY-23926

如果是这种情况,请投票支持该票(创建帐户花了我 1 分钟)。

【讨论】:

    【解决方案3】:

    我在 Pycharm 调试器中为 django 运行 pytests 时遇到问题。添加-s 作为参数对我来说是诀窍。正如 Pycharm 错误报告中所见:PY-22505 来自 Zach 的回答。

    【讨论】:

      【解决方案4】:

      Pycharm unit test interactive debug command line doesn't work

      使用 pytest

      (运行 > 编辑配置 > 默认值 > Python 测试 > py.test > 将 -s 添加到选项字段------>(附加参数)。)

      通过 pytest 设置默认运行测试:

      (首选项 > 工具 > Python 集成工具 > 默认测试运行器)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-02
        • 2015-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多