run black

This commit is contained in:
Ronny Pfannschmidt
2018-05-23 16:48:46 +02:00
parent 3e1590bcfc
commit 703e4b11ba
133 changed files with 13821 additions and 9370 deletions

View File

@@ -8,7 +8,8 @@ from pytest import approx
from operator import eq, ne
from decimal import Decimal
from fractions import Fraction
inf, nan = float('inf'), float('nan')
inf, nan = float("inf"), float("nan")
class MyDocTestRunner(doctest.DocTestRunner):
@@ -17,29 +18,47 @@ class MyDocTestRunner(doctest.DocTestRunner):
doctest.DocTestRunner.__init__(self)
def report_failure(self, out, test, example, got):
raise AssertionError("'{}' evaluates to '{}', not '{}'".format(
example.source.strip(), got.strip(), example.want.strip()))
raise AssertionError(
"'{}' evaluates to '{}', not '{}'".format(
example.source.strip(), got.strip(), example.want.strip()
)
)
class TestApprox(object):
def test_repr_string(self):
plus_minus = u'\u00b1' if sys.version_info[0] > 2 else u'+-'
tol1, tol2, infr = '1.0e-06', '2.0e-06', 'inf'
assert repr(approx(1.0)) == '1.0 {pm} {tol1}'.format(pm=plus_minus, tol1=tol1)
assert repr(approx([1.0, 2.0])) == 'approx([1.0 {pm} {tol1}, 2.0 {pm} {tol2}])'.format(
pm=plus_minus, tol1=tol1, tol2=tol2)
assert repr(approx((1.0, 2.0))) == 'approx((1.0 {pm} {tol1}, 2.0 {pm} {tol2}))'.format(
pm=plus_minus, tol1=tol1, tol2=tol2)
assert repr(approx(inf)) == 'inf'
assert repr(approx(1.0, rel=nan)) == '1.0 {pm} ???'.format(pm=plus_minus)
assert repr(approx(1.0, rel=inf)) == '1.0 {pm} {infr}'.format(pm=plus_minus, infr=infr)
assert repr(approx(1.0j, rel=inf)) == '1j'
plus_minus = u"\u00b1" if sys.version_info[0] > 2 else u"+-"
tol1, tol2, infr = "1.0e-06", "2.0e-06", "inf"
assert repr(approx(1.0)) == "1.0 {pm} {tol1}".format(pm=plus_minus, tol1=tol1)
assert (
repr(approx([1.0, 2.0]))
== "approx([1.0 {pm} {tol1}, 2.0 {pm} {tol2}])".format(
pm=plus_minus, tol1=tol1, tol2=tol2
)
)
assert (
repr(approx((1.0, 2.0)))
== "approx((1.0 {pm} {tol1}, 2.0 {pm} {tol2}))".format(
pm=plus_minus, tol1=tol1, tol2=tol2
)
)
assert repr(approx(inf)) == "inf"
assert repr(approx(1.0, rel=nan)) == "1.0 {pm} ???".format(pm=plus_minus)
assert (
repr(approx(1.0, rel=inf))
== "1.0 {pm} {infr}".format(pm=plus_minus, infr=infr)
)
assert repr(approx(1.0j, rel=inf)) == "1j"
# Dictionaries aren't ordered, so we need to check both orders.
assert repr(approx({'a': 1.0, 'b': 2.0})) in (
"approx({{'a': 1.0 {pm} {tol1}, 'b': 2.0 {pm} {tol2}}})".format(pm=plus_minus, tol1=tol1, tol2=tol2),
"approx({{'b': 2.0 {pm} {tol2}, 'a': 1.0 {pm} {tol1}}})".format(pm=plus_minus, tol1=tol1, tol2=tol2),
assert repr(approx({"a": 1.0, "b": 2.0})) in (
"approx({{'a': 1.0 {pm} {tol1}, 'b': 2.0 {pm} {tol2}}})".format(
pm=plus_minus, tol1=tol1, tol2=tol2
),
"approx({{'b': 2.0 {pm} {tol2}, 'a': 1.0 {pm} {tol1}}})".format(
pm=plus_minus, tol1=tol1, tol2=tol2
),
)
def test_operator_overloading(self):
@@ -56,25 +75,19 @@ class TestApprox(object):
(12345, 12345.0),
(0.0, -0.0),
(345678, 345678),
(Decimal('1.0001'), Decimal('1.0001')),
(Decimal("1.0001"), Decimal("1.0001")),
(Fraction(1, 3), Fraction(-1, -3)),
]
for a, x in examples:
assert a == approx(x)
def test_opposite_sign(self):
examples = [
(eq, 1e-100, -1e-100),
(ne, 1e100, -1e100),
]
examples = [(eq, 1e-100, -1e-100), (ne, 1e100, -1e100)]
for op, a, x in examples:
assert op(a, approx(x))
def test_zero_tolerance(self):
within_1e10 = [
(1.1e-100, 1e-100),
(-1.1e-100, -1e-100),
]
within_1e10 = [(1.1e-100, 1e-100), (-1.1e-100, -1e-100)]
for a, x in within_1e10:
assert x == approx(x, rel=0.0, abs=0.0)
assert a != approx(x, rel=0.0, abs=0.0)
@@ -98,12 +111,7 @@ class TestApprox(object):
def test_inf_tolerance(self):
# Everything should be equal if the tolerance is infinite.
large_diffs = [
(1, 1000),
(1e-50, 1e50),
(-1.0, -1e300),
(0.0, 10),
]
large_diffs = [(1, 1000), (1e-50, 1e50), (-1.0, -1e300), (0.0, 10)]
for a, x in large_diffs:
assert a != approx(x, rel=0.0, abs=0.0)
assert a == approx(x, rel=inf, abs=0.0)
@@ -113,20 +121,13 @@ class TestApprox(object):
def test_inf_tolerance_expecting_zero(self):
# If the relative tolerance is zero but the expected value is infinite,
# the actual tolerance is a NaN, which should be an error.
illegal_kwargs = [
dict(rel=inf, abs=0.0),
dict(rel=inf, abs=inf),
]
illegal_kwargs = [dict(rel=inf, abs=0.0), dict(rel=inf, abs=inf)]
for kwargs in illegal_kwargs:
with pytest.raises(ValueError):
1 == approx(0, **kwargs)
def test_nan_tolerance(self):
illegal_kwargs = [
dict(rel=nan),
dict(abs=nan),
dict(rel=nan, abs=nan),
]
illegal_kwargs = [dict(rel=nan), dict(abs=nan), dict(rel=nan, abs=nan)]
for kwargs in illegal_kwargs:
with pytest.raises(ValueError):
1.1 == approx(1, **kwargs)
@@ -148,8 +149,8 @@ class TestApprox(object):
(eq, 1e0 + 1e-6, 1e0),
(ne, 1e0 + 2e-6, 1e0),
# Absolute tolerance used.
(eq, 1e-100, + 1e-106),
(eq, 1e-100, + 2e-106),
(eq, 1e-100, +1e-106),
(eq, 1e-100, +2e-106),
(eq, 1e-100, 0),
]
for op, a, x in examples:
@@ -172,21 +173,13 @@ class TestApprox(object):
assert 1e-8 + 1e-16 != approx(1e-8, rel=5e-9, abs=5e-17)
def test_relative_tolerance(self):
within_1e8_rel = [
(1e8 + 1e0, 1e8),
(1e0 + 1e-8, 1e0),
(1e-8 + 1e-16, 1e-8),
]
within_1e8_rel = [(1e8 + 1e0, 1e8), (1e0 + 1e-8, 1e0), (1e-8 + 1e-16, 1e-8)]
for a, x in within_1e8_rel:
assert a == approx(x, rel=5e-8, abs=0.0)
assert a != approx(x, rel=5e-9, abs=0.0)
def test_absolute_tolerance(self):
within_1e8_abs = [
(1e8 + 9e-9, 1e8),
(1e0 + 9e-9, 1e0),
(1e-8 + 9e-9, 1e-8),
]
within_1e8_abs = [(1e8 + 9e-9, 1e8), (1e0 + 9e-9, 1e0), (1e-8 + 9e-9, 1e-8)]
for a, x in within_1e8_abs:
assert a == approx(x, rel=0, abs=5e-8)
assert a != approx(x, rel=0, abs=5e-9)
@@ -233,10 +226,7 @@ class TestApprox(object):
assert op(a, approx(x, nan_ok=True))
def test_int(self):
within_1e6 = [
(1000001, 1000000),
(-1000001, -1000000),
]
within_1e6 = [(1000001, 1000000), (-1000001, -1000000)]
for a, x in within_1e6:
assert a == approx(x, rel=5e-6, abs=0)
assert a != approx(x, rel=5e-7, abs=0)
@@ -245,15 +235,15 @@ class TestApprox(object):
def test_decimal(self):
within_1e6 = [
(Decimal('1.000001'), Decimal('1.0')),
(Decimal('-1.000001'), Decimal('-1.0')),
(Decimal("1.000001"), Decimal("1.0")),
(Decimal("-1.000001"), Decimal("-1.0")),
]
for a, x in within_1e6:
assert a == approx(x)
assert a == approx(x, rel=Decimal('5e-6'), abs=0)
assert a != approx(x, rel=Decimal('5e-7'), abs=0)
assert approx(x, rel=Decimal('5e-6'), abs=0) == a
assert approx(x, rel=Decimal('5e-7'), abs=0) != a
assert a == approx(x, rel=Decimal("5e-6"), abs=0)
assert a != approx(x, rel=Decimal("5e-7"), abs=0)
assert approx(x, rel=Decimal("5e-6"), abs=0) == a
assert approx(x, rel=Decimal("5e-7"), abs=0) != a
def test_fraction(self):
within_1e6 = [
@@ -308,10 +298,10 @@ class TestApprox(object):
assert (1, 2) != approx((1, 2, 3))
def test_dict(self):
actual = {'a': 1 + 1e-7, 'b': 2 + 1e-8}
actual = {"a": 1 + 1e-7, "b": 2 + 1e-8}
# Dictionaries became ordered in python3.6, so switch up the order here
# to make sure it doesn't matter.
expected = {'b': 2, 'a': 1}
expected = {"b": 2, "a": 1}
# Return false if any element is outside the tolerance.
assert actual == approx(expected, rel=5e-7, abs=0)
@@ -320,12 +310,12 @@ class TestApprox(object):
assert approx(expected, rel=5e-8, abs=0) != actual
def test_dict_wrong_len(self):
assert {'a': 1, 'b': 2} != approx({'a': 1})
assert {'a': 1, 'b': 2} != approx({'a': 1, 'c': 2})
assert {'a': 1, 'b': 2} != approx({'a': 1, 'b': 2, 'c': 3})
assert {"a": 1, "b": 2} != approx({"a": 1})
assert {"a": 1, "b": 2} != approx({"a": 1, "c": 2})
assert {"a": 1, "b": 2} != approx({"a": 1, "b": 2, "c": 3})
def test_numpy_array(self):
np = pytest.importorskip('numpy')
np = pytest.importorskip("numpy")
actual = np.array([1 + 1e-7, 2 + 1e-8])
expected = np.array([1, 2])
@@ -343,7 +333,7 @@ class TestApprox(object):
assert actual != approx(list(expected), rel=5e-8, abs=0)
def test_numpy_array_wrong_shape(self):
np = pytest.importorskip('numpy')
np = pytest.importorskip("numpy")
a12 = np.array([[1, 2]])
a21 = np.array([[1], [2]])
@@ -354,10 +344,7 @@ class TestApprox(object):
def test_doctests(self):
parser = doctest.DocTestParser()
test = parser.get_doctest(
approx.__doc__,
{'approx': approx},
approx.__name__,
None, None,
approx.__doc__, {"approx": approx}, approx.__name__, None, None
)
runner = MyDocTestRunner()
runner.run(test)
@@ -367,24 +354,28 @@ class TestApprox(object):
Comparing approx instances inside lists should not produce an error in the detailed diff.
Integration test for issue #2111.
"""
testdir.makepyfile("""
testdir.makepyfile(
"""
import pytest
def test_foo():
assert [3] == [pytest.approx(4)]
""")
expected = '4.0e-06'
"""
)
expected = "4.0e-06"
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*At index 0 diff: 3 != 4 * {}'.format(expected),
'=* 1 failed in *=',
])
result.stdout.fnmatch_lines(
["*At index 0 diff: 3 != 4 * {}".format(expected), "=* 1 failed in *="]
)
@pytest.mark.parametrize('op', [
pytest.param(operator.le, id='<='),
pytest.param(operator.lt, id='<'),
pytest.param(operator.ge, id='>='),
pytest.param(operator.gt, id='>'),
])
@pytest.mark.parametrize(
"op",
[
pytest.param(operator.le, id="<="),
pytest.param(operator.lt, id="<"),
pytest.param(operator.ge, id=">="),
pytest.param(operator.gt, id=">"),
],
)
def test_comparison_operator_type_error(self, op):
"""
pytest.approx should raise TypeError for operators other than == and != (#2003).
@@ -393,7 +384,7 @@ class TestApprox(object):
op(1, approx(1, rel=1e-6, abs=1e-12))
def test_numpy_array_with_scalar(self):
np = pytest.importorskip('numpy')
np = pytest.importorskip("numpy")
actual = np.array([1 + 1e-7, 1 - 1e-8])
expected = 1.0
@@ -404,7 +395,7 @@ class TestApprox(object):
assert approx(expected, rel=5e-8, abs=0) != actual
def test_numpy_scalar_with_array(self):
np = pytest.importorskip('numpy')
np = pytest.importorskip("numpy")
actual = 1.0
expected = np.array([1 + 1e-7, 1 - 1e-8])

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -4,8 +4,10 @@ from _pytest import runner
class TestOEJSKITSpecials(object):
def test_funcarg_non_pycollectobj(self, testdir): # rough jstests usage
testdir.makeconftest("""
testdir.makeconftest(
"""
import pytest
def pytest_pycollect_makeitem(collector, name, obj):
if name == "MyClass":
@@ -13,25 +15,29 @@ class TestOEJSKITSpecials(object):
class MyCollector(pytest.Collector):
def reportinfo(self):
return self.fspath, 3, "xyz"
""")
modcol = testdir.getmodulecol("""
"""
)
modcol = testdir.getmodulecol(
"""
import pytest
@pytest.fixture
def arg1(request):
return 42
class MyClass(object):
pass
""")
"""
)
# this hook finds funcarg factories
rep = runner.collect_one_node(collector=modcol)
clscol = rep.result[0]
clscol.obj = lambda arg1: None
clscol.funcargs = {}
pytest._fillfuncargs(clscol)
assert clscol.funcargs['arg1'] == 42
assert clscol.funcargs["arg1"] == 42
def test_autouse_fixture(self, testdir): # rough jstests usage
testdir.makeconftest("""
testdir.makeconftest(
"""
import pytest
def pytest_pycollect_makeitem(collector, name, obj):
if name == "MyClass":
@@ -39,8 +45,10 @@ class TestOEJSKITSpecials(object):
class MyCollector(pytest.Collector):
def reportinfo(self):
return self.fspath, 3, "xyz"
""")
modcol = testdir.getmodulecol("""
"""
)
modcol = testdir.getmodulecol(
"""
import pytest
@pytest.fixture(autouse=True)
def hello():
@@ -50,7 +58,8 @@ class TestOEJSKITSpecials(object):
return 42
class MyClass(object):
pass
""")
"""
)
# this hook finds funcarg factories
rep = runner.collect_one_node(modcol)
clscol = rep.result[0]
@@ -61,6 +70,7 @@ class TestOEJSKITSpecials(object):
def test_wrapped_getfslineno():
def func():
pass
@@ -72,12 +82,14 @@ def test_wrapped_getfslineno():
@wrap
def wrapped_func(x, y, z):
pass
fs, lineno = python.getfslineno(wrapped_func)
fs2, lineno2 = python.getfslineno(wrap)
assert lineno > lineno2, "getfslineno does not unwrap correctly"
class TestMockDecoration(object):
def test_wrapped_getfuncargnames(self):
from _pytest.compat import getfuncargnames
@@ -100,8 +112,10 @@ class TestMockDecoration(object):
from _pytest.compat import getfuncargnames
def wrap(f):
def func():
pass
func.__wrapped__ = f
func.patchings = ["qwe"]
return func
@@ -115,7 +129,8 @@ class TestMockDecoration(object):
def test_unittest_mock(self, testdir):
pytest.importorskip("unittest.mock")
testdir.makepyfile("""
testdir.makepyfile(
"""
import unittest.mock
class T(unittest.TestCase):
@unittest.mock.patch("os.path.abspath")
@@ -123,13 +138,15 @@ class TestMockDecoration(object):
import os
os.path.abspath("hello")
abspath.assert_any_call("hello")
""")
"""
)
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
def test_unittest_mock_and_fixture(self, testdir):
pytest.importorskip("unittest.mock")
testdir.makepyfile("""
testdir.makepyfile(
"""
import os.path
import unittest.mock
import pytest
@@ -143,14 +160,16 @@ class TestMockDecoration(object):
def test_hello(inject_me):
import os
os.path.abspath("hello")
""")
"""
)
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
def test_unittest_mock_and_pypi_mock(self, testdir):
pytest.importorskip("unittest.mock")
pytest.importorskip("mock", "1.0.1")
testdir.makepyfile("""
testdir.makepyfile(
"""
import mock
import unittest.mock
class TestBoth(object):
@@ -165,13 +184,15 @@ class TestMockDecoration(object):
import os
os.path.abspath("hello")
abspath.assert_any_call("hello")
""")
"""
)
reprec = testdir.inline_run()
reprec.assertoutcome(passed=2)
def test_mock(self, testdir):
pytest.importorskip("mock", "1.0.1")
testdir.makepyfile("""
testdir.makepyfile(
"""
import os
import unittest
import mock
@@ -191,17 +212,20 @@ class TestMockDecoration(object):
os.path.normpath(os.path.abspath("hello"))
normpath.assert_any_call("this")
assert os.path.basename("123") == "mock_basename"
""")
"""
)
reprec = testdir.inline_run()
reprec.assertoutcome(passed=2)
calls = reprec.getcalls("pytest_runtest_logreport")
funcnames = [call.report.location[2] for call in calls
if call.report.when == "call"]
funcnames = [
call.report.location[2] for call in calls if call.report.when == "call"
]
assert funcnames == ["T.test_hello", "test_someting"]
def test_mock_sorting(self, testdir):
pytest.importorskip("mock", "1.0.1")
testdir.makepyfile("""
testdir.makepyfile(
"""
import os
import mock
@@ -214,7 +238,8 @@ class TestMockDecoration(object):
@mock.patch("os.path.abspath")
def test_three(abspath):
pass
""")
"""
)
reprec = testdir.inline_run()
calls = reprec.getreports("pytest_runtest_logreport")
calls = [x for x in calls if x.when == "call"]
@@ -223,7 +248,8 @@ class TestMockDecoration(object):
def test_mock_double_patch_issue473(self, testdir):
pytest.importorskip("mock", "1.0.1")
testdir.makepyfile("""
testdir.makepyfile(
"""
from mock import patch
from pytest import mark
@@ -233,20 +259,25 @@ class TestMockDecoration(object):
class TestSimple(object):
def test_simple_thing(self, mock_path, mock_getcwd):
pass
""")
"""
)
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
class TestReRunTests(object):
def test_rerun(self, testdir):
testdir.makeconftest("""
testdir.makeconftest(
"""
from _pytest.runner import runtestprotocol
def pytest_runtest_protocol(item, nextitem):
runtestprotocol(item, log=False, nextitem=nextitem)
runtestprotocol(item, log=True, nextitem=nextitem)
""")
testdir.makepyfile("""
"""
)
testdir.makepyfile(
"""
import pytest
count = 0
req = None
@@ -259,36 +290,46 @@ class TestReRunTests(object):
count += 1
def test_fix(fix):
pass
""")
"""
)
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("""
result.stdout.fnmatch_lines(
"""
*fix count 0*
*fix count 1*
""")
result.stdout.fnmatch_lines("""
"""
)
result.stdout.fnmatch_lines(
"""
*2 passed*
""")
"""
)
def test_pytestconfig_is_session_scoped():
from _pytest.fixtures import pytestconfig
assert pytestconfig._pytestfixturefunction.scope == "session"
class TestNoselikeTestAttribute(object):
def test_module_with_global_test(self, testdir):
testdir.makepyfile("""
testdir.makepyfile(
"""
__test__ = False
def test_hello():
pass
""")
"""
)
reprec = testdir.inline_run()
assert not reprec.getfailedcollections()
calls = reprec.getreports("pytest_runtest_logreport")
assert not calls
def test_class_and_method(self, testdir):
testdir.makepyfile("""
testdir.makepyfile(
"""
__test__ = True
def test_func():
pass
@@ -298,14 +339,16 @@ class TestNoselikeTestAttribute(object):
__test__ = False
def test_method(self):
pass
""")
"""
)
reprec = testdir.inline_run()
assert not reprec.getfailedcollections()
calls = reprec.getreports("pytest_runtest_logreport")
assert not calls
def test_unittest_class(self, testdir):
testdir.makepyfile("""
testdir.makepyfile(
"""
import unittest
class TC(unittest.TestCase):
def test_1(self):
@@ -314,7 +357,8 @@ class TestNoselikeTestAttribute(object):
__test__ = False
def test_2(self):
pass
""")
"""
)
reprec = testdir.inline_run()
assert not reprec.getfailedcollections()
call = reprec.getcalls("pytest_collection_modifyitems")[0]
@@ -328,7 +372,8 @@ class TestNoselikeTestAttribute(object):
RPC wrapper), we shouldn't assume this meant "__test__ = True".
"""
# https://github.com/pytest-dev/pytest/issues/1204
testdir.makepyfile("""
testdir.makepyfile(
"""
class MetaModel(type):
def __getattr__(cls, key):
@@ -344,7 +389,8 @@ class TestNoselikeTestAttribute(object):
def test_blah(self):
pass
""")
"""
)
reprec = testdir.inline_run()
assert not reprec.getfailedcollections()
call = reprec.getcalls("pytest_collection_modifyitems")[0]
@@ -355,7 +401,8 @@ class TestNoselikeTestAttribute(object):
class TestParameterize(object):
def test_idfn_marker(self, testdir):
testdir.makepyfile("""
testdir.makepyfile(
"""
import pytest
def idfn(param):
@@ -369,15 +416,14 @@ class TestParameterize(object):
@pytest.mark.parametrize('a,b', [(0, 2), (1, 2)], ids=idfn)
def test_params(a, b):
pass
""")
res = testdir.runpytest('--collect-only')
res.stdout.fnmatch_lines([
"*spam-2*",
"*ham-2*",
])
"""
)
res = testdir.runpytest("--collect-only")
res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"])
def test_idfn_fixture(self, testdir):
testdir.makepyfile("""
testdir.makepyfile(
"""
import pytest
def idfn(param):
@@ -398,9 +444,7 @@ class TestParameterize(object):
def test_params(a, b):
pass
""")
res = testdir.runpytest('--collect-only')
res.stdout.fnmatch_lines([
"*spam-2*",
"*ham-2*",
])
"""
)
res = testdir.runpytest("--collect-only")
res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"])

File diff suppressed because it is too large Load Diff

View File

@@ -4,6 +4,7 @@ import sys
class TestRaises(object):
def test_raises(self):
source = "int('qwe')"
excinfo = pytest.raises(ValueError, source)
@@ -18,19 +19,23 @@ class TestRaises(object):
pytest.raises(SyntaxError, "qwe qwe qwe")
def test_raises_function(self):
pytest.raises(ValueError, int, 'hello')
pytest.raises(ValueError, int, "hello")
def test_raises_callable_no_exception(self):
class A(object):
def __call__(self):
pass
try:
pytest.raises(ValueError, A())
except pytest.raises.Exception:
pass
def test_raises_as_contextmanager(self, testdir):
testdir.makepyfile("""
testdir.makepyfile(
"""
from __future__ import with_statement
import py, pytest
import _pytest._code
@@ -52,28 +57,27 @@ class TestRaises(object):
with pytest.raises(ZeroDivisionError):
with pytest.raises(ValueError):
1/0
""")
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*3 passed*',
])
result.stdout.fnmatch_lines(["*3 passed*"])
def test_noclass(self):
with pytest.raises(TypeError):
pytest.raises('wrong', lambda: None)
pytest.raises("wrong", lambda: None)
def test_invalid_arguments_to_raises(self):
with pytest.raises(TypeError, match='unknown'):
with pytest.raises(TypeError, unknown='bogus'):
with pytest.raises(TypeError, match="unknown"):
with pytest.raises(TypeError, unknown="bogus"):
raise ValueError()
def test_tuple(self):
with pytest.raises((KeyError, ValueError)):
raise KeyError('oops')
raise KeyError("oops")
def test_no_raise_message(self):
try:
pytest.raises(ValueError, int, '0')
pytest.raises(ValueError, int, "0")
except pytest.raises.Exception as e:
assert e.msg == "DID NOT RAISE {}".format(repr(ValueError))
else:
@@ -97,7 +101,7 @@ class TestRaises(object):
else:
assert False, "Expected pytest.raises.Exception"
@pytest.mark.parametrize('method', ['function', 'with'])
@pytest.mark.parametrize("method", ["function", "with"])
def test_raises_cyclic_reference(self, method):
"""
Ensure pytest.raises does not leave a reference cycle (#1965).
@@ -105,11 +109,12 @@ class TestRaises(object):
import gc
class T(object):
def __call__(self):
raise ValueError
t = T()
if method == 'function':
if method == "function":
pytest.raises(ValueError, t)
else:
with pytest.raises(ValueError):
@@ -127,17 +132,19 @@ class TestRaises(object):
def test_raises_match(self):
msg = r"with base \d+"
with pytest.raises(ValueError, match=msg):
int('asdf')
int("asdf")
msg = "with base 10"
with pytest.raises(ValueError, match=msg):
int('asdf')
int("asdf")
msg = "with base 16"
expr = r"Pattern '{}' not found in 'invalid literal for int\(\) with base 10: 'asdf''".format(msg)
expr = r"Pattern '{}' not found in 'invalid literal for int\(\) with base 10: 'asdf''".format(
msg
)
with pytest.raises(AssertionError, match=expr):
with pytest.raises(ValueError, match=msg):
int('asdf', base=10)
int("asdf", base=10)
def test_raises_match_wrong_type(self):
"""Raising an exception with the wrong type and match= given.
@@ -146,15 +153,16 @@ class TestRaises(object):
really relevant if we got a different exception.
"""
with pytest.raises(ValueError):
with pytest.raises(IndexError, match='nomatch'):
int('asdf')
with pytest.raises(IndexError, match="nomatch"):
int("asdf")
def test_raises_exception_looks_iterable(self):
from six import add_metaclass
class Meta(type(object)):
def __getitem__(self, item):
return 1/0
return 1 / 0
def __len__(self):
return 1
@@ -163,5 +171,7 @@ class TestRaises(object):
class ClassLooksIterableException(Exception):
pass
with pytest.raises(Failed, match="DID NOT RAISE <class 'raises.ClassLooksIterableException'>"):
with pytest.raises(
Failed, match="DID NOT RAISE <class 'raises.ClassLooksIterableException'>"
):
pytest.raises(ClassLooksIterableException, lambda: None)

View File

@@ -1,14 +1,14 @@
import pytest
@pytest.fixture(params=['--setup-only', '--setup-plan', '--setup-show'],
scope='module')
@pytest.fixture(params=["--setup-only", "--setup-plan", "--setup-show"], scope="module")
def mode(request):
return request.param
def test_show_only_active_fixtures(testdir, mode):
p = testdir.makepyfile('''
p = testdir.makepyfile(
'''
import pytest
@pytest.fixture
def _arg0():
@@ -18,21 +18,21 @@ def test_show_only_active_fixtures(testdir, mode):
"""arg1 docstring"""
def test_arg1(arg1):
pass
''')
'''
)
result = testdir.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*SETUP F arg1*',
'*test_arg1 (fixtures used: arg1)*',
'*TEARDOWN F arg1*',
])
result.stdout.fnmatch_lines(
["*SETUP F arg1*", "*test_arg1 (fixtures used: arg1)*", "*TEARDOWN F arg1*"]
)
assert "_arg0" not in result.stdout.str()
def test_show_different_scopes(testdir, mode):
p = testdir.makepyfile('''
p = testdir.makepyfile(
'''
import pytest
@pytest.fixture
def arg_function():
@@ -42,50 +42,60 @@ def test_show_different_scopes(testdir, mode):
"""session scoped fixture"""
def test_arg1(arg_session, arg_function):
pass
''')
'''
)
result = testdir.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'SETUP S arg_session*',
'*SETUP F arg_function*',
'*test_arg1 (fixtures used: arg_function, arg_session)*',
'*TEARDOWN F arg_function*',
'TEARDOWN S arg_session*',
])
result.stdout.fnmatch_lines(
[
"SETUP S arg_session*",
"*SETUP F arg_function*",
"*test_arg1 (fixtures used: arg_function, arg_session)*",
"*TEARDOWN F arg_function*",
"TEARDOWN S arg_session*",
]
)
def test_show_nested_fixtures(testdir, mode):
testdir.makeconftest('''
testdir.makeconftest(
'''
import pytest
@pytest.fixture(scope='session')
def arg_same():
"""session scoped fixture"""
''')
p = testdir.makepyfile('''
'''
)
p = testdir.makepyfile(
'''
import pytest
@pytest.fixture(scope='function')
def arg_same(arg_same):
"""function scoped fixture"""
def test_arg1(arg_same):
pass
''')
'''
)
result = testdir.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'SETUP S arg_same*',
'*SETUP F arg_same (fixtures used: arg_same)*',
'*test_arg1 (fixtures used: arg_same)*',
'*TEARDOWN F arg_same*',
'TEARDOWN S arg_same*',
])
result.stdout.fnmatch_lines(
[
"SETUP S arg_same*",
"*SETUP F arg_same (fixtures used: arg_same)*",
"*test_arg1 (fixtures used: arg_same)*",
"*TEARDOWN F arg_same*",
"TEARDOWN S arg_same*",
]
)
def test_show_fixtures_with_autouse(testdir, mode):
p = testdir.makepyfile('''
p = testdir.makepyfile(
'''
import pytest
@pytest.fixture
def arg_function():
@@ -95,92 +105,104 @@ def test_show_fixtures_with_autouse(testdir, mode):
"""session scoped fixture"""
def test_arg1(arg_function):
pass
''')
'''
)
result = testdir.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'SETUP S arg_session*',
'*SETUP F arg_function*',
'*test_arg1 (fixtures used: arg_function, arg_session)*',
])
result.stdout.fnmatch_lines(
[
"SETUP S arg_session*",
"*SETUP F arg_function*",
"*test_arg1 (fixtures used: arg_function, arg_session)*",
]
)
def test_show_fixtures_with_parameters(testdir, mode):
testdir.makeconftest('''
testdir.makeconftest(
'''
import pytest
@pytest.fixture(scope='session', params=['foo', 'bar'])
def arg_same():
"""session scoped fixture"""
''')
p = testdir.makepyfile('''
'''
)
p = testdir.makepyfile(
'''
import pytest
@pytest.fixture(scope='function')
def arg_other(arg_same):
"""function scoped fixture"""
def test_arg1(arg_other):
pass
''')
'''
)
result = testdir.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'SETUP S arg_same?foo?',
'TEARDOWN S arg_same?foo?',
'SETUP S arg_same?bar?',
'TEARDOWN S arg_same?bar?',
])
result.stdout.fnmatch_lines(
[
"SETUP S arg_same?foo?",
"TEARDOWN S arg_same?foo?",
"SETUP S arg_same?bar?",
"TEARDOWN S arg_same?bar?",
]
)
def test_show_fixtures_with_parameter_ids(testdir, mode):
testdir.makeconftest('''
testdir.makeconftest(
'''
import pytest
@pytest.fixture(
scope='session', params=['foo', 'bar'], ids=['spam', 'ham'])
def arg_same():
"""session scoped fixture"""
''')
p = testdir.makepyfile('''
'''
)
p = testdir.makepyfile(
'''
import pytest
@pytest.fixture(scope='function')
def arg_other(arg_same):
"""function scoped fixture"""
def test_arg1(arg_other):
pass
''')
'''
)
result = testdir.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'SETUP S arg_same?spam?',
'SETUP S arg_same?ham?',
])
result.stdout.fnmatch_lines(
["SETUP S arg_same?spam?", "SETUP S arg_same?ham?"]
)
def test_show_fixtures_with_parameter_ids_function(testdir, mode):
p = testdir.makepyfile('''
p = testdir.makepyfile(
"""
import pytest
@pytest.fixture(params=['foo', 'bar'], ids=lambda p: p.upper())
def foobar():
pass
def test_foobar(foobar):
pass
''')
"""
)
result = testdir.runpytest(mode, p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*SETUP F foobar?FOO?',
'*SETUP F foobar?BAR?',
])
result.stdout.fnmatch_lines(["*SETUP F foobar?FOO?", "*SETUP F foobar?BAR?"])
def test_dynamic_fixture_request(testdir):
p = testdir.makepyfile('''
p = testdir.makepyfile(
"""
import pytest
@pytest.fixture()
def dynamically_requested_fixture():
@@ -190,19 +212,23 @@ def test_dynamic_fixture_request(testdir):
request.getfixturevalue('dynamically_requested_fixture')
def test_dyn(dependent_fixture):
pass
''')
"""
)
result = testdir.runpytest('--setup-only', p)
result = testdir.runpytest("--setup-only", p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*SETUP F dynamically_requested_fixture',
'*TEARDOWN F dynamically_requested_fixture'
])
result.stdout.fnmatch_lines(
[
"*SETUP F dynamically_requested_fixture",
"*TEARDOWN F dynamically_requested_fixture",
]
)
def test_capturing(testdir):
p = testdir.makepyfile('''
p = testdir.makepyfile(
"""
import pytest, sys
@pytest.fixture()
def one():
@@ -213,31 +239,31 @@ def test_capturing(testdir):
assert 0
def test_capturing(two):
pass
''')
"""
)
result = testdir.runpytest('--setup-only', p)
result.stdout.fnmatch_lines([
'this should be captured',
'this should also be captured'
])
result = testdir.runpytest("--setup-only", p)
result.stdout.fnmatch_lines(
["this should be captured", "this should also be captured"]
)
def test_show_fixtures_and_execute_test(testdir):
""" Verifies that setups are shown and tests are executed. """
p = testdir.makepyfile('''
p = testdir.makepyfile(
"""
import pytest
@pytest.fixture
def arg():
assert True
def test_arg(arg):
assert False
''')
"""
)
result = testdir.runpytest("--setup-show", p)
assert result.ret == 1
result.stdout.fnmatch_lines([
'*SETUP F arg*',
'*test_arg (fixtures used: arg)F*',
'*TEARDOWN F arg*',
])
result.stdout.fnmatch_lines(
["*SETUP F arg*", "*test_arg (fixtures used: arg)F*", "*TEARDOWN F arg*"]
)

View File

@@ -1,19 +1,19 @@
def test_show_fixtures_and_test(testdir):
""" Verifies that fixtures are not executed. """
p = testdir.makepyfile('''
p = testdir.makepyfile(
"""
import pytest
@pytest.fixture
def arg():
assert False
def test_arg(arg):
assert False
''')
"""
)
result = testdir.runpytest("--setup-plan", p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*SETUP F arg*',
'*test_arg (fixtures used: arg)',
'*TEARDOWN F arg*',
])
result.stdout.fnmatch_lines(
["*SETUP F arg*", "*test_arg (fixtures used: arg)", "*TEARDOWN F arg*"]
)

View File

@@ -2,13 +2,14 @@
def test_no_items_should_not_show_output(testdir):
result = testdir.runpytest('--fixtures-per-test')
assert 'fixtures used by' not in result.stdout.str()
result = testdir.runpytest("--fixtures-per-test")
assert "fixtures used by" not in result.stdout.str()
assert result.ret == 0
def test_fixtures_in_module(testdir):
p = testdir.makepyfile('''
p = testdir.makepyfile(
'''
import pytest
@pytest.fixture
def _arg0():
@@ -18,22 +19,26 @@ def test_fixtures_in_module(testdir):
"""arg1 docstring"""
def test_arg1(arg1):
pass
''')
'''
)
result = testdir.runpytest("--fixtures-per-test", p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*fixtures used by test_arg1*',
'*(test_fixtures_in_module.py:9)*',
'arg1',
' arg1 docstring',
])
result.stdout.fnmatch_lines(
[
"*fixtures used by test_arg1*",
"*(test_fixtures_in_module.py:9)*",
"arg1",
" arg1 docstring",
]
)
assert "_arg0" not in result.stdout.str()
def test_fixtures_in_conftest(testdir):
testdir.makeconftest('''
testdir.makeconftest(
'''
import pytest
@pytest.fixture
def arg1():
@@ -46,35 +51,41 @@ def test_fixtures_in_conftest(testdir):
"""arg3
docstring
"""
''')
p = testdir.makepyfile('''
'''
)
p = testdir.makepyfile(
"""
def test_arg2(arg2):
pass
def test_arg3(arg3):
pass
''')
"""
)
result = testdir.runpytest("--fixtures-per-test", p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*fixtures used by test_arg2*',
'*(test_fixtures_in_conftest.py:2)*',
'arg2',
' arg2 docstring',
'*fixtures used by test_arg3*',
'*(test_fixtures_in_conftest.py:4)*',
'arg1',
' arg1 docstring',
'arg2',
' arg2 docstring',
'arg3',
' arg3',
' docstring',
])
result.stdout.fnmatch_lines(
[
"*fixtures used by test_arg2*",
"*(test_fixtures_in_conftest.py:2)*",
"arg2",
" arg2 docstring",
"*fixtures used by test_arg3*",
"*(test_fixtures_in_conftest.py:4)*",
"arg1",
" arg1 docstring",
"arg2",
" arg2 docstring",
"arg3",
" arg3",
" docstring",
]
)
def test_should_show_fixtures_used_by_test(testdir):
testdir.makeconftest('''
testdir.makeconftest(
'''
import pytest
@pytest.fixture
def arg1():
@@ -82,30 +93,36 @@ def test_should_show_fixtures_used_by_test(testdir):
@pytest.fixture
def arg2():
"""arg2 from conftest"""
''')
p = testdir.makepyfile('''
'''
)
p = testdir.makepyfile(
'''
import pytest
@pytest.fixture
def arg1():
"""arg1 from testmodule"""
def test_args(arg1, arg2):
pass
''')
'''
)
result = testdir.runpytest("--fixtures-per-test", p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*fixtures used by test_args*',
'*(test_should_show_fixtures_used_by_test.py:6)*',
'arg1',
' arg1 from testmodule',
'arg2',
' arg2 from conftest',
])
result.stdout.fnmatch_lines(
[
"*fixtures used by test_args*",
"*(test_should_show_fixtures_used_by_test.py:6)*",
"arg1",
" arg1 from testmodule",
"arg2",
" arg2 from conftest",
]
)
def test_verbose_include_private_fixtures_and_loc(testdir):
testdir.makeconftest('''
testdir.makeconftest(
'''
import pytest
@pytest.fixture
def _arg1():
@@ -113,46 +130,54 @@ def test_verbose_include_private_fixtures_and_loc(testdir):
@pytest.fixture
def arg2(_arg1):
"""arg2 from conftest"""
''')
p = testdir.makepyfile('''
'''
)
p = testdir.makepyfile(
'''
import pytest
@pytest.fixture
def arg3():
"""arg3 from testmodule"""
def test_args(arg2, arg3):
pass
''')
'''
)
result = testdir.runpytest("--fixtures-per-test", "-v", p)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*fixtures used by test_args*',
'*(test_verbose_include_private_fixtures_and_loc.py:6)*',
'_arg1 -- conftest.py:3',
' _arg1 from conftest',
'arg2 -- conftest.py:6',
' arg2 from conftest',
'arg3 -- test_verbose_include_private_fixtures_and_loc.py:3',
' arg3 from testmodule',
])
result.stdout.fnmatch_lines(
[
"*fixtures used by test_args*",
"*(test_verbose_include_private_fixtures_and_loc.py:6)*",
"_arg1 -- conftest.py:3",
" _arg1 from conftest",
"arg2 -- conftest.py:6",
" arg2 from conftest",
"arg3 -- test_verbose_include_private_fixtures_and_loc.py:3",
" arg3 from testmodule",
]
)
def test_doctest_items(testdir):
testdir.makepyfile('''
testdir.makepyfile(
'''
def foo():
"""
>>> 1 + 1
2
"""
''')
testdir.maketxtfile('''
'''
)
testdir.maketxtfile(
"""
>>> 1 + 1
2
''')
result = testdir.runpytest("--fixtures-per-test", "--doctest-modules",
"--doctest-glob=*.txt", "-v")
"""
)
result = testdir.runpytest(
"--fixtures-per-test", "--doctest-modules", "--doctest-glob=*.txt", "-v"
)
assert result.ret == 0
result.stdout.fnmatch_lines([
'*collected 2 items*',
])
result.stdout.fnmatch_lines(["*collected 2 items*"])

View File

@@ -18,5 +18,5 @@ def test_pycollector_makeitem_is_deprecated():
collector = PyCollectorMock()
with pytest.deprecated_call():
collector.makeitem('foo', 'bar')
collector.makeitem("foo", "bar")
assert collector.called