Dalla versione 3.3, pytest supporta la registrazione in tempo reale, il che significa che tutti i record di registro emessi nei test verranno stampati immediatamente sul terminale. La funzionalità è documentata nella sezione Live Logs . La registrazione in tempo reale è disabilitata per impostazione predefinita; per abilitarlo impostare log_cli = 1in pyproject.toml1 o pytest.ini2 . La registrazione live supporta l'emissione su terminale e file; le relative opzioni consentono la personalizzazione dei record:
terminale:
log_cli_level
log_cli_format
log_cli_date_format
file:
log_file
log_file_level
log_file_format
log_file_date_format
Nota : il log_cliflag non può essere passato dalla riga di comando e deve essere impostato in pytest.ini. Tutte le altre opzioni possono essere sia passate dalla riga di comando che impostate nel file di configurazione. Come sottolineato da Kévin Barré in questo commento , l'override delle opzioni ini dalla riga di comando può essere eseguito tramite l' -o/--overrideopzione. Quindi, invece di dichiarare log_cliin pytest.ini, puoi semplicemente chiamare:
$ pytest -o log_cli=true ...
Esempi
File di test semplice utilizzato per dimostrare:
import logging
LOGGER = logging.getLogger(__name__)
def test_eggs():
LOGGER.info('eggs info')
LOGGER.warning('eggs warning')
LOGGER.error('eggs error')
LOGGER.critical('eggs critical')
assert True
Come puoi vedere, non è necessaria alcuna configurazione aggiuntiva; pytestimposterà il logger automaticamente, in base alle opzioni specificate pytest.inio passate dalla riga di comando.
Registrazione live su terminale, INFOlivello, output di fantasia
Configurazione in pyproject.toml:
[tool.pytest.ini_options]
log_cli = true
log_cli_level = "INFO"
log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
La configurazione identica in legacy pytest.ini:
[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format=%Y-%m-%d %H:%M:%S
Esecuzione del test:
$ pytest test_spam.py
=============================== test session starts ================================
platform darwin -- Python 3.6.4, pytest-3.7.0, py-1.5.3, pluggy-0.7.1 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-4673373, inifile: pytest.ini
collected 1 item
test_spam.py::test_eggs
---------------------------------- live log call -----------------------------------
2018-08-01 14:33:20 [ INFO] eggs info (test_spam.py:7)
2018-08-01 14:33:20 [ WARNING] eggs warning (test_spam.py:8)
2018-08-01 14:33:20 [ ERROR] eggs error (test_spam.py:9)
2018-08-01 14:33:20 [CRITICAL] eggs critical (test_spam.py:10)
PASSED [100%]
============================= 1 passed in 0.01 seconds =============================
Registrazione in tempo reale su terminale e file, solo messaggi e CRITICAL livello nel terminale, output di fantasia nel pytest.logfile
Configurazione in pyproject.toml:
[tool.pytest.ini_options]
log_cli = true
log_cli_level = "CRITICAL"
log_cli_format = "%(message)s"
log_file = "pytest.log"
log_file_level = "DEBUG"
log_file_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_file_date_format = "%Y-%m-%d %H:%M:%S"
La configurazione identica in legacy pytest.ini:
[pytest]
log_cli = 1
log_cli_level = CRITICAL
log_cli_format = %(message)s
log_file = pytest.log
log_file_level = DEBUG
log_file_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_file_date_format=%Y-%m-%d %H:%M:%S
Prova:
$ pytest test_spam.py
=============================== test session starts ================================
platform darwin -- Python 3.6.4, pytest-3.7.0, py-1.5.3, pluggy-0.7.1 -- /Users/hoefling/.virtualenvs/stackoverflow/bin/python3.6
cachedir: .pytest_cache
rootdir: /Users/hoefling/projects/private/stackoverflow/so-4673373, inifile: pytest.ini
collected 1 item
test_spam.py::test_eggs
---------------------------------- live log call -----------------------------------
eggs critical
PASSED [100%]
============================= 1 passed in 0.01 seconds =============================
$ cat pytest.log
2018-08-01 14:38:09 [ INFO] eggs info (test_spam.py:7)
2018-08-01 14:38:09 [ WARNING] eggs warning (test_spam.py:8)
2018-08-01 14:38:09 [ ERROR] eggs error (test_spam.py:9)
2018-08-01 14:38:09 [CRITICAL] eggs critical (test_spam.py:10)
1 pyproject.toml supportato dalla versione 6.0 ed è la migliore opzione IMO. Vedere PEP 518 per le specifiche.
2 Anche se è anche possibile configurare pytestin setup.cfgsotto la [tool:pytest]sezione, non essere tentati di farlo quando si desidera fornire formato di registrazione dal vivo personalizzato. Altri strumenti di lettura setup.cfgpotrebbero trattare cose come %(message)sl'interpolazione di stringhe e fallire. La scelta migliore è pyproject.tomlcomunque usare , ma se sei costretto a usare il formato ini-style legacy, attenersi a pytest.iniper evitare errori.