From 455203d2b3b0c898df6d3661cb1de577dfeda483 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 30 Oct 2018 13:40:53 +0100 Subject: Pytest fixes (#9) * pytest: fix collection warnings via __test__=False Fixes > "cannot collect test class %r because it has a __init__ constructor Ref: https://github.com/pytest-dev/pytest/issues/2007 * pytest: configure testpaths This is faster with test collection. * pytest: fix warning with doctests Fixes > /usr/lib/python3.7/site-packages/_pytest/python.py:764: > RemovedInPytest4Warning: usage of Generator.Function is deprecated, > please use pytest.Function instead * Minor fixes around s/py.test/pytest/ --- docs/DeveloperGuidelines.txt | 8 ++++---- docs/news.txt | 2 +- paste/fixture.py | 22 +++++++++++----------- setup.cfg | 3 +++ tests/test_auth/test_auth_digest.py | 6 +++--- tests/test_doctests.py | 26 +++++++++++++------------- tox.ini | 2 +- 7 files changed, 36 insertions(+), 33 deletions(-) diff --git a/docs/DeveloperGuidelines.txt b/docs/DeveloperGuidelines.txt index 69f39ee..efd602d 100644 --- a/docs/DeveloperGuidelines.txt +++ b/docs/DeveloperGuidelines.txt @@ -52,15 +52,15 @@ is going to bite your head off for committing something. the work here. Paste Script contains several wrappers for external projects (servers in particular). -* Tests are good. We use py.test_, because it is simple. I want to +* Tests are good. We use pytest_, because it is simple. I want to use doctests too, but the infrastructure isn't really there now -- but please feel free to use those too. ``unittest`` is kind of - annoying, and py.test is both more powerful and easier to write for. + annoying, and pytest is both more powerful and easier to write for. Tests should go in the ``tests/`` directory. ``paste.fixture`` contains some convenience functions for testing WSGI applications and middleware. Pay particular attention to ``TestApp``. - -.. _py.test: http://codespeak.net/py/current/doc/test.html + +.. _pytest: https://docs.pytest.org/en/latest/ * If you move something around that someone may be using, keep their imports working and introduce a warning, like:: diff --git a/docs/news.txt b/docs/news.txt index 653c864..999d520 100644 --- a/docs/news.txt +++ b/docs/news.txt @@ -978,7 +978,7 @@ In paste.httpserver ``wdg_validate`` and ``doctest_webapp`` - a ``testserver`` module suitable to test HTTP socket - connections via ``py.test`` + connections via ``pytest`` * Re-factored `paste.wsgilib `_ into several other modules: diff --git a/paste/fixture.py b/paste/fixture.py index 8cff72f..cedf9a8 100644 --- a/paste/fixture.py +++ b/paste/fixture.py @@ -90,13 +90,14 @@ class Dummy_smtplib(object): "SMTP connection not quit") self.__class__.existing = None + class AppError(Exception): pass + class TestApp(object): - # for py.test - disabled = True + __test__ = False # Ignore with pytest test collection. def __init__(self, app, namespace=None, relative_to=None, extra_environ=None, pre_request_hook=None, @@ -189,8 +190,7 @@ class TestApp(object): """ if extra_environ is None: extra_environ = {} - # Hide from py.test: - __tracebackhide__ = True + __tracebackhide__ = True # Hide from pytest: if params: if not isinstance(params, (six.binary_type, six.text_type)): params = urlencode(params, doseq=True) @@ -494,10 +494,10 @@ class CaptureStdout(object): def getvalue(self): return self.captured.getvalue() + class TestResponse(object): - # for py.test - disabled = True + __test__ = False # Ignore with pytest test collection. """ Instances of this class are return by `TestApp @@ -884,10 +884,10 @@ class TestResponse(object): url = 'file:' + fn.replace(os.sep, '/') webbrowser.open_new(url) + class TestRequest(object): - # for py.test - disabled = True + __test__ = False # Ignore with pytest test collection. """ Instances of this class are created by `TestApp @@ -1331,8 +1331,7 @@ class TestFileEnvironment(object): scripts will be run. """ - # for py.test - disabled = True + __test__ = False # Ignore with pytest test collection. def __init__(self, base_path, template_path=None, script_path=None, @@ -1722,9 +1721,10 @@ def _make_pattern(pat): assert 0, ( "Cannot make callable pattern object out of %r" % pat) + def setup_module(module=None): """ - This is used by py.test if it is in the module, so you can + This is used by pytest if it is in the module, so you can import this directly. Use like:: diff --git a/setup.cfg b/setup.cfg index 00ae880..7300c5b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -8,3 +8,6 @@ distribute = register sdist bdist_egg upload pudge publish [bdist_wheel] universal=1 + +[tool:pytest] +testpaths = tests diff --git a/tests/test_auth/test_auth_digest.py b/tests/test_auth/test_auth_digest.py index 1d44038..25fc3e5 100644 --- a/tests/test_auth/test_auth_digest.py +++ b/tests/test_auth/test_auth_digest.py @@ -56,10 +56,10 @@ def test_digest(): # The following code uses sockets to test the functionality, # to enable use: # -# $ TEST_SOCKET py.test -# +# $ TEST_SOCKET=1 pytest + -if os.environ.get("TEST_SOCKET",""): +if os.environ.get("TEST_SOCKET", ""): from six.moves.urllib.error import HTTPError from six.moves.urllib.request import build_opener, HTTPDigestAuthHandler from paste.debug.testserver import serve diff --git a/tests/test_doctests.py b/tests/test_doctests.py index efea589..b243ea3 100644 --- a/tests/test_doctests.py +++ b/tests/test_doctests.py @@ -1,8 +1,11 @@ -import six import doctest -from paste.util.import_string import simple_import import os +import pytest +import six + +from paste.util.import_string import simple_import + filenames = [ 'tests/template.txt', ] @@ -30,29 +33,26 @@ options = doctest.ELLIPSIS | doctest.REPORT_ONLY_FIRST_FAILURE if six.PY3: options |= doctest.IGNORE_EXCEPTION_DETAIL -def test_doctests(): - for filename in filenames: - filename = os.path.join( - os.path.dirname(os.path.dirname(__file__)), - filename) - yield do_doctest, filename -def do_doctest(filename): +@pytest.mark.parametrize('filename', filenames) +def test_doctests(filename): + filename = os.path.join( + os.path.dirname(os.path.dirname(__file__)), + filename) failure, total = doctest.testfile( filename, module_relative=False, optionflags=options) assert not failure, "Failure in %r" % filename -def test_doctest_mods(): - for module in modules: - yield do_doctest_mod, module -def do_doctest_mod(module): +@pytest.mark.parametrize('module', modules) +def test_doctest_mods(module): module = simple_import(module) failure, total = doctest.testmod( module, optionflags=options) assert not failure, "Failure in %r" % module + if __name__ == '__main__': import sys import doctest diff --git a/tox.ini b/tox.ini index 291442d..00ecf6e 100644 --- a/tox.ini +++ b/tox.ini @@ -11,4 +11,4 @@ deps = setenv = coverage: PYTEST_ADDOPTS=--cov --cov-report=term-missing commands = - py.test {posargs} + pytest {posargs} -- cgit v1.2.1