agent-python-pytest icon indicating copy to clipboard operation
agent-python-pytest copied to clipboard

TypeError when using pytest.skip() in fixtures Python 3.12

Open andrewidqa opened this issue 1 year ago • 0 comments

TypeError when using pytest.skip() in fixtures

Description

When using pytest.skip() within a fixture, pytest-reportportal encounters a TypeError during test teardown. The error occurs because the plugin attempts to access the skip exception as if it were an indexable sequence, but the Skipped object doesn't support indexing.

Environment

  • pytest-reportportal: 5.4.4
  • reportportal-client: 5.5.9
  • Python: 3.12
  • Operating System: macOS

Reproduction Steps

  1. Create a test file with the following content:
import pytest

@pytest.fixture(scope="session")
def base_fixture():
    return False

@pytest.fixture()
def skip_fixture(base_fixture):
    if not base_fixture:
        pytest.skip("Skip if base condition is false")

def test_will_skip(skip_fixture):
    pass
  1. Run the test with pytest-reportportal enabled

Current Behavior

The test execution fails with an ExceptionGroup containing two identical TypeError exceptions:

ExceptionGroup: errors during test teardown (2 sub-exceptions)
+-+---------------- 1 ----------------
  | Traceback (most recent call last):
  |   ...
  | TypeError: 'Skipped' object is not subscriptable
  +---------------- 2 ----------------
  | Traceback (most recent call last):
  |   ...
  | TypeError: 'Skipped' object is not subscriptable

The error occurs in pytest_reportportal/plugin.py line 339:

exception = fixturedef.cached_result[2][0]

Expected Behavior

The test should be marked as skipped in ReportPortal without raising any exceptions during teardown.

Full Stack Trace

Tests/test_errors.py:12 (test_will_skip)
+ Exception Group Traceback (most recent call last):
  |   File "/Users/user/PycharmProjects/core-platform-automation/venv/lib/python3.12/site-packages/_pytest/runner.py", line 340, in from_call
  |     result: Optional[TResult] = func()
  |                                 ^^^^^^
  |   File "/Users/user/PycharmProjects/core-platform-automation/venv/lib/python3.12/site-packages/_pytest/runner.py", line 240, in <lambda>
  |     lambda: runtest_hook(item=item, **kwds), when=when, reraise=reraise
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/Users/user/PycharmProjects/core-platform-automation/venv/lib/python3.12/site-packages/pluggy/_hooks.py", line 513, in __call__
  |     return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[... truncated for brevity ...]
  |   File "/Users/user/PycharmProjects/core-platform-automation/venv/lib/python3.12/site-packages/pytest_reportportal/plugin.py", line 339, in pytest_fixture_post_finalizer
  |     exception = fixturedef.cached_result[2][0]
  |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
  | TypeError: 'Skipped' object is not subscriptable

Additional Context

The issue appears to be related to how pytest-reportportal handles the Skipped exception object in fixture teardown. The plugin attempts to access the skip exception as if it were a sequence (cached_result[2][0]), but Skipped objects don't support indexing.

Possible Solutions

  1. Check if cached_result[2] is a Skipped object before attempting to index it
  2. Handle skip exceptions differently in the pytest_fixture_post_finalizer hook

andrewidqa avatar Oct 30 '24 18:10 andrewidqa