Pyramid; Views not found in unit-tests

30 views
Skip to first unread message

Gerhard Schmidt

unread,
Aug 10, 2018, 11:27:12 AM8/10/18
to pylons-discuss
Hi,

I'm writing a pyramid software using traversal with quite some complex
ways of finding views and testing permissions to the view.

Everything works quite well in the running pyramid server but in
unit-tests the views that are there aren't found.

I use the following code to get the callable of a view.

adapters = registry.adapters
context_iface = providedBy(context)
view_callable = adapters.lookup(
(IViewClassifier, request.request_iface, context_iface),
IView, name=request.view_name, default=None)

In the running server this code works fine, in unit-tests it returns
None all the time.

I use a request object derived from pyramid.testing.DummyRequest.

Can anybody give me a pointer what I'm doing wrong.

Regards
Estartu

signature.asc

Oliver

unread,
Aug 10, 2018, 2:59:03 PM8/10/18
to pylons-...@googlegroups.com
You should include your route setup before, so that your registry gets populated.

I usually use pytest and its fixtures to have access to everything I need in testing, e.g. something like this:

import pytest

@pytest.fixture(scope='session')
def test_settings():
import plaster
test_settings = plaster.get_settings('testing.ini', 'app:main')
return test_settings


@pytest.fixture
def test_registry():
from pyramid import registry
reg = registry.Registry('testing')
return reg


@pytest.fixture
def test_request(test_registry):
from pyramid import request
req = request.Request({})
req.registry = test_registry
return req


@pytest.fixture
def test_config(test_settings, test_registry, test_request):
from pyramid import testing
config = testing.setUp(registry=test_registry, request=test_request, settings=test_settings)
yield config
testing.tearDown()


def test_foobar(test_config):
test_config.include('your.feature')
# assert something


test_settings fixture lives normally in the root conftest.py and the others where I need them.



cheers
Oliver
signature.asc

Gerhard Schmidt

unread,
Aug 18, 2018, 5:13:14 AM8/18/18
to pylons-discuss
Hi,

found the error myself. The problem was that I have my tests outside the
product. So doing a config.scan only scans the test product and not the
Pyramid product. Adding a config.scan('<pyramidproductname>') did the
trick.

Regards
Estartu

Jonathan Mackenzie

unread,
Aug 18, 2018, 9:55:40 PM8/18/18
to pylons-discuss
I found that webtest works pretty well. Doesn't require you to use config.scan() and just makes your app as per normal. https://docs.pylonsproject.org/projects/webtest/en/latest/
Reply all
Reply to author
Forward
0 new messages