控制台中没有输出用于pycharm 2017中的单元测试

bac*_*ach 14 teamcity pycharm

我使用import unittest创建了unitests.当我想运行一个特定的测试并且我放置一个断点然后转到控制台并尝试eval表达式没有返回值,好像stdout不再是控制台屏幕.

我从未安装过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']
Run Code Online (Sandbox Code Playgroud)

Zac*_*ung 15

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

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

为所有未来的临时测试设置env var

有了这个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)
    
    Run Code Online (Sandbox Code Playgroud)


lik*_*ppy -1

Pycharm单元测试交互式调试命令行不起作用

使用pytest

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

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

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