Do not use deprecated MarkInfo objects on pytest>=3.6 (#262)

* Do not use deprecated MarkInfo objects on pytest>=3.6

* Provide way to get all parametrize args, not only the ones from the closest marker

* Fix for remaining tests that were using depracated MarkInfo objects
This commit is contained in:
Milosz Sliwinski 2018-11-14 10:24:04 +01:00 committed by Alessio Bogon
parent f1a3e45e51
commit 5678a5c84e
4 changed files with 75 additions and 34 deletions

View File

@ -7,6 +7,7 @@ that enriches the pytest test reporting.
import time
from .feature import force_unicode
from .utils import get_parametrize_markers_args
class StepReport(object):
@ -73,11 +74,11 @@ class ScenarioReport(object):
self.scenario = scenario
self.step_reports = []
self.param_index = None
parametrize = node.keywords._markers.get('parametrize')
if parametrize and scenario.examples:
param_names = parametrize.args[0] if isinstance(parametrize.args[0], (tuple, list)) else [
parametrize.args[0]]
param_values = parametrize.args[1]
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]
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)

View File

@ -84,3 +84,29 @@ def get_request_fixture_names(request):
Compatibility with pytest 3.0.
"""
return request._pyfuncitem._fixtureinfo.names_closure
def get_parametrize_markers_args(node):
"""In pytest 3.6 new API to access markers has been introduced and it deprecated
MarkInfo objects.
This function uses that API if it is available otherwise it uses MarkInfo objects.
"""
mark_name = 'parametrize'
try:
return get_markers_args_using_iter_markers(node, mark_name)
except AttributeError:
return get_markers_args_using_get_marker(node, mark_name)
def get_markers_args_using_iter_markers(node, mark_name):
"""Recommended on pytest>=3.6"""
args = []
for mark in node.iter_markers(mark_name):
args += mark.args
return tuple(args)
def get_markers_args_using_get_marker(node, mark_name):
"""Deprecated on pytest>=3.6"""
return getattr(node.get_marker(mark_name), 'args', ())

View File

@ -6,6 +6,7 @@ import pytest
from pytest_bdd import given, when, then, scenario
from pytest_bdd import exceptions
from pytest_bdd.utils import get_parametrize_markers_args
@scenario(
@ -13,8 +14,8 @@ from pytest_bdd import exceptions
'Outlined given, when, thens',
example_converters=dict(start=int, eat=float, left=str)
)
def test_outlined():
assert test_outlined.parametrize.args == (
def test_outlined(request):
assert get_parametrize_markers_args(request.node) == (
[u'start', u'eat', u'left'], [[12, 5.0, '7'], [5, 4.0, '1']])
@ -133,35 +134,12 @@ def test_outlined_with_other_fixtures(other_fixture):
'Outlined with vertical example table',
example_converters=dict(start=int, eat=float, left=str)
)
def test_vertical_example():
def test_vertical_example(request):
"""Test outlined scenario with vertical examples table."""
assert test_vertical_example.parametrize.args == (
assert get_parametrize_markers_args(request.node) == (
[u'start', u'eat', u'left'], [[12, 5.0, '7'], [2, 1.0, '1']])
def test_empty_example_values():
"""Test outlined scenario with empty example values."""
@scenario(
'outline.feature',
'Outlined with empty example values',
)
def test_scenario():
pass
assert test_scenario.parametrize.args == (
[u'start', u'eat', u'left'], [['#', '', '']])
@scenario(
'outline.feature',
'Outlined with empty example values vertical',
)
def test_scenario():
pass
assert test_scenario.parametrize.args == (
[u'start', u'eat', u'left'], [['#', '', '']])
@given('there are <start> <fruits>')
def start_fruits(start, fruits):
assert isinstance(start, int)
@ -187,8 +165,8 @@ def should_have_left_fruits(start_fruits, start, eat, left, fruits):
'Outlined given, when, thens',
example_converters=dict(start=int, eat=float, left=str)
)
def test_outlined_feature():
assert test_outlined_feature.parametrize.args == (
def test_outlined_feature(request):
assert get_parametrize_markers_args(request.node) == (
['start', 'eat', 'left'],
[[12, 5.0, '7'], [5, 4.0, '1']],
['fruits'],

View File

@ -0,0 +1,36 @@
"""Scenario Outline with empty example values tests."""
from pytest_bdd import given, scenario, then, when
from pytest_bdd.utils import get_parametrize_markers_args
@given('there are <start> cucumbers')
def start_cucumbers(start):
pass
@when('I eat <eat> cucumbers')
def eat_cucumbers(eat):
pass
@then('I should have <left> cucumbers')
def should_have_left_cucumbers(left):
pass
@scenario(
'outline.feature',
'Outlined with empty example values',
)
def test_scenario_with_empty_example_values(request):
assert get_parametrize_markers_args(request.node) == (
[u'start', u'eat', u'left'], [['#', '', '']])
@scenario(
'outline.feature',
'Outlined with empty example values vertical',
)
def test_scenario_with_empty_example_values_vertical(request):
assert get_parametrize_markers_args(request.node) == (
[u'start', u'eat', u'left'], [['#', '', '']])