Skip to content

Commit 455203d

Browse files
blueyedcdent
authored andcommitted
Pytest fixes (#9)
* pytest: fix collection warnings via __test__=False Fixes > "cannot collect test class %r because it has a __init__ constructor Ref: pytest-dev/pytest#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/
1 parent 495ff97 commit 455203d

File tree

7 files changed

+36
-33
lines changed

7 files changed

+36
-33
lines changed

docs/DeveloperGuidelines.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ is going to bite your head off for committing something.
5252
the work here. Paste Script contains several wrappers for external
5353
projects (servers in particular).
5454

55-
* Tests are good. We use py.test_, because it is simple. I want to
55+
* Tests are good. We use pytest_, because it is simple. I want to
5656
use doctests too, but the infrastructure isn't really there now --
5757
but please feel free to use those too. ``unittest`` is kind of
58-
annoying, and py.test is both more powerful and easier to write for.
58+
annoying, and pytest is both more powerful and easier to write for.
5959
Tests should go in the ``tests/`` directory. ``paste.fixture``
6060
contains some convenience functions for testing WSGI applications
6161
and middleware. Pay particular attention to ``TestApp``.
62-
63-
.. _py.test: http://codespeak.net/py/current/doc/test.html
62+
63+
.. _pytest: https://docs.pytest.org/en/latest/
6464

6565
* If you move something around that someone may be using, keep their
6666
imports working and introduce a warning, like::

docs/news.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ In paste.httpserver
978978
``wdg_validate`` and ``doctest_webapp``
979979

980980
- a ``testserver`` module suitable to test HTTP socket
981-
connections via ``py.test``
981+
connections via ``pytest``
982982

983983
* Re-factored `paste.wsgilib <module-paste.wsgilib.html>`_ into
984984
several other modules:

paste/fixture.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ def reset(self):
9090
"SMTP connection not quit")
9191
self.__class__.existing = None
9292

93+
9394
class AppError(Exception):
9495
pass
9596

97+
9698
class TestApp(object):
9799

98-
# for py.test
99-
disabled = True
100+
__test__ = False # Ignore with pytest test collection.
100101

101102
def __init__(self, app, namespace=None, relative_to=None,
102103
extra_environ=None, pre_request_hook=None,
@@ -189,8 +190,7 @@ def get(self, url, params=None, headers=None, extra_environ=None,
189190
"""
190191
if extra_environ is None:
191192
extra_environ = {}
192-
# Hide from py.test:
193-
__tracebackhide__ = True
193+
__tracebackhide__ = True # Hide from pytest:
194194
if params:
195195
if not isinstance(params, (six.binary_type, six.text_type)):
196196
params = urlencode(params, doseq=True)
@@ -494,10 +494,10 @@ def writelines(self, lines):
494494
def getvalue(self):
495495
return self.captured.getvalue()
496496

497+
497498
class TestResponse(object):
498499

499-
# for py.test
500-
disabled = True
500+
__test__ = False # Ignore with pytest test collection.
501501

502502
"""
503503
Instances of this class are return by `TestApp
@@ -884,10 +884,10 @@ def showbrowser(self):
884884
url = 'file:' + fn.replace(os.sep, '/')
885885
webbrowser.open_new(url)
886886

887+
887888
class TestRequest(object):
888889

889-
# for py.test
890-
disabled = True
890+
__test__ = False # Ignore with pytest test collection.
891891

892892
"""
893893
Instances of this class are created by `TestApp
@@ -1331,8 +1331,7 @@ class TestFileEnvironment(object):
13311331
scripts will be run.
13321332
"""
13331333

1334-
# for py.test
1335-
disabled = True
1334+
__test__ = False # Ignore with pytest test collection.
13361335

13371336
def __init__(self, base_path, template_path=None,
13381337
script_path=None,
@@ -1722,9 +1721,10 @@ def _make_pattern(pat):
17221721
assert 0, (
17231722
"Cannot make callable pattern object out of %r" % pat)
17241723

1724+
17251725
def setup_module(module=None):
17261726
"""
1727-
This is used by py.test if it is in the module, so you can
1727+
This is used by pytest if it is in the module, so you can
17281728
import this directly.
17291729
17301730
Use like::

setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ distribute = register sdist bdist_egg upload pudge publish
88

99
[bdist_wheel]
1010
universal=1
11+
12+
[tool:pytest]
13+
testpaths = tests

tests/test_auth/test_auth_digest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ def test_digest():
5656
# The following code uses sockets to test the functionality,
5757
# to enable use:
5858
#
59-
# $ TEST_SOCKET py.test
60-
#
59+
# $ TEST_SOCKET=1 pytest
60+
6161

62-
if os.environ.get("TEST_SOCKET",""):
62+
if os.environ.get("TEST_SOCKET", ""):
6363
from six.moves.urllib.error import HTTPError
6464
from six.moves.urllib.request import build_opener, HTTPDigestAuthHandler
6565
from paste.debug.testserver import serve

tests/test_doctests.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import six
21
import doctest
3-
from paste.util.import_string import simple_import
42
import os
53

4+
import pytest
5+
import six
6+
7+
from paste.util.import_string import simple_import
8+
69
filenames = [
710
'tests/template.txt',
811
]
@@ -30,29 +33,26 @@
3033
if six.PY3:
3134
options |= doctest.IGNORE_EXCEPTION_DETAIL
3235

33-
def test_doctests():
34-
for filename in filenames:
35-
filename = os.path.join(
36-
os.path.dirname(os.path.dirname(__file__)),
37-
filename)
38-
yield do_doctest, filename
3936

40-
def do_doctest(filename):
37+
@pytest.mark.parametrize('filename', filenames)
38+
def test_doctests(filename):
39+
filename = os.path.join(
40+
os.path.dirname(os.path.dirname(__file__)),
41+
filename)
4142
failure, total = doctest.testfile(
4243
filename, module_relative=False,
4344
optionflags=options)
4445
assert not failure, "Failure in %r" % filename
4546

46-
def test_doctest_mods():
47-
for module in modules:
48-
yield do_doctest_mod, module
4947

50-
def do_doctest_mod(module):
48+
@pytest.mark.parametrize('module', modules)
49+
def test_doctest_mods(module):
5150
module = simple_import(module)
5251
failure, total = doctest.testmod(
5352
module, optionflags=options)
5453
assert not failure, "Failure in %r" % module
5554

55+
5656
if __name__ == '__main__':
5757
import sys
5858
import doctest

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ deps =
1111
setenv =
1212
coverage: PYTEST_ADDOPTS=--cov --cov-report=term-missing
1313
commands =
14-
py.test {posargs}
14+
pytest {posargs}

0 commit comments

Comments
 (0)