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

fix step arguments with default value #610

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/pytest_bdd/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from . import exceptions
from .feature import get_feature, get_features
from .steps import StepFunctionContext, get_step_fixture_name, inject_fixture
from .utils import CONFIG_STACK, get_args, get_caller_module_locals, get_caller_module_path
from .utils import CONFIG_STACK, get_args, get_args_with_default_value, get_caller_module_locals, get_caller_module_path

if TYPE_CHECKING:
from typing import Any, Iterable
Expand Down Expand Up @@ -140,6 +140,7 @@ def _execute_step_function(
converters = context.converters
kwargs = {}
args = get_args(context.step_func)
args_with_default = get_args_with_default_value(context.step_func)

try:
parsed_args = context.parser.parse_arguments(step.name)
Expand All @@ -151,7 +152,12 @@ def _execute_step_function(
value = converters[arg](value)
kwargs[arg] = value

kwargs = {arg: kwargs[arg] if arg in kwargs else request.getfixturevalue(arg) for arg in args}
for arg in args:
if not arg in kwargs:
if not arg in args_with_default:
kwargs[arg] = request.getfixturevalue(arg)
else:
kwargs[arg] = args_with_default[arg]
kw["step_func_args"] = kwargs

request.config.hook.pytest_bdd_before_step_call(**kw)
Expand Down
10 changes: 10 additions & 0 deletions src/pytest_bdd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def get_args(func: Callable) -> list[str]:
]


def get_args_with_default_value(func: Callable) -> dict:
"""Get a dictionary of arguments with default value

:param func: The function to inspect
:return: A dictionary of arguments with default value
:rtype: dict"""
params = signature(func).parameters.values()
return {param.name: param.default for param in params if not param.default == param.empty}


def get_caller_module_locals(stacklevel: int = 1) -> dict[str, Any]:
"""Get the caller module locals dictionary.

Expand Down