allure-framework / allure-framework/allure-python

Integration with python/pytest logging functionality

Offen
#209 4 Kommentare 11 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
theme:core type:enhancement
Vorherrschende Sprache
Python
Sterne
814
Forks
260
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

#### I'm submitting a ...
- [x] feature request

#### What is the current behavior?
logger calls from python logging module are not shown on the report.

#### What is the expected behavior?
they be shown on the report

#### What is the motivation / use case for changing the behavior?
the report should contain as much info as possible. Log calls are very relevant information.

#### Please tell us about your environment:

- Allure version: 2.6.0
- Test framework: pytest@3.4
- Allure adaptor: allure-pytest@2.3.2b1

#### Other information

I actually have a simple working workaround for this, by defining a custom logging handler for Allure (you can use it on conftest.py or as a plugin):

```
import logging
import allure

class AllureLoggingHandler(logging.Handler):
def log(self, message):
with allure.step('Log {}'.format(message)):
pass

def emit(self, record):
self.log("({}) {}".format(record.levelname, record.getMessage()))

class AllureCatchLogs:
def __init__(self):
self.rootlogger = logging.getLogger()
self.allurehandler = AllureLoggingHandler()
def __enter__(self):
if self.allurehandler not in self.rootlogger.handlers:
self.rootlogger.addHandler(self.allurehandler)
def __exit__(self, exc_type, exc_value, traceback):
self.rootlogger.removeHandler(self.allurehandler)

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_setup():
with AllureCatchLogs():
yield

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call():
with AllureCatchLogs():
yield

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown():
with AllureCatchLogs():
yield
```

Then you can test with a simple test:

```
def test_print():
logging.info("Logging an info message")
logging.debug("Logging a DEBUG message")
logging.warning("Sample time is too low!")
raise Exception
```
It can be added for the test like this example or easily added also in the config section of a custom pytest plugin. Not sure if this could be merged into allure pytest plugin though.

It is still missing more complete formatting (module name). Also the handler log level is not dynamically adjusted as done in the pytest logging plugin, but it seems to work fine like this because pytest sets both the handler and the logger to the same level.

It even works with pytest logging switches. For instance, if we run this test with --log_level=INFO, the debug log record won't appear:

![image](https://user-images.githubusercontent.com/20566367/36359007-e2fdffc8-1516-11e8-9c81-d06b704c18f3.png)

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne damit, die pytest-Integrationspunkte und die Beispiele für conftest.py-hookwrapper in der Issue zu prüfen, und verfolge dann, wie Ereignisse im Testlebenszyklus zu Allure-Reportdaten werden. Verwende den bereitgestellten logging handler und den Beispieltest, um zu prüfen, ob Python-logging-Records im Report erscheinen und dabei die Log-Level-Schalter von pytest respektiert werden; abgeschlossen ist die Aufgabe, wenn die Integration ohne einen benutzerdefinierten Handler funktioniert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
testing
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.