allure-framework / allure-python

Allure integrations for Python test frameworks
https://allurereport.org/
Apache License 2.0
719 stars 235 forks source link

Fix regression: allure_pytest.utils.allure_title crashes if obj attr doesn't exist (Fixes #733) #734

Closed delatrie closed 1 year ago

delatrie commented 1 year ago

Context

This pull request fixes the regression defect that could be observed if allure-pytest is run against a pytest item with no obj attribute or property. This is typical for items that have no meaningful object they can refer. The real-world examples are pytest-mypy's MypyItem and YamlItem from Working with non-python tests.

The breaking change was introduced during the work on #732. The intent was to get rid of exception-based flow control. The fix is just to add another getattr call instead of accessing the obj attribute directly.

AllureFileLogger patching fix

tests.e2e.allure_in_memory_context now always patches allure_commons.logger.AllureFileLogger in addition to provided paths.

Motivation

Generally, when setting up the in-memory logger, we want to patch the class imported by an integration-specific module (e.g., the allure_pytest.plugin.AllureFileLogger class), not the original one (i.e., allure_commons.logger.AllureFileLogger). This preloads the module and replaces the logger class with the in-memory logger. The module is then removed from sys.modules by pytester once the test is completed and the next test repeats the sequence.

This works universally, regardless of whether allure is enabled for the running session or not or whether we test allure-pytest, allure-behave or other integrations. The only exception is when there is a reference to the patched module that prevents it from being unloaded. The patched module gets reimported by a pytester and the patching appears to have no effect.

Example

From ./tests/allure_pytest/defects/issue733_test.py:

from allure_pytest.utils import allure_title
# ...

The ref chain after the first end-to-end test on allure-pytest is executed:

Consequently, all in-memory end-to-end tests will fail.

Now if we also patch allure_commons.logger.AllureFileLogger, the correct class is imported when pytester reimports the module and the tests pass.

Checklist

Closes #733