twitch-chat-bot icon indicating copy to clipboard operation
twitch-chat-bot copied to clipboard

when tests fail in pytest, post to chat

Open asottile opened this issue 5 years ago • 2 comments

idk quite how this would work, but here's an idea:

  1. start some sort of web socket / server in bot
  2. change pytest to be an alias
  3. somehow grep output for failures (or install a pytest plugin? idk)

asottile avatar Nov 14 '20 22:11 asottile

Could you alias a command like pt to a pyteset_alias.py file

pytest_alias.py

import sys
import os

args = sys.argv[1:]

os.system(f'pytest {" ".join([a for a in args])}')

and then call bot functions from within conftest.py?

import pytest


def pytest_sessionstart(session):
    session.results = {}


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item):
    outcome = yield
    result = outcome.get_result()

    if result.when == "call":
        item.session.results[item] = result


def pytest_sessionfinish(session):
    if sum(1 for result in session.results.values() if result.failed) > 0:
        do_bot_stuff()


def do_bot_stuff():
    print("bot goes brrr")

This is working for me, I get

FAILED bot goes brrr

after typing this in the terminal python pytest_alias.py -sv tests/test_alias.py

MiConnell avatar Nov 21 '20 05:11 MiConnell

I think the tricky part is pytest is usually in the project's virtualenv and not the bot's virtualenv so the injection needs to be super generic (can't really overwrite the project's conftest.py for example)

asottile avatar Nov 21 '20 05:11 asottile