Fix formatting of print() calls

This commit is contained in:
Anthony Sottile 2018-11-22 00:15:14 -08:00
parent 3eaa6d8835
commit b3700f61ba
19 changed files with 99 additions and 99 deletions

View File

@ -52,7 +52,7 @@ is that you can use print statements for debugging::
# content of test_module.py # content of test_module.py
def setup_function(function): def setup_function(function):
print ("setting up %s" % function) print("setting up %s" % function)
def test_func1(): def test_func1():
assert True assert True

View File

@ -466,7 +466,7 @@ test function. From a conftest file we can read it like this::
def pytest_runtest_setup(item): def pytest_runtest_setup(item):
for mark in item.iter_markers(name='glob'): for mark in item.iter_markers(name='glob'):
print ("glob args=%s kwargs=%s" %(mark.args, mark.kwargs)) print("glob args=%s kwargs=%s" % (mark.args, mark.kwargs))
sys.stdout.flush() sys.stdout.flush()
Let's run this without capturing output and see what we get:: Let's run this without capturing output and see what we get::

View File

@ -13,7 +13,7 @@ calls it::
@pytest.fixture(scope="session", autouse=True) @pytest.fixture(scope="session", autouse=True)
def callattr_ahead_of_alltests(request): def callattr_ahead_of_alltests(request):
print ("callattr_ahead_of_alltests called") print("callattr_ahead_of_alltests called")
seen = set([None]) seen = set([None])
session = request.node session = request.node
for item in session.items: for item in session.items:
@ -31,20 +31,20 @@ will be called ahead of running any tests::
class TestHello(object): class TestHello(object):
@classmethod @classmethod
def callme(cls): def callme(cls):
print ("callme called!") print("callme called!")
def test_method1(self): def test_method1(self):
print ("test_method1 called") print("test_method1 called")
def test_method2(self): def test_method2(self):
print ("test_method1 called") print("test_method1 called")
class TestOther(object): class TestOther(object):
@classmethod @classmethod
def callme(cls): def callme(cls):
print ("callme other called") print("callme other called")
def test_other(self): def test_other(self):
print ("test other") print("test other")
# works with unittest as well ... # works with unittest as well ...
import unittest import unittest
@ -52,10 +52,10 @@ will be called ahead of running any tests::
class SomeTest(unittest.TestCase): class SomeTest(unittest.TestCase):
@classmethod @classmethod
def callme(self): def callme(self):
print ("SomeTest callme called") print("SomeTest callme called")
def test_unit1(self): def test_unit1(self):
print ("test_unit1 method called") print("test_unit1 method called")
If you run this without output capturing:: If you run this without output capturing::

View File

@ -460,7 +460,7 @@ read an optional server URL from the test module which uses our fixture::
server = getattr(request.module, "smtpserver", "smtp.gmail.com") server = getattr(request.module, "smtpserver", "smtp.gmail.com")
smtp_connection = smtplib.SMTP(server, 587, timeout=5) smtp_connection = smtplib.SMTP(server, 587, timeout=5)
yield smtp_connection yield smtp_connection
print ("finalizing %s (%s)" % (smtp_connection, server)) print("finalizing %s (%s)" % (smtp_connection, server))
smtp_connection.close() smtp_connection.close()
We use the ``request.module`` attribute to optionally obtain an We use the ``request.module`` attribute to optionally obtain an
@ -821,23 +821,23 @@ to show the setup/teardown flow::
@pytest.fixture(scope="module", params=["mod1", "mod2"]) @pytest.fixture(scope="module", params=["mod1", "mod2"])
def modarg(request): def modarg(request):
param = request.param param = request.param
print (" SETUP modarg %s" % param) print(" SETUP modarg %s" % param)
yield param yield param
print (" TEARDOWN modarg %s" % param) print(" TEARDOWN modarg %s" % param)
@pytest.fixture(scope="function", params=[1,2]) @pytest.fixture(scope="function", params=[1,2])
def otherarg(request): def otherarg(request):
param = request.param param = request.param
print (" SETUP otherarg %s" % param) print(" SETUP otherarg %s" % param)
yield param yield param
print (" TEARDOWN otherarg %s" % param) print(" TEARDOWN otherarg %s" % param)
def test_0(otherarg): def test_0(otherarg):
print (" RUN test0 with otherarg %s" % otherarg) print(" RUN test0 with otherarg %s" % otherarg)
def test_1(modarg): def test_1(modarg):
print (" RUN test1 with modarg %s" % modarg) print(" RUN test1 with modarg %s" % modarg)
def test_2(otherarg, modarg): def test_2(otherarg, modarg):
print (" RUN test2 with otherarg %s and modarg %s" % (otherarg, modarg)) print(" RUN test2 with otherarg %s and modarg %s" % (otherarg, modarg))
Let's run the tests in verbose mode and with looking at the print-output:: Let's run the tests in verbose mode and with looking at the print-output::

View File

@ -26,9 +26,9 @@ a per-session Database object::
# content of conftest.py # content of conftest.py
class Database(object): class Database(object):
def __init__(self): def __init__(self):
print ("database instance created") print("database instance created")
def destroy(self): def destroy(self):
print ("database instance destroyed") print("database instance destroyed")
def pytest_funcarg__db(request): def pytest_funcarg__db(request):
return request.cached_setup(setup=DataBase, return request.cached_setup(setup=DataBase,

View File

@ -138,7 +138,7 @@ Request a unique temporary directory for functional tests
# content of test_tmpdir.py # content of test_tmpdir.py
def test_needsfiles(tmpdir): def test_needsfiles(tmpdir):
print (tmpdir) print(tmpdir)
assert 0 assert 0
List the name ``tmpdir`` in the test function signature and ``pytest`` will lookup and call a fixture factory to create the resource before performing the test function call. Before the test runs, ``pytest`` creates a unique-per-test-invocation temporary directory:: List the name ``tmpdir`` in the test function signature and ``pytest`` will lookup and call a fixture factory to create the resource before performing the test function call. Before the test runs, ``pytest`` creates a unique-per-test-invocation temporary directory::
@ -151,7 +151,7 @@ List the name ``tmpdir`` in the test function signature and ``pytest`` will look
tmpdir = local('PYTEST_TMPDIR/test_needsfiles0') tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
def test_needsfiles(tmpdir): def test_needsfiles(tmpdir):
print (tmpdir) print(tmpdir)
> assert 0 > assert 0
E assert 0 E assert 0

View File

@ -73,7 +73,7 @@ sub directory but not for other directories::
a/conftest.py: a/conftest.py:
def pytest_runtest_setup(item): def pytest_runtest_setup(item):
# called for running each test in 'a' directory # called for running each test in 'a' directory
print ("setting up", item) print("setting up", item)
a/test_sub.py: a/test_sub.py:
def test_sub(): def test_sub():

View File

@ -65,9 +65,9 @@ def report(issues):
print(title) print(title)
# print() # print()
# lines = body.split("\n") # lines = body.split("\n")
# print ("\n".join(lines[:3])) # print("\n".join(lines[:3]))
# if len(lines) > 3 or len(body) > 240: # if len(lines) > 3 or len(body) > 240:
# print ("...") # print("...")
print("\n\nFound %s open issues" % len(issues)) print("\n\nFound %s open issues" % len(issues))

View File

@ -206,7 +206,7 @@ class TestGeneralUsage(object):
testdir.makeconftest( testdir.makeconftest(
""" """
import sys import sys
print ("should not be seen") print("should not be seen")
sys.stderr.write("stder42\\n") sys.stderr.write("stder42\\n")
""" """
) )
@ -218,7 +218,7 @@ class TestGeneralUsage(object):
def test_conftest_printing_shows_if_error(self, testdir): def test_conftest_printing_shows_if_error(self, testdir):
testdir.makeconftest( testdir.makeconftest(
""" """
print ("should be seen") print("should be seen")
assert 0 assert 0
""" """
) )
@ -301,7 +301,7 @@ class TestGeneralUsage(object):
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.addcall({'x': 3}, id='hello-123') metafunc.addcall({'x': 3}, id='hello-123')
def pytest_runtest_setup(item): def pytest_runtest_setup(item):
print (item.keywords) print(item.keywords)
if 'hello-123' in item.keywords: if 'hello-123' in item.keywords:
pytest.skip("hello") pytest.skip("hello")
assert 0 assert 0

View File

@ -1495,7 +1495,7 @@ class TestFixtureManagerParseFactories(object):
return "class" return "class"
def test_hello(self, item, fm): def test_hello(self, item, fm):
faclist = fm.getfixturedefs("hello", item.nodeid) faclist = fm.getfixturedefs("hello", item.nodeid)
print (faclist) print(faclist)
assert len(faclist) == 3 assert len(faclist) == 3
assert faclist[0].func(item._request) == "conftest" assert faclist[0].func(item._request) == "conftest"
@ -2040,7 +2040,7 @@ class TestAutouseManagement(object):
values.append("step2-%d" % item) values.append("step2-%d" % item)
def test_finish(): def test_finish():
print (values) print(values)
assert values == ["setup-1", "step1-1", "step2-1", "teardown-1", assert values == ["setup-1", "step1-1", "step2-1", "teardown-1",
"setup-2", "step1-2", "step2-2", "teardown-2",] "setup-2", "step1-2", "step2-2", "teardown-2",]
""" """
@ -2880,7 +2880,7 @@ class TestFixtureMarker(object):
def base(request, fix1): def base(request, fix1):
def cleanup_base(): def cleanup_base():
values.append("fin_base") values.append("fin_base")
print ("finalizing base") print("finalizing base")
request.addfinalizer(cleanup_base) request.addfinalizer(cleanup_base)
def test_begin(): def test_begin():
@ -3480,13 +3480,13 @@ class TestContextManagerFixtureFuncs(object):
from test_context import fixture from test_context import fixture
@fixture @fixture
def arg1(): def arg1():
print ("setup") print("setup")
yield 1 yield 1
print ("teardown") print("teardown")
def test_1(arg1): def test_1(arg1):
print ("test1", arg1) print("test1", arg1)
def test_2(arg1): def test_2(arg1):
print ("test2", arg1) print("test2", arg1)
assert 0 assert 0
""" """
) )
@ -3509,13 +3509,13 @@ class TestContextManagerFixtureFuncs(object):
from test_context import fixture from test_context import fixture
@fixture(scope="module") @fixture(scope="module")
def arg1(): def arg1():
print ("setup") print("setup")
yield 1 yield 1
print ("teardown") print("teardown")
def test_1(arg1): def test_1(arg1):
print ("test1", arg1) print("test1", arg1)
def test_2(arg1): def test_2(arg1):
print ("test2", arg1) print("test2", arg1)
""" """
) )
result = testdir.runpytest("-s") result = testdir.runpytest("-s")

View File

@ -283,7 +283,7 @@ class TestReRunTests(object):
global count, req global count, req
assert request != req assert request != req
req = request req = request
print ("fix count %s" % count) print("fix count %s" % count)
count += 1 count += 1
def test_fix(fix): def test_fix(fix):
pass pass

View File

@ -43,7 +43,7 @@ class TestRaises(object):
with pytest.raises(ZeroDivisionError) as excinfo: with pytest.raises(ZeroDivisionError) as excinfo:
assert isinstance(excinfo, _pytest._code.ExceptionInfo) assert isinstance(excinfo, _pytest._code.ExceptionInfo)
1/0 1/0
print (excinfo) print(excinfo)
assert excinfo.type == ZeroDivisionError assert excinfo.type == ZeroDivisionError
assert isinstance(excinfo.value, ZeroDivisionError) assert isinstance(excinfo.value, ZeroDivisionError)

View File

@ -107,8 +107,8 @@ def test_capturing_unicode(testdir, method):
# taken from issue 227 from nosetests # taken from issue 227 from nosetests
def test_unicode(): def test_unicode():
import sys import sys
print (sys.stdout) print(sys.stdout)
print (%s) print(%s)
""" """
% obj % obj
) )
@ -121,7 +121,7 @@ def test_capturing_bytes_in_utf8_encoding(testdir, method):
testdir.makepyfile( testdir.makepyfile(
""" """
def test_unicode(): def test_unicode():
print ('b\\u00f6y') print('b\\u00f6y')
""" """
) )
result = testdir.runpytest("--capture=%s" % method) result = testdir.runpytest("--capture=%s" % method)
@ -131,7 +131,7 @@ def test_capturing_bytes_in_utf8_encoding(testdir, method):
def test_collect_capturing(testdir): def test_collect_capturing(testdir):
p = testdir.makepyfile( p = testdir.makepyfile(
""" """
print ("collect %s failure" % 13) print("collect %s failure" % 13)
import xyz42123 import xyz42123
""" """
) )
@ -144,14 +144,14 @@ class TestPerTestCapturing(object):
p = testdir.makepyfile( p = testdir.makepyfile(
""" """
def setup_module(mod): def setup_module(mod):
print ("setup module") print("setup module")
def setup_function(function): def setup_function(function):
print ("setup " + function.__name__) print("setup " + function.__name__)
def test_func1(): def test_func1():
print ("in func1") print("in func1")
assert 0 assert 0
def test_func2(): def test_func2():
print ("in func2") print("in func2")
assert 0 assert 0
""" """
) )
@ -172,14 +172,14 @@ class TestPerTestCapturing(object):
""" """
import sys import sys
def setup_module(func): def setup_module(func):
print ("module-setup") print("module-setup")
def setup_function(func): def setup_function(func):
print ("function-setup") print("function-setup")
def test_func(): def test_func():
print ("in function") print("in function")
assert 0 assert 0
def teardown_function(func): def teardown_function(func):
print ("in teardown") print("in teardown")
""" """
) )
result = testdir.runpytest(p) result = testdir.runpytest(p)
@ -198,9 +198,9 @@ class TestPerTestCapturing(object):
p = testdir.makepyfile( p = testdir.makepyfile(
""" """
def test_func1(): def test_func1():
print ("in func1") print("in func1")
def test_func2(): def test_func2():
print ("in func2") print("in func2")
assert 0 assert 0
""" """
) )
@ -213,12 +213,12 @@ class TestPerTestCapturing(object):
p = testdir.makepyfile( p = testdir.makepyfile(
""" """
def setup_function(function): def setup_function(function):
print ("setup func1") print("setup func1")
def teardown_function(function): def teardown_function(function):
print ("teardown func1") print("teardown func1")
assert 0 assert 0
def test_func1(): def test_func1():
print ("in func1") print("in func1")
pass pass
""" """
) )
@ -238,7 +238,7 @@ class TestPerTestCapturing(object):
p = testdir.makepyfile( p = testdir.makepyfile(
""" """
def teardown_module(mod): def teardown_module(mod):
print ("teardown module") print("teardown module")
assert 0 assert 0
def test_func(): def test_func():
pass pass
@ -259,10 +259,10 @@ class TestPerTestCapturing(object):
"""\ """\
import sys import sys
def test_capturing(): def test_capturing():
print (42) print(42)
sys.stderr.write(str(23)) sys.stderr.write(str(23))
def test_capturing_error(): def test_capturing_error():
print (1) print(1)
sys.stderr.write(str(2)) sys.stderr.write(str(2))
raise ValueError raise ValueError
""" """
@ -392,7 +392,7 @@ class TestCaptureFixture(object):
reprec = testdir.inline_runsource( reprec = testdir.inline_runsource(
"""\ """\
def test_hello(capsys): def test_hello(capsys):
print (42) print(42)
out, err = capsys.readouterr() out, err = capsys.readouterr()
assert out.startswith("42") assert out.startswith("42")
""", """,
@ -460,7 +460,7 @@ class TestCaptureFixture(object):
p = testdir.makepyfile( p = testdir.makepyfile(
"""\ """\
def test_hello(cap{}): def test_hello(cap{}):
print ("xxx42xxx") print("xxx42xxx")
assert 0 assert 0
""".format( """.format(
method method
@ -702,7 +702,7 @@ def test_capture_conftest_runtest_setup(testdir):
testdir.makeconftest( testdir.makeconftest(
""" """
def pytest_runtest_setup(): def pytest_runtest_setup():
print ("hello19") print("hello19")
""" """
) )
testdir.makepyfile("def test_func(): pass") testdir.makepyfile("def test_func(): pass")
@ -737,7 +737,7 @@ def test_capture_early_option_parsing(testdir):
testdir.makeconftest( testdir.makeconftest(
""" """
def pytest_runtest_setup(): def pytest_runtest_setup():
print ("hello19") print("hello19")
""" """
) )
testdir.makepyfile("def test_func(): pass") testdir.makepyfile("def test_func(): pass")
@ -1302,14 +1302,14 @@ def test_capturing_and_logging_fundamentals(testdir, method):
logging.warn("hello1") logging.warn("hello1")
outerr = cap.readouterr() outerr = cap.readouterr()
print ("suspend, captured %%s" %%(outerr,)) print("suspend, captured %%s" %%(outerr,))
logging.warn("hello2") logging.warn("hello2")
cap.pop_outerr_to_orig() cap.pop_outerr_to_orig()
logging.warn("hello3") logging.warn("hello3")
outerr = cap.readouterr() outerr = cap.readouterr()
print ("suspend2, captured %%s" %% (outerr,)) print("suspend2, captured %%s" %% (outerr,))
""" """
% (method,) % (method,)
) )

View File

@ -371,7 +371,7 @@ class TestPython(object):
import sys import sys
def test_fail(): def test_fail():
print ("hello-stdout") print("hello-stdout")
sys.stderr.write("hello-stderr\\n") sys.stderr.write("hello-stderr\\n")
logging.info('info msg') logging.info('info msg')
logging.warning('warning msg') logging.warning('warning msg')
@ -589,7 +589,7 @@ class TestPython(object):
""" """
# coding: latin1 # coding: latin1
def test_hello(): def test_hello():
print (%r) print(%r)
assert 0 assert 0
""" """
% value % value

View File

@ -189,7 +189,7 @@ def test_ini_markers(testdir):
""" """
def test_markers(pytestconfig): def test_markers(pytestconfig):
markers = pytestconfig.getini("markers") markers = pytestconfig.getini("markers")
print (markers) print(markers)
assert len(markers) >= 2 assert len(markers) >= 2
assert markers[0].startswith("a1:") assert markers[0].startswith("a1:")
assert markers[1].startswith("a2:") assert markers[1].startswith("a2:")

View File

@ -71,11 +71,11 @@ def test_nose_setup_func(testdir):
@with_setup(my_setup, my_teardown) @with_setup(my_setup, my_teardown)
def test_hello(): def test_hello():
print (values) print(values)
assert values == [1] assert values == [1]
def test_world(): def test_world():
print (values) print(values)
assert values == [1,2] assert values == [1,2]
""" """
@ -95,11 +95,11 @@ def test_nose_setup_func_failure(testdir):
@with_setup(my_setup, my_teardown) @with_setup(my_setup, my_teardown)
def test_hello(): def test_hello():
print (values) print(values)
assert values == [1] assert values == [1]
def test_world(): def test_world():
print (values) print(values)
assert values == [1,2] assert values == [1,2]
""" """
@ -147,11 +147,11 @@ def test_nose_setup_partial(testdir):
my_teardown_partial = partial(my_teardown, 2) my_teardown_partial = partial(my_teardown, 2)
def test_hello(): def test_hello():
print (values) print(values)
assert values == [1] assert values == [1]
def test_world(): def test_world():
print (values) print(values)
assert values == [1,2] assert values == [1,2]
test_hello.setup = my_setup_partial test_hello.setup = my_setup_partial
@ -202,21 +202,21 @@ def test_nose_test_generator_fixtures(testdir):
class TestClass(object): class TestClass(object):
def setup(self): def setup(self):
print ("setup called in %s" % self) print("setup called in %s" % self)
self.called = ['setup'] self.called = ['setup']
def teardown(self): def teardown(self):
print ("teardown called in %s" % self) print("teardown called in %s" % self)
eq_(self.called, ['setup']) eq_(self.called, ['setup'])
self.called.append('teardown') self.called.append('teardown')
def test(self): def test(self):
print ("test called in %s" % self) print("test called in %s" % self)
for i in range(0, 5): for i in range(0, 5):
yield self.check, i yield self.check, i
def check(self, i): def check(self, i):
print ("check called in %s" % self) print("check called in %s" % self)
expect = ['setup'] expect = ['setup']
#for x in range(0, i): #for x in range(0, i):
# expect.append('setup') # expect.append('setup')

View File

@ -202,21 +202,21 @@ def test_func_generator_setup(testdir):
import sys import sys
def setup_module(mod): def setup_module(mod):
print ("setup_module") print("setup_module")
mod.x = [] mod.x = []
def setup_function(fun): def setup_function(fun):
print ("setup_function") print("setup_function")
x.append(1) x.append(1)
def teardown_function(fun): def teardown_function(fun):
print ("teardown_function") print("teardown_function")
x.pop() x.pop()
def test_one(): def test_one():
assert x == [1] assert x == [1]
def check(): def check():
print ("check") print("check")
sys.stderr.write("e\\n") sys.stderr.write("e\\n")
assert x == [1] assert x == [1]
yield check yield check

View File

@ -365,7 +365,7 @@ class TestFixtureReporting(object):
testdir.makepyfile( testdir.makepyfile(
""" """
def setup_function(function): def setup_function(function):
print ("setup func") print("setup func")
assert 0 assert 0
def test_nada(): def test_nada():
pass pass
@ -389,7 +389,7 @@ class TestFixtureReporting(object):
def test_nada(): def test_nada():
pass pass
def teardown_function(function): def teardown_function(function):
print ("teardown func") print("teardown func")
assert 0 assert 0
""" """
) )
@ -412,7 +412,7 @@ class TestFixtureReporting(object):
assert 0, "failingfunc" assert 0, "failingfunc"
def teardown_function(function): def teardown_function(function):
print ("teardown func") print("teardown func")
assert False assert False
""" """
) )
@ -436,13 +436,13 @@ class TestFixtureReporting(object):
testdir.makepyfile( testdir.makepyfile(
""" """
def setup_function(function): def setup_function(function):
print ("setup func") print("setup func")
def test_fail(): def test_fail():
assert 0, "failingfunc" assert 0, "failingfunc"
def teardown_function(function): def teardown_function(function):
print ("teardown func") print("teardown func")
""" """
) )
result = testdir.runpytest() result = testdir.runpytest()
@ -854,7 +854,7 @@ class TestGenericReporting(object):
def g(): def g():
raise IndexError raise IndexError
def test_func(): def test_func():
print (6*7) print(6*7)
g() # --calling-- g() # --calling--
""" """
) )
@ -863,9 +863,9 @@ class TestGenericReporting(object):
result = testdir.runpytest("--tb=%s" % tbopt) result = testdir.runpytest("--tb=%s" % tbopt)
s = result.stdout.str() s = result.stdout.str()
if tbopt == "long": if tbopt == "long":
assert "print (6*7)" in s assert "print(6*7)" in s
else: else:
assert "print (6*7)" not in s assert "print(6*7)" not in s
if tbopt != "no": if tbopt != "no":
assert "--calling--" in s assert "--calling--" in s
assert "IndexError" in s assert "IndexError" in s
@ -881,7 +881,7 @@ class TestGenericReporting(object):
def g(): def g():
raise IndexError raise IndexError
def test_func1(): def test_func1():
print (6*7) print(6*7)
g() # --calling-- g() # --calling--
def test_func2(): def test_func2():
assert 0, "hello" assert 0, "hello"

View File

@ -244,7 +244,7 @@ def test_setup_failure_is_shown(testdir):
def setUp(self): def setUp(self):
assert 0, "down1" assert 0, "down1"
def test_method(self): def test_method(self):
print ("never42") print("never42")
xyz xyz
""" """
) )
@ -610,14 +610,14 @@ def test_djangolike_testcase(testdir):
class DjangoLikeTestCase(TestCase): class DjangoLikeTestCase(TestCase):
def setUp(self): def setUp(self):
print ("setUp()") print("setUp()")
def test_presetup_has_been_run(self): def test_presetup_has_been_run(self):
print ("test_thing()") print("test_thing()")
self.assertTrue(hasattr(self, 'was_presetup')) self.assertTrue(hasattr(self, 'was_presetup'))
def tearDown(self): def tearDown(self):
print ("tearDown()") print("tearDown()")
def __call__(self, result=None): def __call__(self, result=None):
try: try:
@ -639,11 +639,11 @@ def test_djangolike_testcase(testdir):
return return
def _pre_setup(self): def _pre_setup(self):
print ("_pre_setup()") print("_pre_setup()")
self.was_presetup = True self.was_presetup = True
def _post_teardown(self): def _post_teardown(self):
print ("_post_teardown()") print("_post_teardown()")
""" """
) )
result = testdir.runpytest("-s") result = testdir.runpytest("-s")