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

@@ -12,6 +12,7 @@ from _pytest import runner, main, outcomes
class TestSetupState(object):
def test_setup(self, testdir):
ss = runner.SetupState()
item = testdir.getitem("def test_func(): pass")
@@ -30,11 +31,13 @@ class TestSetupState(object):
ss.teardown_exact(item, None)
def test_setup_fails_and_failure_is_cached(self, testdir):
item = testdir.getitem("""
item = testdir.getitem(
"""
def setup_module(mod):
raise ValueError(42)
def test_func(): pass
""")
"""
)
ss = runner.SetupState()
pytest.raises(ValueError, lambda: ss.prepare(item))
pytest.raises(ValueError, lambda: ss.prepare(item))
@@ -43,13 +46,13 @@ class TestSetupState(object):
r = []
def fin1():
r.append('fin1')
r.append("fin1")
def fin2():
raise Exception('oops')
raise Exception("oops")
def fin3():
r.append('fin3')
r.append("fin3")
item = testdir.getitem("def test_func(): pass")
ss = runner.SetupState()
@@ -58,17 +61,17 @@ class TestSetupState(object):
ss.addfinalizer(fin3, item)
with pytest.raises(Exception) as err:
ss._callfinalizers(item)
assert err.value.args == ('oops',)
assert r == ['fin3', 'fin1']
assert err.value.args == ("oops",)
assert r == ["fin3", "fin1"]
def test_teardown_multiple_fail(self, testdir):
# Ensure the first exception is the one which is re-raised.
# Ideally both would be reported however.
def fin1():
raise Exception('oops1')
raise Exception("oops1")
def fin2():
raise Exception('oops2')
raise Exception("oops2")
item = testdir.getitem("def test_func(): pass")
ss = runner.SetupState()
@@ -76,15 +79,18 @@ class TestSetupState(object):
ss.addfinalizer(fin2, item)
with pytest.raises(Exception) as err:
ss._callfinalizers(item)
assert err.value.args == ('oops2',)
assert err.value.args == ("oops2",)
class BaseFunctionalTests(object):
def test_passfunction(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
def test_func():
pass
""")
"""
)
rep = reports[1]
assert rep.passed
assert not rep.failed
@@ -92,10 +98,12 @@ class BaseFunctionalTests(object):
assert not rep.longrepr
def test_failfunction(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
def test_func():
assert 0
""")
"""
)
rep = reports[1]
assert not rep.passed
assert not rep.skipped
@@ -105,11 +113,13 @@ class BaseFunctionalTests(object):
# assert isinstance(rep.longrepr, ReprExceptionInfo)
def test_skipfunction(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
import pytest
def test_func():
pytest.skip("hello")
""")
"""
)
rep = reports[1]
assert not rep.failed
assert not rep.passed
@@ -123,13 +133,15 @@ class BaseFunctionalTests(object):
# assert not rep.skipped.failurerepr
def test_skip_in_setup_function(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
import pytest
def setup_function(func):
pytest.skip("hello")
def test_func():
pass
""")
"""
)
print(reports)
rep = reports[0]
assert not rep.failed
@@ -142,13 +154,15 @@ class BaseFunctionalTests(object):
assert reports[1].passed # teardown
def test_failure_in_setup_function(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
import pytest
def setup_function(func):
raise ValueError(42)
def test_func():
pass
""")
"""
)
rep = reports[0]
assert not rep.skipped
assert not rep.passed
@@ -157,13 +171,15 @@ class BaseFunctionalTests(object):
assert len(reports) == 2
def test_failure_in_teardown_function(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
import pytest
def teardown_function(func):
raise ValueError(42)
def test_func():
pass
""")
"""
)
print(reports)
assert len(reports) == 3
rep = reports[2]
@@ -175,17 +191,21 @@ class BaseFunctionalTests(object):
# assert rep.longrepr.reprtraceback.reprentries
def test_custom_failure_repr(self, testdir):
testdir.makepyfile(conftest="""
testdir.makepyfile(
conftest="""
import pytest
class Function(pytest.Function):
def repr_failure(self, excinfo):
return "hello"
""")
reports = testdir.runitem("""
"""
)
reports = testdir.runitem(
"""
import pytest
def test_func():
assert 0
""")
"""
)
rep = reports[1]
assert not rep.skipped
assert not rep.passed
@@ -196,28 +216,36 @@ class BaseFunctionalTests(object):
# assert rep.failed.failurerepr == "hello"
def test_teardown_final_returncode(self, testdir):
rec = testdir.inline_runsource("""
rec = testdir.inline_runsource(
"""
def test_func():
pass
def teardown_function(func):
raise ValueError(42)
""")
"""
)
assert rec.ret == 1
def test_logstart_logfinish_hooks(self, testdir):
rec = testdir.inline_runsource("""
rec = testdir.inline_runsource(
"""
import pytest
def test_func():
pass
""")
"""
)
reps = rec.getcalls("pytest_runtest_logstart pytest_runtest_logfinish")
assert [x._name for x in reps] == ['pytest_runtest_logstart', 'pytest_runtest_logfinish']
assert (
[x._name for x in reps]
== ["pytest_runtest_logstart", "pytest_runtest_logfinish"]
)
for rep in reps:
assert rep.nodeid == 'test_logstart_logfinish_hooks.py::test_func'
assert rep.location == ('test_logstart_logfinish_hooks.py', 1, 'test_func')
assert rep.nodeid == "test_logstart_logfinish_hooks.py::test_func"
assert rep.location == ("test_logstart_logfinish_hooks.py", 1, "test_func")
def test_exact_teardown_issue90(self, testdir):
rec = testdir.inline_runsource("""
rec = testdir.inline_runsource(
"""
import pytest
class TestClass(object):
@@ -238,7 +266,8 @@ class BaseFunctionalTests(object):
traceback.format_exception(*excinfo)
def teardown_function(func):
raise ValueError(42)
""")
"""
)
reps = rec.getreports("pytest_runtest_logreport")
print(reps)
for i in range(2):
@@ -256,7 +285,8 @@ class BaseFunctionalTests(object):
def test_exact_teardown_issue1206(self, testdir):
"""issue shadowing error with wrong number of arguments on teardown_method."""
rec = testdir.inline_runsource("""
rec = testdir.inline_runsource(
"""
import pytest
class TestClass(object):
@@ -265,18 +295,19 @@ class BaseFunctionalTests(object):
def test_method(self):
assert True
""")
"""
)
reps = rec.getreports("pytest_runtest_logreport")
print(reps)
assert len(reps) == 3
#
assert reps[0].nodeid.endswith("test_method")
assert reps[0].passed
assert reps[0].when == 'setup'
assert reps[0].when == "setup"
#
assert reps[1].nodeid.endswith("test_method")
assert reps[1].passed
assert reps[1].when == 'call'
assert reps[1].when == "call"
#
assert reps[2].nodeid.endswith("test_method")
assert reps[2].failed
@@ -285,22 +316,26 @@ class BaseFunctionalTests(object):
# python3 error
"TypeError: teardown_method() missing 2 required positional arguments: 'y' and 'z'",
# python2 error
'TypeError: teardown_method() takes exactly 4 arguments (2 given)'
"TypeError: teardown_method() takes exactly 4 arguments (2 given)",
)
def test_failure_in_setup_function_ignores_custom_repr(self, testdir):
testdir.makepyfile(conftest="""
testdir.makepyfile(
conftest="""
import pytest
class Function(pytest.Function):
def repr_failure(self, excinfo):
assert 0
""")
reports = testdir.runitem("""
"""
)
reports = testdir.runitem(
"""
def setup_function(func):
raise ValueError(42)
def test_func():
pass
""")
"""
)
assert len(reports) == 2
rep = reports[0]
print(rep)
@@ -314,10 +349,12 @@ class BaseFunctionalTests(object):
def test_systemexit_does_not_bail_out(self, testdir):
try:
reports = testdir.runitem("""
reports = testdir.runitem(
"""
def test_func():
raise SystemExit(42)
""")
"""
)
except SystemExit:
pytest.fail("runner did not catch SystemExit")
rep = reports[1]
@@ -326,11 +363,13 @@ class BaseFunctionalTests(object):
def test_exit_propagates(self, testdir):
try:
testdir.runitem("""
testdir.runitem(
"""
import pytest
def test_func():
raise pytest.exit.Exception()
""")
"""
)
except pytest.exit.Exception:
pass
else:
@@ -338,17 +377,22 @@ class BaseFunctionalTests(object):
class TestExecutionNonForked(BaseFunctionalTests):
def getrunner(self):
def f(item):
return runner.runtestprotocol(item, log=False)
return f
def test_keyboardinterrupt_propagates(self, testdir):
try:
testdir.runitem("""
testdir.runitem(
"""
def test_func():
raise KeyboardInterrupt("fake")
""")
"""
)
except KeyboardInterrupt:
pass
else:
@@ -364,24 +408,29 @@ class TestExecutionForked(BaseFunctionalTests):
return boxed.forked_run_report
def test_suicide(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
def test_func():
import os
os.kill(os.getpid(), 15)
""")
"""
)
rep = reports[0]
assert rep.failed
assert rep.when == "???"
class TestSessionReports(object):
def test_collect_result(self, testdir):
col = testdir.getmodulecol("""
col = testdir.getmodulecol(
"""
def test_func1():
pass
class TestClass(object):
pass
""")
"""
)
rep = runner.collect_one_node(col)
assert not rep.failed
assert not rep.skipped
@@ -404,9 +453,11 @@ reporttypes = [
]
@pytest.mark.parametrize('reporttype', reporttypes, ids=[x.__name__ for x in reporttypes])
@pytest.mark.parametrize(
"reporttype", reporttypes, ids=[x.__name__ for x in reporttypes]
)
def test_report_extra_parameters(reporttype):
if hasattr(inspect, 'signature'):
if hasattr(inspect, "signature"):
args = list(inspect.signature(reporttype.__init__).parameters.keys())[1:]
else:
args = inspect.getargspec(reporttype.__init__)[0][1:]
@@ -416,23 +467,25 @@ def test_report_extra_parameters(reporttype):
def test_callinfo():
ci = runner.CallInfo(lambda: 0, '123')
ci = runner.CallInfo(lambda: 0, "123")
assert ci.when == "123"
assert ci.result == 0
assert "result" in repr(ci)
ci = runner.CallInfo(lambda: 0 / 0, '123')
ci = runner.CallInfo(lambda: 0 / 0, "123")
assert ci.when == "123"
assert not hasattr(ci, 'result')
assert not hasattr(ci, "result")
assert ci.excinfo
assert "exc" in repr(ci)
# design question: do we want general hooks in python files?
# then something like the following functional tests makes sense
@pytest.mark.xfail
def test_runtest_in_module_ordering(testdir):
p1 = testdir.makepyfile("""
p1 = testdir.makepyfile(
"""
import pytest
def pytest_runtest_setup(item): # runs after class-level!
item.function.mylist.append("module")
@@ -455,22 +508,21 @@ def test_runtest_in_module_ordering(testdir):
assert mylist == ['class', 'module'], mylist
def pytest_runtest_teardown(item):
del item.function.mylist
""")
"""
)
result = testdir.runpytest(p1)
result.stdout.fnmatch_lines([
"*2 passed*"
])
result.stdout.fnmatch_lines(["*2 passed*"])
def test_outcomeexception_exceptionattributes():
outcome = outcomes.OutcomeException('test')
outcome = outcomes.OutcomeException("test")
assert outcome.args[0] == outcome.msg
def test_outcomeexception_passes_except_Exception():
with pytest.raises(outcomes.OutcomeException):
try:
raise outcomes.OutcomeException('test')
raise outcomes.OutcomeException("test")
except Exception:
pass
@@ -493,72 +545,76 @@ def test_pytest_fail():
def test_pytest_exit_msg(testdir):
testdir.makeconftest("""
testdir.makeconftest(
"""
import pytest
def pytest_configure(config):
pytest.exit('oh noes')
""")
"""
)
result = testdir.runpytest()
result.stderr.fnmatch_lines([
"Exit: oh noes",
])
result.stderr.fnmatch_lines(["Exit: oh noes"])
def test_pytest_fail_notrace(testdir):
testdir.makepyfile("""
testdir.makepyfile(
"""
import pytest
def test_hello():
pytest.fail("hello", pytrace=False)
def teardown_function(function):
pytest.fail("world", pytrace=False)
""")
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines([
"world",
"hello",
])
assert 'def teardown_function' not in result.stdout.str()
result.stdout.fnmatch_lines(["world", "hello"])
assert "def teardown_function" not in result.stdout.str()
@pytest.mark.parametrize('str_prefix', ['u', ''])
@pytest.mark.parametrize("str_prefix", ["u", ""])
def test_pytest_fail_notrace_non_ascii(testdir, str_prefix):
"""Fix pytest.fail with pytrace=False with non-ascii characters (#1178).
This tests with native and unicode strings containing non-ascii chars.
"""
testdir.makepyfile(u"""
testdir.makepyfile(
u"""
# coding: utf-8
import pytest
def test_hello():
pytest.fail(%s'oh oh: ☺', pytrace=False)
""" % str_prefix)
"""
% str_prefix
)
result = testdir.runpytest()
if sys.version_info[0] >= 3:
result.stdout.fnmatch_lines(['*test_hello*', "oh oh: ☺"])
result.stdout.fnmatch_lines(["*test_hello*", "oh oh: ☺"])
else:
result.stdout.fnmatch_lines(['*test_hello*', "oh oh: *"])
assert 'def test_hello' not in result.stdout.str()
result.stdout.fnmatch_lines(["*test_hello*", "oh oh: *"])
assert "def test_hello" not in result.stdout.str()
def test_pytest_no_tests_collected_exit_status(testdir):
result = testdir.runpytest()
result.stdout.fnmatch_lines('*collected 0 items*')
result.stdout.fnmatch_lines("*collected 0 items*")
assert result.ret == main.EXIT_NOTESTSCOLLECTED
testdir.makepyfile(test_foo="""
testdir.makepyfile(
test_foo="""
def test_foo():
assert 1
""")
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines('*collected 1 item*')
result.stdout.fnmatch_lines('*1 passed*')
result.stdout.fnmatch_lines("*collected 1 item*")
result.stdout.fnmatch_lines("*1 passed*")
assert result.ret == main.EXIT_OK
result = testdir.runpytest('-k nonmatch')
result.stdout.fnmatch_lines('*collected 1 item*')
result.stdout.fnmatch_lines('*1 deselected*')
result = testdir.runpytest("-k nonmatch")
result.stdout.fnmatch_lines("*collected 1 item*")
result.stdout.fnmatch_lines("*1 deselected*")
assert result.ret == main.EXIT_NOTESTSCOLLECTED
@@ -592,9 +648,12 @@ def test_importorskip(monkeypatch):
mod = types.ModuleType("hello123")
mod.__version__ = "1.3"
monkeypatch.setitem(sys.modules, "hello123", mod)
pytest.raises(pytest.skip.Exception, """
pytest.raises(
pytest.skip.Exception,
"""
pytest.importorskip("hello123", minversion="1.3.1")
""")
""",
)
mod2 = pytest.importorskip("hello123", minversion="1.3")
assert mod2 == mod
except pytest.skip.Exception:
@@ -610,12 +669,15 @@ def test_importorskip_imports_last_module_part():
def test_importorskip_dev_module(monkeypatch):
try:
mod = types.ModuleType("mockmodule")
mod.__version__ = '0.13.0.dev-43290'
monkeypatch.setitem(sys.modules, 'mockmodule', mod)
mod2 = pytest.importorskip('mockmodule', minversion='0.12.0')
mod.__version__ = "0.13.0.dev-43290"
monkeypatch.setitem(sys.modules, "mockmodule", mod)
mod2 = pytest.importorskip("mockmodule", minversion="0.12.0")
assert mod2 == mod
pytest.raises(pytest.skip.Exception, """
pytest.importorskip('mockmodule1', minversion='0.14.0')""")
pytest.raises(
pytest.skip.Exception,
"""
pytest.importorskip('mockmodule1', minversion='0.14.0')""",
)
except pytest.skip.Exception:
print(_pytest._code.ExceptionInfo())
pytest.fail("spurious skip")
@@ -623,26 +685,31 @@ def test_importorskip_dev_module(monkeypatch):
def test_importorskip_module_level(testdir):
"""importorskip must be able to skip entire modules when used at module level"""
testdir.makepyfile('''
testdir.makepyfile(
"""
import pytest
foobarbaz = pytest.importorskip("foobarbaz")
def test_foo():
pass
''')
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(['*collected 0 items / 1 skipped*'])
result.stdout.fnmatch_lines(["*collected 0 items / 1 skipped*"])
def test_pytest_cmdline_main(testdir):
p = testdir.makepyfile("""
p = testdir.makepyfile(
"""
import pytest
def test_hello():
assert 1
if __name__ == '__main__':
pytest.cmdline.main([__file__])
""")
"""
)
import subprocess
popen = subprocess.Popen([sys.executable, str(p)], stdout=subprocess.PIPE)
popen.communicate()
ret = popen.wait()
@@ -650,7 +717,8 @@ def test_pytest_cmdline_main(testdir):
def test_unicode_in_longrepr(testdir):
testdir.makeconftest("""
testdir.makeconftest(
"""
# -*- coding: utf-8 -*-
import pytest
@pytest.hookimpl(hookwrapper=True)
@@ -659,52 +727,61 @@ def test_unicode_in_longrepr(testdir):
rep = outcome.get_result()
if rep.when == "call":
rep.longrepr = u'ä'
""")
testdir.makepyfile("""
"""
)
testdir.makepyfile(
"""
def test_out():
assert 0
""")
"""
)
result = testdir.runpytest()
assert result.ret == 1
assert "UnicodeEncodeError" not in result.stderr.str()
def test_failure_in_setup(testdir):
testdir.makepyfile("""
testdir.makepyfile(
"""
def setup_module():
0/0
def test_func():
pass
""")
"""
)
result = testdir.runpytest("--tb=line")
assert "def setup_module" not in result.stdout.str()
def test_makereport_getsource(testdir):
testdir.makepyfile("""
testdir.makepyfile(
"""
def test_foo():
if False: pass
else: assert False
""")
"""
)
result = testdir.runpytest()
assert 'INTERNALERROR' not in result.stdout.str()
result.stdout.fnmatch_lines(['*else: assert False*'])
assert "INTERNALERROR" not in result.stdout.str()
result.stdout.fnmatch_lines(["*else: assert False*"])
def test_makereport_getsource_dynamic_code(testdir, monkeypatch):
"""Test that exception in dynamically generated code doesn't break getting the source line."""
import inspect
original_findsource = inspect.findsource
def findsource(obj, *args, **kwargs):
# Can be triggered by dynamically created functions
if obj.__name__ == 'foo':
if obj.__name__ == "foo":
raise IndexError()
return original_findsource(obj, *args, **kwargs)
monkeypatch.setattr(inspect, 'findsource', findsource)
monkeypatch.setattr(inspect, "findsource", findsource)
testdir.makepyfile("""
testdir.makepyfile(
"""
import pytest
@pytest.fixture
@@ -713,9 +790,10 @@ def test_makereport_getsource_dynamic_code(testdir, monkeypatch):
def test_fix(foo):
assert False
""")
result = testdir.runpytest('-vv')
assert 'INTERNALERROR' not in result.stdout.str()
"""
)
result = testdir.runpytest("-vv")
assert "INTERNALERROR" not in result.stdout.str()
result.stdout.fnmatch_lines(["*test_fix*", "*fixture*'missing'*not found*"])
@@ -725,19 +803,20 @@ def test_store_except_info_on_error():
"""
# Simulate item that might raise a specific exception, depending on `raise_error` class var
class ItemMightRaise(object):
nodeid = 'item_that_raises'
nodeid = "item_that_raises"
raise_error = True
def runtest(self):
if self.raise_error:
raise IndexError('TEST')
raise IndexError("TEST")
try:
runner.pytest_runtest_call(ItemMightRaise())
except IndexError:
pass
# Check that exception info is stored on sys
assert sys.last_type is IndexError
assert sys.last_value.args[0] == 'TEST'
assert sys.last_value.args[0] == "TEST"
assert sys.last_traceback
# The next run should clear the exception info stored by the previous run
@@ -750,8 +829,11 @@ def test_store_except_info_on_error():
def test_current_test_env_var(testdir, monkeypatch):
pytest_current_test_vars = []
monkeypatch.setattr(sys, 'pytest_current_test_vars', pytest_current_test_vars, raising=False)
testdir.makepyfile('''
monkeypatch.setattr(
sys, "pytest_current_test_vars", pytest_current_test_vars, raising=False
)
testdir.makepyfile(
"""
import pytest
import sys
import os
@@ -764,13 +846,20 @@ def test_current_test_env_var(testdir, monkeypatch):
def test(fix):
sys.pytest_current_test_vars.append(('call', os.environ['PYTEST_CURRENT_TEST']))
''')
"""
)
result = testdir.runpytest_inprocess()
assert result.ret == 0
test_id = 'test_current_test_env_var.py::test'
assert pytest_current_test_vars == [
('setup', test_id + ' (setup)'), ('call', test_id + ' (call)'), ('teardown', test_id + ' (teardown)')]
assert 'PYTEST_CURRENT_TEST' not in os.environ
test_id = "test_current_test_env_var.py::test"
assert (
pytest_current_test_vars
== [
("setup", test_id + " (setup)"),
("call", test_id + " (call)"),
("teardown", test_id + " (teardown)"),
]
)
assert "PYTEST_CURRENT_TEST" not in os.environ
class TestReportContents(object):
@@ -782,24 +871,29 @@ class TestReportContents(object):
return lambda item: runner.runtestprotocol(item, log=False)
def test_longreprtext_pass(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
def test_func():
pass
""")
"""
)
rep = reports[1]
assert rep.longreprtext == ''
assert rep.longreprtext == ""
def test_longreprtext_failure(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
def test_func():
x = 1
assert x == 4
""")
"""
)
rep = reports[1]
assert 'assert 1 == 4' in rep.longreprtext
assert "assert 1 == 4" in rep.longreprtext
def test_captured_text(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
import pytest
import sys
@@ -816,21 +910,24 @@ class TestReportContents(object):
sys.stdout.write('call: stdout\\n')
sys.stderr.write('call: stderr\\n')
assert 0
""")
"""
)
setup, call, teardown = reports
assert setup.capstdout == 'setup: stdout\n'
assert call.capstdout == 'setup: stdout\ncall: stdout\n'
assert teardown.capstdout == 'setup: stdout\ncall: stdout\nteardown: stdout\n'
assert setup.capstdout == "setup: stdout\n"
assert call.capstdout == "setup: stdout\ncall: stdout\n"
assert teardown.capstdout == "setup: stdout\ncall: stdout\nteardown: stdout\n"
assert setup.capstderr == 'setup: stderr\n'
assert call.capstderr == 'setup: stderr\ncall: stderr\n'
assert teardown.capstderr == 'setup: stderr\ncall: stderr\nteardown: stderr\n'
assert setup.capstderr == "setup: stderr\n"
assert call.capstderr == "setup: stderr\ncall: stderr\n"
assert teardown.capstderr == "setup: stderr\ncall: stderr\nteardown: stderr\n"
def test_no_captured_text(self, testdir):
reports = testdir.runitem("""
reports = testdir.runitem(
"""
def test_func():
pass
""")
"""
)
rep = reports[1]
assert rep.capstdout == ''
assert rep.capstderr == ''
assert rep.capstdout == ""
assert rep.capstderr == ""