anthonywritescode / twitch-chat-bot

MIT License
71 stars 32 forks source link

when tests fail in pytest, post to chat #37

Open asottile opened 3 years ago

asottile commented 3 years ago

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)
MiConnell commented 3 years ago

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

asottile commented 3 years ago

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)