简体   繁体   English

如何在运行pytest时让PyCharm显示进度条?

[英]How to get PyCharm to display progressbar while running pytest?

I use pytest and have a test that takes a very long time. 我使用pytest进行测试需要很长时间。 So I added a progress bar with progressbar2 . 所以我添加了一个带有progressbar2的进度条。

import progressbar
from time import sleep


def test_progressbar_bug():
    with progressbar.ProgressBar() as bar:
        for i in bar(range(10)):
            sleep(1)

Running this in a terminal with pytest -s gives me a nice test output with a progress bar that updates as time passes. 在带有pytest -s的终端中运行它给了我一个很好的测试输出,其中一个进度条随着时间的推移而更新。

However, if I try to run this from within PyCharm with a pytest configuration (and -s as option) I just get an empty output. 但是,如果我尝试使用pytest配置在PyCharm中运行它(并且-s作为选项),我只得到一个空输出。 Only after the test is finished the complete progress bar appears. 只有在测试完成后才会出现完整的进度条。

I also tried to create my own progress display with sys.stdout without any success in PyCharm's integrated pytest. 我还尝试使用sys.stdout创建自己的进度显示,但在PyCharm的集成pytest中没有任何成功。

from time import sleep
import sys


def test_progress_bug():
    for i in range(10):
        sys.stdout.write("\rProgress: %3d" % (i * 100 / 10.) + "%")
        sys.stdout.flush()
        sleep(1)

What do I have to do to display a progress bar that shows the progress of a single test when running pytests in PyCharm? 在PyCharm中运行pytests时,我需要做什么来显示一个显示单个测试进度的进度条?

Edit: I don't really like my current workaround - which is running this as a normal python configuration - because I don't get the fancy test overview of PyCharm: 编辑:我真的不喜欢我目前的解决方法 - 这是一个普通的python配置 - 因为我没有得到PyCharm的花哨的测试概述:

Test.py Test.py

import os

if __name__ == "__main__":
    os.system("pytest -s")

I had the same problem. 我有同样的问题。 Just install "progressbar2" lib and it will work 只需安装“progressbar2”lib即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM