Skip to content

Commit

Permalink
- Remove broken integration test example from testing and source file…
Browse files Browse the repository at this point in the history
…, per Pylons#2172

- Update functional test with explicit instructions and to sync with actual starter scaffold
  • Loading branch information
stevepiercy committed Dec 14, 2015
1 parent 6f34bf8 commit 312aa1b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 57 deletions.
26 changes: 0 additions & 26 deletions docs/narr/MyProject/myproject/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,6 @@ def test_my_view(self):
self.assertEqual(info['project'], 'MyProject')


class ViewIntegrationTests(unittest.TestCase):
def setUp(self):
""" This sets up the application registry with the
registrations your application declares in its ``includeme``
function.
"""
self.config = testing.setUp()
self.config.include('myproject')

def tearDown(self):
""" Clear out the application registry """
testing.tearDown()

def test_my_view(self):
from myproject.views import my_view
request = testing.DummyRequest()
result = my_view(request)
self.assertEqual(result.status, '200 OK')
body = result.app_iter[0]
self.assertTrue('Welcome to' in body)
self.assertEqual(len(result.headerlist), 2)
self.assertEqual(result.headerlist[0],
('Content-Type', 'text/html; charset=UTF-8'))
self.assertEqual(result.headerlist[1], ('Content-Length',
str(len(body))))

class FunctionalTests(unittest.TestCase):
def setUp(self):
from myproject import main
Expand Down
73 changes: 42 additions & 31 deletions docs/narr/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -348,26 +348,6 @@ code's integration with the rest of :app:`Pyramid`.

See also :ref:`including_configuration`

Let's demonstrate this by showing an integration test for a view.

Given the following view definition, which assumes that your application's
:term:`package` name is ``myproject``, and within that :term:`package` there
exists a module ``views``, which in turn contains a :term:`view` function named
``my_view``:

.. literalinclude:: MyProject/myproject/views.py
:linenos:
:lines: 1-6
:language: python

You'd then create a ``tests`` module within your ``myproject`` package,
containing the following test code:

.. literalinclude:: MyProject/myproject/tests.py
:linenos:
:pyobject: ViewIntegrationTests
:language: python

Writing unit tests that use the :class:`~pyramid.config.Configurator` API to
set up the right "mock" registrations is often preferred to creating
integration tests. Unit tests will run faster (because they do less for each
Expand All @@ -388,22 +368,53 @@ package, which provides APIs for invoking HTTP(S) requests to your application.

Regardless of which testing :term:`package` you use, ensure to add a
``tests_require`` dependency on that package to your application's
``setup.py`` file:
``setup.py`` file. Using the project ``MyProject`` generated by the starter
scaffold as described in :doc:`project`, we would insert the following code immediately following the
``requires`` block in the file ``MyProject/setup.py``.

.. literalinclude:: MyProject/setup.py
:linenos:
:emphasize-lines: 26-28,48
:language: python
.. code-block:: ini
:linenos:
:lineno-start: 11
:emphasize-lines: 8-
requires = [
'pyramid',
'pyramid_chameleon',
'pyramid_debugtoolbar',
'waitress',
]
test_requires = [
'webtest',
]
Remember to change the dependency.

.. code-block:: ini
:linenos:
:lineno-start: 39
:emphasize-lines: 2
install_requires=requires,
tests_require=test_requires,
test_suite="myproject",
As always, whenever you change your dependencies, make sure to run the
following command.

.. code-block:: bash
$VENV/bin/python setup.py develop
Let us assume your :term:`package` is named ``myproject`` which contains a
``views`` module, which in turn contains a :term:`view` function ``my_view``
that returns a HTML body when the root URL is invoked:
In your ``MyPackage`` project, your :term:`package` is named ``myproject``
which contains a ``views`` module, which in turn contains a :term:`view`
function ``my_view`` that returns an HTML body when the root URL is invoked:

.. literalinclude:: MyProject/myproject/views.py
:linenos:
:language: python

Then the following example functional test demonstrates invoking the above
The following example functional test demonstrates invoking the above
:term:`view`:

.. literalinclude:: MyProject/myproject/tests.py
Expand All @@ -414,9 +425,9 @@ Then the following example functional test demonstrates invoking the above
When this test is run, each test method creates a "real" :term:`WSGI`
application using the ``main`` function in your ``myproject.__init__`` module,
using :term:`WebTest` to wrap that WSGI application. It assigns the result to
``self.testapp``. In the test named ``test_root``. The ``TestApp``'s ``GET``
``self.testapp``. In the test named ``test_root``, the ``TestApp``'s ``GET``
method is used to invoke the root URL. Finally, an assertion is made that the
returned HTML contains the text ``MyProject``.
returned HTML contains the text ``Pyramid``.

See the :term:`WebTest` documentation for further information about the methods
available to a :class:`webtest.app.TestApp` instance.

0 comments on commit 312aa1b

Please sign in to comment.