Joao Coelho
Joao Coelho

Reputation: 3156

How to properly fail in pytest pytest_sessionstart

I'm doing some verification in pytest's pytest_sessionstart in conftest.py and if the verification fails I raise ValueError.

This works, but the printed error is pretty brutal with lots of INTERNALERROR lines that makes it confusing to read.

I was looking to something more clean with just my error message. Is there a way to do that?

Upvotes: 2

Views: 692

Answers (1)

hoefling
hoefling

Reputation: 66251

Use pytest.exit:

import pytest


def precondition():
    raise ValueError('precondition failed')


def pytest_sessionstart(session):
    try:
        precondition()
    except ValueError as err:
        pytest.exit(str(err), returncode=1)

Example output:

$ pytest
Exit: precondition failed
!!!!!!!!!!!!!!!!!!!!!!!! _pytest.outcomes.Exit: precondition failed !!!!!!!!!!!!!!!!!!!!!!!!!

Upvotes: 3

Related Questions