Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

python - Output ASCII art to console on succesfull pytest run

I'm using pytest to run tests in Django project. I'm using pytest.ini where DJANGO_SETTINGS_MODULE is defined, so I run tests with just:

pytest

Now, I want to add some ASCII art to the console output if the test run is successful. I know I can do:

pytest && cat ascii_art.txt

But I want to hide the ASCII art to config or somewhere else so that I keep running tests with just pytest. I don't see any pytest config option I can use. Any other ideas how this could be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are lots of places where you can print your own stuff in pytest; select an appropriate hook from the hooks list and override it, adding your own printing. To spice the examples a bit up, I will print some system info using a screenfetch wrapper function:

def screenfetch():
    exec = shutil.which('screenfetch')
    out = ''
    if exec:
        out = subprocess.run(exec, stdout=subprocess.PIPE, universal_newlines=True).stdout
    return out

Custom print after test execution finished

Create a file conftest.py in your project root dir with the following contents:

from utils import screenfetch

def pytest_unconfigure(config):
    print(screenfetch())

Result:

enter image description here

If you want a conditional print on successful test runs only, use pytest_sessionfinish hook to store the exit code:

def pytest_sessionfinish(session, exitstatus):
    session.config.exitstatus = exitstatus

def pytest_unconfigure(config):
    if config.exitstatus == 0:
        print(screenfetch())

Another examples:

Enhanced summary

# conftest.py
from utils import screenfetch

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    terminalreporter.ensure_newline()
    terminalreporter.write(screenfetch())

Custom print before pytest output starts

# conftest.py

from utils import screenfetch

def pytest_configure(config):
    print(screenfetch())

Custom print after pytest's header info

# conftest.py

import screenfetch

def pytest_report_header(config, startdir):
    return screenfetch()

Custom print after tests collected, before test run

# conftest.py

import os
from utils import screenfetch

def pytest_collection_modifyitems(session, items):
    terminalreporter = session.config.pluginmanager.get_plugin('terminalreporter')
    terminalreporter.ensure_newline()
    terminalreporter.write(screenfetch())

Custom print after each test

def pytest_report_teststatus(report, config):
    if report.when == 'teardown':  # you may e.g. also check the outcome here to filter passed or failed tests only
        terminalreporter = config.pluginmanager.get_plugin('terminalreporter')
        terminalreporter.ensure_newline()
        terminalreporter.write(screenfetch())

Note that I use terminalreporter plugin instead of just printing where possible - this is how pytest itself emits the output.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...