Testing a view#
Another base Plone feature that we can test is a View.
Create a new view#
Create a new view with plonecli:
cd plonetraining.testing
plonecli add view
Follow the prompts and create a new view with the TestingItemView
Python class and a matching template.
This new view will be automatically registered in our package.
Test the view#
plonecli
creates basic tests (in the test_view_testing_item_view.py
file).
Inspecting this file, we see something like this:
class ViewsIntegrationTest(unittest.TestCase):
layer = PLONETRAINING_TESTING_INTEGRATION_TESTING
def setUp(self):
self.portal = self.layer['portal']
self.request = self.layer['request']
setRoles(self.portal, TEST_USER_ID, ['Manager'])
api.content.create(self.portal, 'Folder', 'other-folder')
api.content.create(self.portal, 'Document', 'front-page')
def test_testing_item_view_is_registered(self):
view = getMultiAdapter(
(self.portal['other-folder'], self.portal.REQUEST),
name='testing-item-view'
)
self.assertTrue(view.__name__ == 'testing-item-view')
# self.assertTrue(
# 'Sample View' in view(),
# 'Sample View is not found in testing-item-view'
# )
def test_testing_item_view_not_matching_interface(self):
with self.assertRaises(ComponentLookupError):
getMultiAdapter(
(self.portal['front-page'], self.portal.REQUEST),
name='testing-item-view',
)
In setUp
we are creating sample content that we will use in tests.
The first test (test_testing_item_view_is_registered
) tries to call the view on a Folder and checks that everything works well and that we get the correct view.
The second test (test_testing_item_view_not_matching_interface
) tries to call the view on a non-folderish content item (a Document) and checks that this raises an Exception.
If we take a look at the configure.zcml file where the view is registered, we can see that the view is registered only for folderish types. We want to test that this registration is correct.
We can test several things about a view:
If it is available only for a certain type of object
If it renders as we expect, by calling the view in an Integration test, or using the browser in a Functional test
If its methods return what we expect, by calling methods directly from the view instance
Exercise 1#
We want to use this view only for our content type and not for all folderish ones (also because TestingItem isn't a folderish type).
Change the view registration to be available only for our type
Update tests to check that we can call it only on a TestingItem content
The view template prints a string that is returned from its class. Write a test that checks this string.
Note
plonecli uses getMultiAdapter
to obtain a view and we use this for consistency with these pre-created tests, but the preferred way of obtaining a view is via plone.api.
Exercise 2#
Add a method in the view that gets a parameter from the request (
message
) and returns it.Check the method in the integration test
Update the template to print the value from that method
Test that calling the method from the view returns what we expect
Write a functional test to test browser integration