pytest-dev / pytest-bdd

BDD library for the pytest runner
https://pytest-bdd.readthedocs.io/en/latest/
MIT License
1.31k stars 221 forks source link

ScenarioNotFound (Python 2.7.8, virtualenv) #68

Closed ghost closed 10 years ago

ghost commented 10 years ago

I had the same setup working in an existing project, but in a new one I'm working on from scrtach BDD style, I get this error message,

==================================== ERRORS ====================================
______________ ERROR collecting tests/functional/test_landing.py _______________
tests/functional/test_landing.py:5: in <module>
    @scenario('landing.feature', 'Landing')
../../.virtualenvs/flask-todomvc/lib/python2.7/site-packages/pytest_bdd/scenario.py:266: in scenario
    'Scenario "{0}" in feature "{1}" is not found.'.format(scenario_name, feature_name)
E   ScenarioNotFound: Scenario "Landing" in feature "landing.feature" is not found.

You can see that the landing.feature file was found, though not the particular scenario, which is written like this (in features/landing.feature):

Scenario: Landing
  When I go to the home page
  Then I should see "Welcome to Home"

and in test_landing.py, I have:

# -*- coding: utf-8 -*-
from pytest_bdd import scenario

@scenario('landing.feature', 'Landing')
def test_landing_page():
    pass
bubenkoff commented 10 years ago

features folder should be then mentioned in the pytestbdd_feature_base_dir which you'll need to override http://pytest-bdd.readthedocs.org/en/latest/#feature-file-paths

default behavior is to consider the test files's folder as an anchor to look up the feature file

ghost commented 10 years ago

Thanks for the quick reply.

I have it setup like the following (in tests/functional/conftest.py), and I believe that the feature file itself is beeing picked up, but not the scenario within... or did I get it all wrong?

# -*- coding: utf-8 -*-
import os

import pytest

@pytest.fixture
def pytestbdd_feature_base_dir():
    """Feature files base directory."""
    return os.path.abspath(
        os.path.join(
            os.path.dirname(os.path.dirname("__file__")),  # can only go up to root package
            'features',
        )
    )

PS had to quote "__file__" because I was getting an error about an undefined global. The path is correct and pointing to the project/features directory.

bubenkoff commented 10 years ago

nope, you have to unquote it of course it is defined global for sure

bubenkoff commented 10 years ago
@pytest.fixture  # pragma: no cover
def pytestbdd_feature_base_dir(request):
    """Base feature directory."""
    return os.path.dirname(request.module.__file__)

this is the default implementation of this fixture as you see, file is used also, but the module of the test, while you need a fixed folder like:

import os.path

import project

@pytest.fixture
def pytestbdd_feature_base_dir(request):
    """Base feature directory."""
    return os.path.join(os.path.dirname(project.__file__), 'features')

if file structure is like:

 root
    --\
     features
     --\
       some_feature.feature
     tests
      --\
        __init__.py
       conftest.py
       test_some.py
      project
      --\
        __init__.py
       code.py
ghost commented 10 years ago

This is what I'm getting unquoting __file__... Otherwise, I've got the directory structure setup the way you've recomended.

 py.test -f
============================= test session starts ==============================
platform linux2 -- Python 2.7.8 -- py-1.4.24 -- pytest-2.6.2
plugins: flask, bdd, bdd, xdist
collected 0 items / 1 errors
collected 0 items / 1 errors

==================================== ERRORS ====================================
______________ ERROR collecting tests/functional/test_landing.py _______________
tests/functional/test_landing.py:5: in <module>
    @scenario('landing.feature', 'Landing')
../../.virtualenvs/flask-todomvc/lib/python2.7/site-packages/pytest_bdd/scenario.py:258: in scenario
    base_path = get_fixture(caller_module, 'pytestbdd_feature_base_dir')
../../.virtualenvs/flask-todomvc/lib/python2.7/site-packages/pytest_bdd/scenario.py:195: in get_fixture
    return call_fixture(globs[fixture])
../../.virtualenvs/flask-todomvc/lib/python2.7/site-packages/pytest_bdd/scenario.py:182: in call_fixture
    return function(*args)
tests/functional/conftest.py:12: in pytestbdd_feature_base_dir
    os.path.dirname(os.path.dirname(__file__)),  # can only go up to root package
E   NameError: global name '__file__' is not defined

In respect to 'ScenarioNotFound', I get this error even if I hard code the path ie return "/home/uri/[project-root]/features"

bubenkoff commented 10 years ago

did you put init.py near the test_landing.py? would be nice if you upload the whole story somewhere :)

ghost commented 10 years ago

I've uploaded it up here https://github.com/usharf/flask-todomvc I leave py.test -f to run it, using pytest-xdist

login.feature is there as a skeleton and not in use

bubenkoff commented 10 years ago
diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py
index 4692865..08dda65 100644
--- a/tests/functional/conftest.py
+++ b/tests/functional/conftest.py
@@ -1,11 +1,12 @@
 # -*- coding: utf-8 -*-
-import os
-from os.path import abspath
+import os.path

 import pytest

+import tests
+

 @pytest.fixture
 def pytestbdd_feature_base_dir(request):
     """Feature files base directory."""
-    return "/home/uri/labs/flask-todomvc/features"
+    return os.path.join(os.path.dirname(tests.__file__), '..', 'features')
diff --git a/tests/functional/test_landing.py b/tests/functional/test_landing.py
index adb042a..5ef6563 100644
--- a/tests/functional/test_landing.py
+++ b/tests/functional/test_landing.py
@@ -2,6 +2,6 @@
 from pytest_bdd import scenario

-@scenario('landing.feature', 'Landing')
+@scenario('landing.feature', 'Succesful landing')
 def test_landing_page():
     pass
bubenkoff commented 10 years ago

this is the what you need to do

bubenkoff commented 10 years ago

Scenario "Landing" in feature "landing.feature" happened because you had 'Succesful landing'

ghost commented 10 years ago

I have the stories file opened in Emacs with the scenario as "Landing", yet the comitted work, which I though I've just saved, still has the old name... I've it changed while investigating this issue, but clearly have not saved...

Thanks and I'm really sorry about this. It's now moving along and I can continue with the steps.

Cheers