Skip to content

Commit d857349

Browse files
authored
Replace version comparison with duck-style checks (fix #802) (#803)
1 parent 097fcf6 commit d857349

File tree

8 files changed

+59
-32
lines changed

8 files changed

+59
-32
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ exclude =
77
./tests/allure_behave/acceptance/behave_support/background/background_steps.py
88
per-file-ignores =
99
./allure-python-commons/src/model2.py:A003
10+
./allure-python-commons/src/types.py:A005
11+
./allure-robotframework/src/listener/types.py:A005

.github/workflows/build.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969

7070
- name: Cache commons
7171
id: commons
72-
uses: actions/cache@v3
72+
uses: actions/cache@v4
7373
with:
7474
path: dist/
7575
key: commons-${{ github.sha }}
@@ -89,7 +89,7 @@ jobs:
8989
- uses: actions/checkout@v4
9090

9191
- name: Set up Python
92-
uses: actions/setup-python@v4
92+
uses: actions/setup-python@v5
9393
with:
9494
python-version: "3.12"
9595

@@ -124,7 +124,7 @@ jobs:
124124

125125
- name: Get commons from cache
126126
id: commons
127-
uses: actions/cache@v3
127+
uses: actions/cache@v4
128128
with:
129129
path: dist/
130130
key: commons-${{ github.sha }}
@@ -165,7 +165,7 @@ jobs:
165165

166166
- name: Get commons from cache
167167
id: commons
168-
uses: actions/cache@v3
168+
uses: actions/cache@v4
169169
with:
170170
path: dist/
171171
key: commons-${{ github.sha }}

.github/workflows/release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ jobs:
1010
runs-on: ubuntu-latest
1111

1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414

1515
- name: Set up Python
16-
uses: actions/setup-python@v4
16+
uses: actions/setup-python@v5
1717
with:
1818
python-version: '3.x'
1919

allure-behave/src/listener.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
from allure_behave.utils import get_fullname
2323
from allure_behave.utils import TEST_PLAN_SKIP_REASON
2424
from allure_behave.utils import get_hook_name
25-
import behave
26-
from packaging import version
27-
28-
BEHAVE_1_2_7_OR_GREATER = version.parse(behave.__version__) > version.parse("1.2.6")
2925

3026

3127
class AllureListener:
@@ -101,10 +97,8 @@ def stop_test(self, parent_uuid, uuid, name, context, exc_type, exc_val, exc_tb)
10197
self.stop_scenario(context['scenario'])
10298

10399
def stop_scenario(self, scenario):
104-
if BEHAVE_1_2_7_OR_GREATER:
105-
should_run = scenario.should_run_with_tags(self.behave_config.tag_expression)
106-
else:
107-
should_run = scenario.should_run_with_tags(self.behave_config.tags)
100+
tag_expression = self.__get_tag_expression(self.behave_config)
101+
should_run = scenario.should_run_with_tags(tag_expression)
108102
should_run = should_run and scenario.should_run_with_name_select(self.behave_config)
109103
should_drop_skipped_by_option = scenario.status == 'skipped' and not self.behave_config.show_skipped
110104
should_drop_excluded = self.hide_excluded and (scenario.skip_reason == TEST_PLAN_SKIP_REASON or not should_run)
@@ -213,6 +207,14 @@ def add_link(self, url, link_type, name):
213207
def stop_session(self):
214208
self.group_context.exit()
215209

210+
@staticmethod
211+
def __get_tag_expression(config):
212+
tag_expression = getattr(config, "tag_expression", None)
213+
if tag_expression is None:
214+
# Behave 1.2.6 and earlier
215+
return getattr(config, "tags")
216+
return tag_expression
217+
216218

217219
class GroupContext:
218220
def __init__(self, logger):

allure-pytest/src/compat.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Provides compatibility with different pytest versions."""
2+
3+
from inspect import signature
4+
5+
__GETFIXTUREDEFS_2ND_PAR_IS_STR = None
6+
7+
8+
def getfixturedefs(fixturemanager, name, item):
9+
"""Calls FixtureManager.getfixturedefs in a way compatible with Python
10+
versions before and after the change described in pytest-dev/pytest#11785.
11+
"""
12+
getfixturedefs = fixturemanager.getfixturedefs
13+
itemarg = __resolve_getfixturedefs_2nd_arg(getfixturedefs, item)
14+
return getfixturedefs(name, itemarg)
15+
16+
17+
def __resolve_getfixturedefs_2nd_arg(getfixturedefs, item):
18+
# Starting from pytest 8.1, getfixturedefs requires the item itself.
19+
# In earlier versions it requires the nodeid string.
20+
return item.nodeid if __2nd_parameter_is_str(getfixturedefs) else item
21+
22+
23+
def __2nd_parameter_is_str(getfixturedefs):
24+
global __GETFIXTUREDEFS_2ND_PAR_IS_STR
25+
if __GETFIXTUREDEFS_2ND_PAR_IS_STR is None:
26+
__GETFIXTUREDEFS_2ND_PAR_IS_STR =\
27+
__get_2nd_parameter_type(getfixturedefs) is str
28+
return __GETFIXTUREDEFS_2ND_PAR_IS_STR
29+
30+
31+
def __get_2nd_parameter_type(fn):
32+
return list(
33+
signature(fn).parameters.values()
34+
)[1].annotation

allure-pytest/src/listener.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import pytest
22
import doctest
3-
from packaging import version
43

54
import allure_commons
65
from allure_commons.utils import now
76
from allure_commons.utils import uuid4
87
from allure_commons.utils import represent
98
from allure_commons.utils import platform_label
109
from allure_commons.utils import host_tag, thread_tag
10+
from allure_commons.utils import md5
1111
from allure_commons.reporter import AllureReporter
1212
from allure_commons.model2 import TestStepResult, TestResult, TestBeforeResult, TestAfterResult
1313
from allure_commons.model2 import TestResultContainer
@@ -25,7 +25,7 @@
2525
from allure_pytest.utils import get_pytest_report_status
2626
from allure_pytest.utils import format_allure_link
2727
from allure_pytest.utils import get_history_id
28-
from allure_commons.utils import md5
28+
from allure_pytest.compat import getfixturedefs
2929

3030

3131
class AllureListener:
@@ -349,23 +349,13 @@ def _test_fixtures(item):
349349

350350
if hasattr(item, "_request") and hasattr(item._request, "fixturenames"):
351351
for name in item._request.fixturenames:
352-
fixturedefs_pytest = _getfixturedefs(fixturemanager, name, item)
352+
fixturedefs_pytest = getfixturedefs(fixturemanager, name, item)
353353
if fixturedefs_pytest:
354354
fixturedefs.extend(fixturedefs_pytest)
355355

356356
return fixturedefs
357357

358358

359-
def _getfixturedefs(fixturemanager, name, item):
360-
# See pytest-dev/pytest#11785
361-
itemarg = item if __is_pytest8_1_or_greater() else item.nodeid
362-
return fixturemanager.getfixturedefs(name, itemarg)
363-
364-
365-
def __is_pytest8_1_or_greater():
366-
return version.parse(pytest.__version__) >= version.parse("8.1")
367-
368-
369359
def _exception_brokes_test(exception):
370360
return not isinstance(exception, (
371361
AssertionError,

tests/allure_pytest/acceptance/capture/capture_attach_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,9 @@ def test_capture_log(allure_pytest_runner: AllurePytestRunner, logging):
9090
... logger.info("Start step")
9191
"""
9292

93-
params = [] if logging else ["-p", "no:logging"]
93+
log_level = "INFO" if logging else "WARNING"
9494
allure_results = allure_pytest_runner.run_docstring(
95-
"--log-level=INFO",
96-
*params
95+
f"--log-level={log_level}",
9796
)
9897

9998
if_logging_ = is_ if logging else is_not

tests/allure_pytest_bdd/acceptance/capture/capture_attach_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ def then_the_postconditions_are_held():
134134
"""
135135
)
136136

137-
params = [] if logging else ["-p", "no:logging"]
137+
log_level = "INFO" if logging else "WARNING"
138138
allure_results = allure_pytest_bdd_runner.run_pytest(
139139
("scenario.feature", feature_content),
140-
steps_content, cli_args=("--log-level=INFO", *params)
140+
steps_content, cli_args=(f"--log-level={log_level}",)
141141
)
142142

143143
if_logging_ = is_ if logging else is_not

0 commit comments

Comments
 (0)