Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bdd_features_base_dir is relative to pytest rootdir #573

Merged
merged 2 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Unreleased
- Add ``stacklevel`` param to ``given``, ``when``, ``then``, ``step`` decorators. This allows for programmatic step generation `#548 <https://github.com/pytest-dev/pytest-bdd/pull/548>`_
- Hide pytest-bdd internal method in user tracebacks `#557 <https://github.com/pytest-dev/pytest-bdd/pull/557>`_.
- Make the package PEP 561-compatible `#559 <https://github.com/pytest-dev/pytest-bdd/issues/559>`_ `#563 <https://github.com/pytest-dev/pytest-bdd/pull/563>`_.
- Configuration option ``bdd_features_base_dir`` is interpreted as relative to the `pytest root directory <https://docs.pytest.org/en/latest/reference/customize.html#rootdir>`_ (previously it was relative to the current working directory). `#573 <https://github.com/pytest-dev/pytest-bdd/pull/573>`_


6.0.1
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ then
Feature file paths
------------------

By default, pytest-bdd will use current module's path as base path for finding feature files, but this behaviour can be changed in the pytest configuration file (i.e. `pytest.ini`, `tox.ini` or `setup.cfg`) by declaring the new base path in the `bdd_features_base_dir` key. The path is interpreted as relative to the working directory when starting pytest.
By default, pytest-bdd will use current module's path as base path for finding feature files, but this behaviour can be changed in the pytest configuration file (i.e. `pytest.ini`, `tox.ini` or `setup.cfg`) by declaring the new base path in the `bdd_features_base_dir` key. The path is interpreted as relative to the `pytest root directory <https://docs.pytest.org/en/latest/reference/customize.html#rootdir>`__.
You can also override features base path on a per-scenario basis, in order to override the path for specific tests.

pytest.ini:
Expand Down
7 changes: 5 additions & 2 deletions src/pytest_bdd/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,11 @@ def scenario(


def get_features_base_dir(caller_module_path: str) -> str:
default_base_dir = os.path.dirname(caller_module_path)
return get_from_ini("bdd_features_base_dir", default_base_dir)
d = get_from_ini("bdd_features_base_dir", None)
if d is None:
return os.path.dirname(caller_module_path)
rootdir = CONFIG_STACK[-1].rootpath
return os.path.join(rootdir, d)


def get_from_ini(key: str, default: str) -> str:
Expand Down
15 changes: 15 additions & 0 deletions tests/feature/test_feature_base_dir.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test feature base dir."""
import os

import pytest

NOT_EXISTING_FEATURE_PATHS = [".", "/does/not/exist/"]
Expand All @@ -21,6 +23,19 @@ def test_feature_path_ok(pytester):
result.assert_outcomes(passed=2)


def test_feature_path_ok_running_outside_rootdir(pytester):
base_dir = "features"
prepare_testdir(pytester, base_dir)

old_dir = os.getcwd()
os.chdir("/")
try:
result = pytester.runpytest(pytester.path, "-k", "test_ok_by_ini")
result.assert_outcomes(passed=2)
finally:
os.chdir(old_dir)


def test_feature_path_by_param_not_found(pytester):
"""As param takes precedence even if ini config is correct it should fail
if passed param is incorrect"""
Expand Down