Skip to content

Commit

Permalink
No need to keep a counter, just try to pop from the stack if any
Browse files Browse the repository at this point in the history
  • Loading branch information
youtux committed Nov 11, 2023
1 parent 825fcf8 commit c232828
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/pytest_bdd/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
from .parser import Feature, Scenario, Step


# Counter to track how many times pytest initialization was started
_INIT_COUNT = 0


def pytest_addhooks(pluginmanager: PytestPluginManager) -> None:
"""Register plugin hooks."""
from pytest_bdd import hooks
Expand Down Expand Up @@ -56,8 +52,6 @@ def _pytest_bdd_example() -> dict:

def pytest_addoption(parser: Parser) -> None:
"""Add pytest-bdd options."""
global _INIT_COUNT
_INIT_COUNT += 1
add_bdd_ini(parser)
cucumber_json.add_options(parser)
generation.add_options(parser)
Expand All @@ -72,19 +66,15 @@ def add_bdd_ini(parser: Parser) -> None:
def pytest_configure(config: Config) -> None:
"""Configure all subplugins."""
CONFIG_STACK.append(config)
assert _INIT_COUNT == len(CONFIG_STACK)
cucumber_json.configure(config)
gherkin_terminal_reporter.configure(config)


def pytest_unconfigure(config: Config) -> None:
"""Unconfigure all subplugins."""
global _INIT_COUNT
assert len(CONFIG_STACK) <= _INIT_COUNT
if len(CONFIG_STACK) == _INIT_COUNT:
if CONFIG_STACK:
CONFIG_STACK.pop()
cucumber_json.unconfigure(config)
_INIT_COUNT -= 1
cucumber_json.unconfigure(config)


@pytest.hookimpl(hookwrapper=True)
Expand Down

2 comments on commit c232828

@n3world
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you will pop too early. If there is more than one config on the stack you will pop the last config even when the current session didn't start and you will end up in a session without a config on the stack

@youtux
Copy link
Contributor Author

@youtux youtux commented on c232828 Nov 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole config stack was just made to make the tests in this project to work (IIRC the config stack was implemented because we saw some test pollution in the test suite of this project).
Projects using this library should not encounter this issue, since pytest_unconfigure is supposed to be called once per pytest invocation.

Please sign in to comment.