pytest-dev / pytest-dev/pytest-bdd
Request for Supporting Dynamic Import in pytest_bdd_before_scenario
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 251
- Avg merge
- 43m
- Merged PRs (30d)
- 2
Description
I would like to request support for dynamically importing step definitions in the pytest_bdd_before_scenario hook. This feature would greatly enhance the flexibility and modularity of test setups, especially in large projects with multiple feature files and step definitions.
Current Behavior: Currently, pytest-bdd does not provide a straightforward way to dynamically import step definitions based on the feature file being executed. While it is possible to use importlib to import modules dynamically, the step definitions are not registered correctly, leading to issues where steps are not found during test execution.
Proposed Enhancement: Enhance the pytest_bdd_before_scenario hook to support dynamic import and registration of step definitions. This could involve:
- Allowing dynamic import of step definition modules based on the feature file path or tags.
- Ensuring that dynamically imported step definitions are correctly registered and recognized by pytest-bdd.
Use Case: In large projects with multiple feature files and step definitions, it is often necessary to organize step definitions into separate modules. Dynamically importing and registering these step definitions based on the feature file being executed would:
- Improve modularity and maintainability of the test code.
- Reduce the need for importing all step definitions globally, which can lead to namespace conflicts and increased memory usage.
- Allow for more flexible and scalable test setups.
Example Scenario: Consider a project with the following structure:
project_root/
tests/
features/
feature_a/
feature_a.feature
feature_b/
feature_b.feature
steps/
test_feature_a.py
test_feature_b.py
conftest.py
hooks.py
In the hooks.py file, we would like to dynamically import and register the appropriate step definitions based on the feature file being executed:
import importlib
from pytest_bdd import before_scenario
def pytest_bdd_before_scenario(request, feature, scenario):
feature_path = feature.filename
if 'feature_a' in feature_path:
importlib.import_module('tests.steps.test_feature_a')
elif 'feature_b' in feature_path:
importlib.import_module('tests.steps.test_feature_b')
With the proposed enhancement, pytest-bdd would ensure that the dynamically imported step definitions are correctly registered and recognized during test execution.
Thank you for considering this enhancement request. I look forward to your feedback and hope to see this feature implemented in a future release.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the pytest_bdd_before_scenario hook described in hooks.py and compare it with the example conftest.py and step modules under tests/steps/. Reproduce the feature_a and feature_b scenario layout, then investigate how dynamically imported definitions are registered. Done means imports selected by feature path or tags are recognized during execution without requiring all step modules to be imported globally.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100