forked from test_framework/pytest-bdd
Merge pull request #6 from olegpidsadnyi/feature-base-path
base path for feature files
This commit is contained in:
commit
d3b63d33de
22
README.md
22
README.md
|
@ -3,6 +3,7 @@ BDD library for the py.test runner
|
|||
|
||||
[![Build Status](https://api.travis-ci.org/olegpidsadnyi/pytest-bdd.png)](https://travis-ci.org/olegpidsadnyi/pytest-bdd)
|
||||
|
||||
|
||||
Install pytest-bdd
|
||||
=================
|
||||
|
||||
|
@ -85,6 +86,7 @@ author of the article, but article should have some default author.
|
|||
Given I'm the admin
|
||||
And there is an article
|
||||
|
||||
|
||||
Reuse fixtures
|
||||
================
|
||||
|
||||
|
@ -129,6 +131,26 @@ test_common.py:
|
|||
There are no definitions of the steps in the test file. They were collected from the parent
|
||||
conftests.
|
||||
|
||||
|
||||
Feature file paths
|
||||
==================
|
||||
|
||||
But default, pytest-bdd will use current module's path as base path for finding feature files, but this behaviour can
|
||||
be changed by having fixture named 'pytestbdd_feature_base_dir' which should return the new base path.
|
||||
|
||||
test_publish_article.py:
|
||||
|
||||
import pytest
|
||||
from pytest_bdd import scenario
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pytestbdd_feature_base_dir():
|
||||
return '/home/user/projects/foo.bar/features'
|
||||
|
||||
test_publish = scenario('publish_article.feature', 'Publishing the article')
|
||||
|
||||
|
||||
Subplugins
|
||||
==========
|
||||
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
"""Pytest plugin entry point. Used for any fixtures needed."""
|
||||
|
||||
import os.path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pytestbdd_feature_base_dir(request):
|
||||
return os.path.dirname(request.module.__file__)
|
|
@ -10,7 +10,6 @@ test_publish_article = scenario(
|
|||
scenario_name='Publishing the article',
|
||||
)
|
||||
"""
|
||||
|
||||
import inspect
|
||||
from os import path as op
|
||||
|
||||
|
@ -26,7 +25,8 @@ def scenario(feature_name, scenario_name):
|
|||
|
||||
def _scenario(request):
|
||||
# Get the feature
|
||||
feature_path = op.abspath(op.join(op.dirname(request.module.__file__), feature_name))
|
||||
base_path = request.getfuncargvalue('pytestbdd_feature_base_dir')
|
||||
feature_path = op.abspath(op.join(base_path, feature_name))
|
||||
feature = Feature.get_feature(feature_path)
|
||||
|
||||
# Get the scenario
|
||||
|
|
|
@ -107,6 +107,8 @@ def _step_decorator(step_type, step_name):
|
|||
frame = inspect.stack()[1]
|
||||
module = inspect.getmodule(frame[0])
|
||||
if step_type == GIVEN:
|
||||
if not hasattr(func, '_pytestfixturefunction'):
|
||||
# avoid overfixturing of a fixture
|
||||
func = pytest.fixture(func)
|
||||
step_func = lambda request: request.getfuncargvalue(func.func_name)
|
||||
|
||||
|
|
6
setup.py
6
setup.py
|
@ -29,6 +29,12 @@ setup(
|
|||
install_requires=[
|
||||
'pytest',
|
||||
],
|
||||
# the following makes a plugin available to py.test
|
||||
entry_points = {
|
||||
'pytest11': [
|
||||
'pytest-bdd = pytest_bdd.plugin',
|
||||
]
|
||||
},
|
||||
tests_require=['mock'],
|
||||
packages=['pytest_bdd'],
|
||||
)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
"""Configuration for pytest runner."""
|
||||
|
||||
from pytest_bdd import given, when
|
||||
from pytest_bdd.plugin import pytestbdd_feature_base_dir
|
||||
|
||||
pytest_plugins = 'pytester'
|
||||
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
"""Test feature base dir."""
|
||||
import os.path
|
||||
|
||||
import pytest
|
||||
|
||||
from pytest_bdd import scenario
|
||||
|
||||
|
||||
@pytest.fixture(params=[
|
||||
'When step can be the first',
|
||||
])
|
||||
def scenario_name(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pytestbdd_feature_base_dir():
|
||||
return '/does/not/exist'
|
||||
|
||||
|
||||
def test_feature_path(request, scenario_name):
|
||||
"""Test feature base dir."""
|
||||
sc = scenario('steps.feature', scenario_name)
|
||||
with pytest.raises(IOError) as exc:
|
||||
sc(request)
|
||||
|
||||
assert os.path.join('/does/not/exist/', 'steps.feature') in str(exc.value)
|
Loading…
Reference in New Issue