-
Mémo Désactiver les print en sortie dans Pytest
Ignore prints in pytest output
Prenons une fonction basique :
def return_false(): print("Hello, I am a dirty print!") return False
Elle contient un
print()
, et testée par pytest, ça donne ça :tests/test_machin.py::test_un_truc "Hello, I am a dirty print!" PASSED
C'est moche.
Voici donc un petit décorateur tout laid qui fait le boulot.
def nocapture(func): @functools.wraps(func) def wrapper(*args, **kwargs): sys.stdout = open(os.devnull, 'w') func(*args, **kwargs) sys.stdout = sys.__stdout__ return func return wrapper
Dans le fichier de test, on l'appelle comme ceci:
from truc import return_false @nocapture def test_un_truc(): myfunc = return_false() assert myfunc is False
En sortie:
tests/test_machin.py::test_un_truc PASSED
10 octobre 2019 13:19