Merge pull request #6 from olegpidsadnyi/feature-base-path

base path for feature files
This commit is contained in:
Oleg Pidsadnyi 2013-05-29 11:35:29 -07:00
commit d3b63d33de
7 changed files with 71 additions and 3 deletions

View File

@ -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
==========

10
pytest_bdd/plugin.py Normal file
View File

@ -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__)

View 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

View File

@ -107,7 +107,9 @@ def _step_decorator(step_type, step_name):
frame = inspect.stack()[1]
module = inspect.getmodule(frame[0])
if step_type == GIVEN:
func = pytest.fixture(func)
if not hasattr(func, '_pytestfixturefunction'):
# avoid overfixturing of a fixture
func = pytest.fixture(func)
step_func = lambda request: request.getfuncargvalue(func.func_name)
step_func.__name__ = step_name

View File

@ -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'],
)

View File

@ -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'

View File

@ -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)