简体   繁体   中英

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?

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 !!!!!!!!!!!!!!!!!!!!!!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM