forked from test_framework/pytest-bdd
extra fix for encoding
This commit is contained in:
parent
05217a897a
commit
2e3357aa9d
|
@ -111,13 +111,20 @@ def _open_file(filename, encoding):
|
|||
return open(filename, 'r', encoding=encoding)
|
||||
|
||||
|
||||
def _force_unicode(string, encoding):
|
||||
if sys.version_info < (3, 0):
|
||||
def force_unicode(string, encoding='utf-8'):
|
||||
if sys.version_info < (3, 0) and isinstance(string, str):
|
||||
return string.decode(encoding)
|
||||
else:
|
||||
return string
|
||||
|
||||
|
||||
def force_encode(string, encoding='utf-8'):
|
||||
if sys.version_info < (3, 0):
|
||||
return string.encode(encoding)
|
||||
else:
|
||||
return string
|
||||
|
||||
|
||||
class Feature(object):
|
||||
"""Feature."""
|
||||
|
||||
|
@ -134,7 +141,7 @@ class Feature(object):
|
|||
prev_mode = None
|
||||
|
||||
with _open_file(filename, encoding) as f:
|
||||
content = _force_unicode(f.read(), encoding)
|
||||
content = force_unicode(f.read(), encoding)
|
||||
for line_number, line in enumerate(content.split('\n')):
|
||||
line = strip(line)
|
||||
if not line:
|
||||
|
|
|
@ -16,7 +16,7 @@ from os import path as op # pragma: no cover
|
|||
|
||||
from _pytest import python
|
||||
|
||||
from pytest_bdd.feature import Feature # pragma: no cover
|
||||
from pytest_bdd.feature import Feature, force_encode # pragma: no cover
|
||||
from pytest_bdd.steps import recreate_function, get_caller_module
|
||||
from pytest_bdd.types import GIVEN
|
||||
|
||||
|
@ -37,7 +37,7 @@ class GivenAlreadyUsed(Exception): # pragma: no cover
|
|||
"""Fixture that implements the Given has been already used."""
|
||||
|
||||
|
||||
def _find_step_function(request, name):
|
||||
def _find_step_function(request, name, encoding):
|
||||
"""Match the step defined by the regular expression pattern.
|
||||
|
||||
:param request: PyTest request object.
|
||||
|
@ -46,8 +46,9 @@ def _find_step_function(request, name):
|
|||
:return: Step function.
|
||||
|
||||
"""
|
||||
|
||||
try:
|
||||
return request.getfuncargvalue(name)
|
||||
return request.getfuncargvalue(force_encode(name, encoding))
|
||||
except python.FixtureLookupError:
|
||||
|
||||
for fixturename, fixturedefs in request._fixturemanager._arg2fixturedefs.items():
|
||||
|
@ -107,7 +108,7 @@ def scenario(feature_name, scenario_name, encoding='utf-8'):
|
|||
givens = set()
|
||||
# Execute scenario steps
|
||||
for step in scenario.steps:
|
||||
step_func = _find_step_function(request, step.name)
|
||||
step_func = _find_step_function(request, step.name, encoding=encoding)
|
||||
|
||||
# Check the step types are called in the correct order
|
||||
if step_func.step_type != step.type:
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
# coding: utf-8
|
||||
"""Tests for testing cases when we have unicode in feature file."""
|
||||
|
||||
import sys
|
||||
import re
|
||||
import pytest
|
||||
import functools
|
||||
from pytest_bdd import scenario, given, then
|
||||
|
||||
scenario = functools.partial(scenario, 'unicode.feature')
|
||||
test_steps_in_feature_file_have_unicode = scenario('Steps in .feature file have unicode')
|
||||
test_steps_in_py_file_have_unicode = scenario('Steps in .py file have unicode')
|
||||
|
||||
pattern = '(?P<content>\'\w+\')'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def string():
|
||||
"""String fixture."""
|
||||
return {'content': ''}
|
||||
|
||||
|
||||
@given(re.compile(r"there is a string with content '{0}'".format('(?P<content>.+)')))
|
||||
def there_is_a_string_with_content(content, string):
|
||||
"""Create string with unicode content"""
|
||||
string['content'] = content
|
||||
|
||||
|
||||
@given("there is an other string with content 'с каким-то контентом'")
|
||||
def there_is_an_other_string_with_content(string):
|
||||
"""Create other string with unicode content"""
|
||||
string['content'] = u"с каким-то контентом"
|
||||
|
||||
|
||||
@then("I should see that the other string equals to content 'с каким-то контентом'")
|
||||
def assert_that_the_other_string_equals_to_content(string):
|
||||
"""Assert that the other string equals to content."""
|
||||
assert string['content'] == u"с каким-то контентом"
|
||||
|
||||
|
||||
@then(re.compile(r"I should see that the string equals to content '(?P<content>.+)'"))
|
||||
def assert_that_the_string_equals_to_content(content, string):
|
||||
"""Assert that the string equals to content."""
|
||||
assert string['content'] == content
|
||||
if sys.version_info < (3, 0):
|
||||
assert isinstance(content, unicode)
|
|
@ -0,0 +1,8 @@
|
|||
Scenario: Steps in .feature file have unicode
|
||||
Given there is a string with content 'с каким-то контентом'
|
||||
Then I should see that the string equals to content 'с каким-то контентом'
|
||||
|
||||
|
||||
Scenario: Steps in .py file have unicode
|
||||
Given there is an other string with content 'с каким-то контентом'
|
||||
Then I should see that the other string equals to content 'с каким-то контентом'
|
Loading…
Reference in New Issue