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

Fixes #271: Use parametrize markers params for getting example_kwargs #272

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
32 changes: 21 additions & 11 deletions pytest_bdd/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time

from .feature import force_unicode
from .utils import get_parametrize_markers_args
from .utils import get_parametrize_markers_args, get_parametrize_params


class StepReport(object):
Expand Down Expand Up @@ -73,20 +73,30 @@ def __init__(self, scenario, node):
"""
self.scenario = scenario
self.step_reports = []
self.param_index = None

parametrize_args = get_parametrize_markers_args(node)
if parametrize_args and scenario.examples:
param_names = parametrize_args[0] if isinstance(parametrize_args[0], (tuple, list)) else [
parametrize_args[0]]
param_values = parametrize_args[1]
params = get_parametrize_params(parametrize_args)

self.param_index = self.get_param_index(node, params)
self.example_kwargs = self.get_example_kwargs(node, params)

def get_param_index(self, node, params):
if params:
param_names = params[0]['names']
param_values = params[0]['values']
node_param_values = [node.funcargs[param_name] for param_name in param_names]
if node_param_values in param_values:
self.param_index = param_values.index(node_param_values)
return param_values.index(node_param_values)
elif tuple(node_param_values) in param_values:
self.param_index = param_values.index(tuple(node_param_values))
self.example_kwargs = {
example_param: force_unicode(node.funcargs[example_param])
for example_param in scenario.get_example_params()
return param_values.index(tuple(node_param_values))
return None

def get_example_kwargs(self, node, params):
params_names = (param['names'] for param in params)
all_names = sum(params_names, [])
return {
example_param_name: force_unicode(node.funcargs[example_param_name])
for example_param_name in all_names
}

@property
Expand Down
30 changes: 30 additions & 0 deletions pytest_bdd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,33 @@ def get_markers_args_using_iter_markers(node, mark_name):
def get_markers_args_using_get_marker(node, mark_name):
"""Deprecated on pytest>=3.6"""
return getattr(node.get_marker(mark_name), 'args', ())


def get_parametrize_params(parametrize_args):
"""Group parametrize markers arguments names and values.

:param parametrize_args: parametrize markers arguments.
:return: `list` of `dict` in the form of:
[
{
"names": ["name1", "name2", ...],
"values": [value1, value2, ...],
},
...
]
"""
params = []
for i in range(0, len(parametrize_args), 2):
params.append({
'names': _coerce_list(parametrize_args[i]),
'values': parametrize_args[i+1]
})
return params


def _coerce_list(names):
if not isinstance(names, (tuple, list)):
# As pytest.mark.parametrize has only one param name,
# it is not returned as a list. Convert it to list:
names = [names]
return list(names)
1 change: 1 addition & 0 deletions tests/feature/outline_feature.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Feature: Outline
| start | eat | left |
| 12 | 5 | 7 |
| 5 | 4 | 1 |
| 4 | 2 | 2 |

Scenario Outline: Outlined given, when, thens
Given there are <start> <fruits>
Expand Down
4 changes: 4 additions & 0 deletions tests/feature/parametrized.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ Scenario: Parametrized given, when, thens
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers


Scenario: Parametrized given - single param
Copy link
Contributor

Choose a reason for hiding this comment

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

what is this test asserting? There is no "then" step

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You already have asked that question :). Shall we add comment somewhere in the code explaining what this test is testing?

#272 (comment)

Given there are <start> cucumbers
18 changes: 9 additions & 9 deletions tests/feature/test_gherkin_terminal_reporter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import re


import pytest

from pytest_bdd import scenario, given, when, then
from pytest_bdd import given, scenario, then, when
from tests.utils import get_test_filepath, prepare_feature_and_py_files


Expand Down Expand Up @@ -349,18 +349,18 @@ def output_output_must_contain_parameters_values(test_execution, gherkin_scenari

@pytest.mark.parametrize(
'feature_file, py_file, name', [
('./steps/unicode.feature', './steps/test_unicode.py', 'test_steps_in_feature_file_have_unicode')
('./steps/unicode.feature', './steps/test_unicode.py', 'test_steps_in_feature_file_have_unicode'),
('./feature/parametrized.feature', './feature/test_parametrized.py', 'test_parametrized')
]
)
def test_scenario_in_expanded_mode(testdir, test_execution, feature_file, py_file, name):
def test_scenario_in_expanded_mode(testdir, feature_file, py_file, name):
prepare_feature_and_py_files(testdir, feature_file, py_file)

test_execution['gherkin'] = testdir.runpytest(
'-k %s' % name,
py_filename = os.path.basename(py_file)
result = testdir.runpytest(
'%s::%s' % (py_filename, name),
'--gherkin-terminal-reporter',
'--gherkin-terminal-reporter-expanded',
'-vv',
)

ghe = test_execution['gherkin']
ghe.assert_outcomes(passed=1)
result.assert_outcomes(passed=1)
Copy link
Contributor

Choose a reason for hiding this comment

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

we are asserting that the test pass, but not the behaviour of the terminal report, which is the whole point of this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem that this pull request fixes is that expanded gherkin report was crashing while reporting test_parametrized test.
So this assertion just checks if test was able to run or if there was a crash.

2 changes: 1 addition & 1 deletion tests/feature/test_outline.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def should_have_left_fruits(start_fruits, start, eat, left, fruits):
def test_outlined_feature(request):
assert get_parametrize_markers_args(request.node) == (
['start', 'eat', 'left'],
[[12, 5.0, '7'], [5, 4.0, '1']],
[[12, 5.0, '7'], [5, 4.0, '1'], [4, 2.0, '2']],
youtux marked this conversation as resolved.
Show resolved Hide resolved
['fruits'],
[[u'oranges'], [u'apples']]
)
11 changes: 11 additions & 0 deletions tests/feature/test_parametrized.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ def test_parametrized(request, start, eat, left):
"""Test parametrized scenario."""


@pytest.mark.parametrize(
'start', [12]
)
@scenario(
'parametrized.feature',
'Parametrized given - single param',
)
def test_parametrized_single_param(request, start):
youtux marked this conversation as resolved.
Show resolved Hide resolved
"""Test parametrized scenario."""


@pytest.fixture(params=[1, 2])
def foo_bar(request):
return 'bar' * request.param
Expand Down
13 changes: 11 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ def get_test_filepath(filepath):
return os.path.join(curr_file_dirpath, filepath)


def get_filename_without_ext(path):
filename = os.path.basename(path)
return os.path.splitext(filename)[0]


def prepare_feature_and_py_files(testdir, feature_file, py_file):
Copy link
Contributor

Choose a reason for hiding this comment

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

This function was used only once, and it's implementation was tied to the caller. I would say to remove it and let the caller test_scenario_in_expanded_mode test manage the resources (and let the test be in the pytest-bdd style, not a normal test)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will rewrite it to pytest-bdd style.

I added prepare_feature_and_py_files to be able to reuse feature files that are already in other tests, to test them in expanded mode.

I will remove it, and I'll create separate tests for gherkin reporter. That means that we may have some duplication but it is safer approach.

Copy link
Contributor Author

@sliwinski-milosz sliwinski-milosz Mar 12, 2019

Choose a reason for hiding this comment

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

Tests have been rewritten to pytest-bdd style.

feature_filepath = get_test_filepath(feature_file)
with open(feature_filepath) as feature_file:
feature_content = feature_file.read()
testdir.makefile('.feature', unicode=feature_content)
feature_filename = get_filename_without_ext(feature_file.name)
kwargs = {feature_filename: feature_content}
testdir.makefile('.feature', **kwargs)

py_filepath = get_test_filepath(py_file)
with open(py_filepath) as py_file:
py_content = py_file.read()
testdir.makepyfile(test_gherkin=py_content)
py_filename = get_filename_without_ext(py_file.name)
kwargs = {py_filename: py_content}
testdir.makepyfile(**kwargs)