diff options
| author | Steve Piercy <web@stevepiercy.com> | 2020-01-01 17:40:46 -0800 |
|---|---|---|
| committer | Steve Piercy <web@stevepiercy.com> | 2020-01-02 23:30:59 -0800 |
| commit | 80e9923262fee4892a750d0d6366b193cb4204ba (patch) | |
| tree | 66a8156cb8be070a437c847a425101b648f690ce | |
| parent | 8b2bb44114e3a724d66035e76051a6910e14657e (diff) | |
| download | pyramid-80e9923262fee4892a750d0d6366b193cb4204ba.tar.gz pyramid-80e9923262fee4892a750d0d6366b193cb4204ba.tar.bz2 pyramid-80e9923262fee4892a750d0d6366b193cb4204ba.zip | |
Resync through Quick Tour after moving tests directory
| -rw-r--r-- | docs/quick_tour.rst | 52 | ||||
| -rw-r--r-- | docs/quick_tour/logging/MANIFEST.in | 3 | ||||
| -rw-r--r-- | docs/quick_tour/logging/pytest.ini | 2 | ||||
| -rw-r--r-- | docs/quick_tour/logging/tests/__init__.py | 0 | ||||
| -rw-r--r-- | docs/quick_tour/logging/tests/test_it.py | 39 | ||||
| -rw-r--r-- | docs/quick_tour/package/MANIFEST.in | 3 | ||||
| -rw-r--r-- | docs/quick_tour/package/pytest.ini | 2 | ||||
| -rw-r--r-- | docs/quick_tour/package/tests/__init__.py | 0 | ||||
| -rw-r--r-- | docs/quick_tour/package/tests/test_it.py | 39 | ||||
| -rw-r--r-- | docs/quick_tour/sessions/MANIFEST.in | 3 | ||||
| -rw-r--r-- | docs/quick_tour/sessions/pytest.ini | 2 | ||||
| -rw-r--r-- | docs/quick_tour/sessions/tests/__init__.py | 0 | ||||
| -rw-r--r-- | docs/quick_tour/sessions/tests/test_it.py | 39 | ||||
| -rw-r--r-- | docs/quick_tour/sqla_demo/MANIFEST.in | 3 | ||||
| -rw-r--r-- | docs/quick_tour/sqla_demo/pytest.ini | 7 | ||||
| -rw-r--r-- | docs/quick_tour/sqla_demo/tests/__init__.py | 0 | ||||
| -rw-r--r-- | docs/quick_tour/sqla_demo/tests/test_it.py | 66 |
17 files changed, 230 insertions, 30 deletions
diff --git a/docs/quick_tour.rst b/docs/quick_tour.rst index e6914337f..b6e456d2b 100644 --- a/docs/quick_tour.rst +++ b/docs/quick_tour.rst @@ -676,15 +676,15 @@ the relevant ``.ini`` configuration file. :ref:`Quick Tutorial pyramid_debugtoolbar <qtut_debugtoolbar>` and :ref:`pyramid_debugtoolbar <toolbar:overview>` -Unit tests and ``pytest`` -========================= +Unit and functional tests and ``pytest`` +======================================== Yikes! We got this far and we haven't yet discussed tests. This is particularly egregious, as Pyramid has had a deep commitment to full test coverage since before its release. -Our ``pyramid-cookiecutter-starter`` cookiecutter generated a ``tests.py`` module with -one unit test and one functional test in it. It also configured ``setup.py`` with test requirements: +Our ``pyramid-cookiecutter-starter`` cookiecutter generated a ``test_it.py`` module inside the ``tests`` package with two unit tests and two functional tests in it. +It also configured ``setup.py`` with test requirements: ``pytest`` as the test runner, ``WebTest`` for running view tests, and the ``pytest-cov`` tool which yells at us for code that isn't tested: @@ -709,33 +709,35 @@ This yields the following output. .. code-block:: text =========================== test session starts =========================== - platform darwin -- Python 3.6.0, pytest-3.0.5, py-1.4.32, pluggy-0.4.0 - rootdir: /Users/stevepiercy/hello_world, inifile: pytest.ini - plugins: cov-2.4.0 - collected 2 items - - hello_world/tests.py .. - - ------------- coverage: platform darwin, python 3.6.0-final-0 ------------- - Name Stmts Miss Cover Missing - ----------------------------------------------------------------------- - hello_world/__init__.py 8 0 100% - hello_world/views.py 3 0 100% - ----------------------------------------------------------------------- - TOTAL 11 0 100% - - - ========================= 2 passed in 1.37 seconds ========================= + platform darwin -- Python 3.7.3, pytest-5.3.2, py-1.8.1, pluggy-0.13.1 + rootdir: /<somepath>/hello_world, inifile: pytest.ini, testpaths: hello_world, tests + plugins: cov-2.8.1 + collected 4 items + + tests/test_it.py .... [100%] + + ---------- coverage: platform darwin, python 3.7.3-final-0 ----------- + Name Stmts Miss Cover Missing + ------------------------------------------------------------- + hello_world/__init__.py 7 0 100% + hello_world/routes.py 3 0 100% + hello_world/views/__init__.py 0 0 100% + hello_world/views/default.py 3 0 100% + hello_world/views/notfound.py 4 0 100% + ------------------------------------------------------------- + TOTAL 17 0 100% + + ======================== 4 passed in 0.65 seconds ========================= Our tests passed, and its coverage is complete. What did our test look like? -.. literalinclude:: quick_tour/package/hello_world/tests.py +.. literalinclude:: quick_tour/package/hello_world/tests/test_it.py :language: python :linenos: -Pyramid supplies helpers for test writing, which we use in the test setup and -teardown. Our first test imports the view, makes a dummy request, and sees if the -view returns what we expected. Our second test verifies that the response body from a request to the web root contains what we expected. +Pyramid supplies helpers for test writing, which we use in the test setup and teardown. +Our view tests import the view, make a dummy request, and sees if the view returns what we expected. +Our functional tests verify that the response body from a request to the web root contains what we expected and that the expected response code for making a request to ``/badurl`` results in ``404``. .. seealso:: See also: :ref:`Quick Tutorial Unit Testing <qtut_unit_testing>`, :ref:`Quick diff --git a/docs/quick_tour/logging/MANIFEST.in b/docs/quick_tour/logging/MANIFEST.in index a75da6dad..7a73762f7 100644 --- a/docs/quick_tour/logging/MANIFEST.in +++ b/docs/quick_tour/logging/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include hello_world *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/quick_tour/logging/pytest.ini b/docs/quick_tour/logging/pytest.ini index b419855e1..f707d54e4 100644 --- a/docs/quick_tour/logging/pytest.ini +++ b/docs/quick_tour/logging/pytest.ini @@ -1,3 +1,3 @@ [pytest] testpaths = hello_world -python_files = test*.py +python_files = *.py diff --git a/docs/quick_tour/logging/tests/__init__.py b/docs/quick_tour/logging/tests/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/docs/quick_tour/logging/tests/__init__.py diff --git a/docs/quick_tour/logging/tests/test_it.py b/docs/quick_tour/logging/tests/test_it.py new file mode 100644 index 000000000..90c6302fe --- /dev/null +++ b/docs/quick_tour/logging/tests/test_it.py @@ -0,0 +1,39 @@ +import unittest + +from pyramid import testing + + +class ViewTests(unittest.TestCase): + def setUp(self): + self.config = testing.setUp() + + def tearDown(self): + testing.tearDown() + + def test_my_view(self): + from hello_world.views.default import my_view + request = testing.DummyRequest() + info = my_view(request) + self.assertEqual(info['project'], 'hello_world') + + def test_notfound_view(self): + from hello_world.views.notfound import notfound_view + request = testing.DummyRequest() + info = notfound_view(request) + self.assertEqual(info, {}) + + +class FunctionalTests(unittest.TestCase): + def setUp(self): + from hello_world import main + app = main({}) + from webtest import TestApp + self.testapp = TestApp(app) + + def test_root(self): + res = self.testapp.get('/', status=200) + self.assertTrue(b'Pyramid' in res.body) + + def test_notfound(self): + res = self.testapp.get('/badurl', status=404) + self.assertTrue(res.status_code == 404) diff --git a/docs/quick_tour/package/MANIFEST.in b/docs/quick_tour/package/MANIFEST.in index a75da6dad..7a73762f7 100644 --- a/docs/quick_tour/package/MANIFEST.in +++ b/docs/quick_tour/package/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include hello_world *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/quick_tour/package/pytest.ini b/docs/quick_tour/package/pytest.ini index b419855e1..f707d54e4 100644 --- a/docs/quick_tour/package/pytest.ini +++ b/docs/quick_tour/package/pytest.ini @@ -1,3 +1,3 @@ [pytest] testpaths = hello_world -python_files = test*.py +python_files = *.py diff --git a/docs/quick_tour/package/tests/__init__.py b/docs/quick_tour/package/tests/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/docs/quick_tour/package/tests/__init__.py diff --git a/docs/quick_tour/package/tests/test_it.py b/docs/quick_tour/package/tests/test_it.py new file mode 100644 index 000000000..90c6302fe --- /dev/null +++ b/docs/quick_tour/package/tests/test_it.py @@ -0,0 +1,39 @@ +import unittest + +from pyramid import testing + + +class ViewTests(unittest.TestCase): + def setUp(self): + self.config = testing.setUp() + + def tearDown(self): + testing.tearDown() + + def test_my_view(self): + from hello_world.views.default import my_view + request = testing.DummyRequest() + info = my_view(request) + self.assertEqual(info['project'], 'hello_world') + + def test_notfound_view(self): + from hello_world.views.notfound import notfound_view + request = testing.DummyRequest() + info = notfound_view(request) + self.assertEqual(info, {}) + + +class FunctionalTests(unittest.TestCase): + def setUp(self): + from hello_world import main + app = main({}) + from webtest import TestApp + self.testapp = TestApp(app) + + def test_root(self): + res = self.testapp.get('/', status=200) + self.assertTrue(b'Pyramid' in res.body) + + def test_notfound(self): + res = self.testapp.get('/badurl', status=404) + self.assertTrue(res.status_code == 404) diff --git a/docs/quick_tour/sessions/MANIFEST.in b/docs/quick_tour/sessions/MANIFEST.in index a75da6dad..7a73762f7 100644 --- a/docs/quick_tour/sessions/MANIFEST.in +++ b/docs/quick_tour/sessions/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include hello_world *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/quick_tour/sessions/pytest.ini b/docs/quick_tour/sessions/pytest.ini index b419855e1..f707d54e4 100644 --- a/docs/quick_tour/sessions/pytest.ini +++ b/docs/quick_tour/sessions/pytest.ini @@ -1,3 +1,3 @@ [pytest] testpaths = hello_world -python_files = test*.py +python_files = *.py diff --git a/docs/quick_tour/sessions/tests/__init__.py b/docs/quick_tour/sessions/tests/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/docs/quick_tour/sessions/tests/__init__.py diff --git a/docs/quick_tour/sessions/tests/test_it.py b/docs/quick_tour/sessions/tests/test_it.py new file mode 100644 index 000000000..90c6302fe --- /dev/null +++ b/docs/quick_tour/sessions/tests/test_it.py @@ -0,0 +1,39 @@ +import unittest + +from pyramid import testing + + +class ViewTests(unittest.TestCase): + def setUp(self): + self.config = testing.setUp() + + def tearDown(self): + testing.tearDown() + + def test_my_view(self): + from hello_world.views.default import my_view + request = testing.DummyRequest() + info = my_view(request) + self.assertEqual(info['project'], 'hello_world') + + def test_notfound_view(self): + from hello_world.views.notfound import notfound_view + request = testing.DummyRequest() + info = notfound_view(request) + self.assertEqual(info, {}) + + +class FunctionalTests(unittest.TestCase): + def setUp(self): + from hello_world import main + app = main({}) + from webtest import TestApp + self.testapp = TestApp(app) + + def test_root(self): + res = self.testapp.get('/', status=200) + self.assertTrue(b'Pyramid' in res.body) + + def test_notfound(self): + res = self.testapp.get('/badurl', status=404) + self.assertTrue(res.status_code == 404) diff --git a/docs/quick_tour/sqla_demo/MANIFEST.in b/docs/quick_tour/sqla_demo/MANIFEST.in index e079655f9..13a26726d 100644 --- a/docs/quick_tour/sqla_demo/MANIFEST.in +++ b/docs/quick_tour/sqla_demo/MANIFEST.in @@ -1,2 +1,5 @@ include *.txt *.ini *.cfg *.rst recursive-include sqla_demo *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2 +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] diff --git a/docs/quick_tour/sqla_demo/pytest.ini b/docs/quick_tour/sqla_demo/pytest.ini index b4b690a0f..e7fd17682 100644 --- a/docs/quick_tour/sqla_demo/pytest.ini +++ b/docs/quick_tour/sqla_demo/pytest.ini @@ -1,3 +1,6 @@ [pytest] -testpaths = sqla_demo -python_files = test*.py +addopts = --strict + +testpaths = + sqla_demo + tests diff --git a/docs/quick_tour/sqla_demo/tests/__init__.py b/docs/quick_tour/sqla_demo/tests/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/docs/quick_tour/sqla_demo/tests/__init__.py diff --git a/docs/quick_tour/sqla_demo/tests/test_it.py b/docs/quick_tour/sqla_demo/tests/test_it.py new file mode 100644 index 000000000..c79e05022 --- /dev/null +++ b/docs/quick_tour/sqla_demo/tests/test_it.py @@ -0,0 +1,66 @@ +import unittest + +from pyramid import testing + +import transaction + + +def dummy_request(dbsession): + return testing.DummyRequest(dbsession=dbsession) + + +class BaseTest(unittest.TestCase): + def setUp(self): + self.config = testing.setUp(settings={ + 'sqlalchemy.url': 'sqlite:///:memory:' + }) + self.config.include('sqla_demo.models') + settings = self.config.get_settings() + + from sqla_demo.models import ( + get_engine, + get_session_factory, + get_tm_session, + ) + + self.engine = get_engine(settings) + session_factory = get_session_factory(self.engine) + + self.session = get_tm_session(session_factory, transaction.manager) + + def init_database(self): + from sqla_demo.models.meta import Base + Base.metadata.create_all(self.engine) + + def tearDown(self): + from sqla_demo.models.meta import Base + + testing.tearDown() + transaction.abort() + Base.metadata.drop_all(self.engine) + + +class TestMyViewSuccessCondition(BaseTest): + + def setUp(self): + super(TestMyViewSuccessCondition, self).setUp() + self.init_database() + + from sqla_demo.models import MyModel + + model = MyModel(name='one', value=55) + self.session.add(model) + + def test_passing_view(self): + from sqla_demo.views.default import my_view + info = my_view(dummy_request(self.session)) + self.assertEqual(info['one'].name, 'one') + self.assertEqual(info['project'], 'sqla_demo') + + +class TestMyViewFailureCondition(BaseTest): + + def test_failing_view(self): + from sqla_demo.views.default import my_view + info = my_view(dummy_request(self.session)) + self.assertEqual(info.status_int, 500) |
