Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when tests fail in pytest, post to chat #37

Open
asottile opened this issue Nov 14, 2020 · 2 comments
Open

when tests fail in pytest, post to chat #37

asottile opened this issue Nov 14, 2020 · 2 comments

Comments

@asottile
Copy link
Member

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
Copy link
Contributor

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
Copy link
Member Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants