2013-11-29 08:29:14 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-10-25 15:01:29 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2010-09-18 20:03:28 +08:00
|
|
|
import sys
|
2014-09-27 09:29:47 +08:00
|
|
|
import textwrap
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2018-08-03 06:16:14 +08:00
|
|
|
import attr
|
2018-08-23 10:26:11 +08:00
|
|
|
import six
|
2018-10-25 15:01:29 +08:00
|
|
|
|
|
|
|
import _pytest.assertion as plugin
|
|
|
|
import pytest
|
2016-09-22 07:06:45 +08:00
|
|
|
from _pytest.assertion import truncate
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.assertion import util
|
2014-09-27 09:29:47 +08:00
|
|
|
|
|
|
|
PY3 = sys.version_info >= (3, 0)
|
2010-10-03 00:47:39 +08:00
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
|
|
|
|
def mock_config():
|
|
|
|
class Config(object):
|
|
|
|
verbose = False
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def getoption(self, name):
|
2018-05-23 22:48:46 +08:00
|
|
|
if name == "verbose":
|
2013-03-28 09:39:01 +08:00
|
|
|
return self.verbose
|
2018-05-23 22:48:46 +08:00
|
|
|
raise KeyError("Not mocked out: %s" % name)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
return Config()
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestImportHookInstallation(object):
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("initial_conftest", [True, False])
|
|
|
|
@pytest.mark.parametrize("mode", ["plain", "rewrite"])
|
2016-06-22 18:42:11 +08:00
|
|
|
def test_conftest_assertion_rewrite(self, testdir, initial_conftest, mode):
|
|
|
|
"""Test that conftest files are using assertion rewrite on import.
|
|
|
|
(#1619)
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("foo/tests").ensure(dir=1)
|
|
|
|
conftest_path = "conftest.py" if initial_conftest else "foo/conftest.py"
|
2016-06-22 18:42:11 +08:00
|
|
|
contents = {
|
|
|
|
conftest_path: """
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def check_first():
|
|
|
|
def check(values, value):
|
|
|
|
assert values.pop(0) == value
|
|
|
|
return check
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"foo/tests/test_foo.py": """
|
2016-06-22 18:42:11 +08:00
|
|
|
def test(check_first):
|
|
|
|
check_first([10, 30], 30)
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
2016-06-22 18:42:11 +08:00
|
|
|
}
|
|
|
|
testdir.makepyfile(**contents)
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest_subprocess("--assert=%s" % mode)
|
|
|
|
if mode == "plain":
|
|
|
|
expected = "E AssertionError"
|
|
|
|
elif mode == "rewrite":
|
|
|
|
expected = "*assert 10 == 30*"
|
2016-06-22 18:42:11 +08:00
|
|
|
else:
|
|
|
|
assert 0
|
|
|
|
result.stdout.fnmatch_lines([expected])
|
|
|
|
|
2017-01-20 07:33:51 +08:00
|
|
|
def test_rewrite_assertions_pytester_plugin(self, testdir):
|
|
|
|
"""
|
|
|
|
Assertions in the pytester plugin must also benefit from assertion
|
|
|
|
rewriting (#1920).
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-01-20 07:33:51 +08:00
|
|
|
pytest_plugins = ['pytester']
|
|
|
|
def test_dummy_failure(testdir): # how meta!
|
|
|
|
testdir.makepyfile('def test(): assert 0')
|
|
|
|
r = testdir.inline_run()
|
|
|
|
r.assertoutcome(passed=1)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-01-20 07:33:51 +08:00
|
|
|
result = testdir.runpytest_subprocess()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*assert 1 == 0*"])
|
2017-01-20 07:33:51 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("mode", ["plain", "rewrite"])
|
2016-06-26 00:26:45 +08:00
|
|
|
def test_pytest_plugins_rewrite(self, testdir, mode):
|
|
|
|
contents = {
|
2018-05-23 22:48:46 +08:00
|
|
|
"conftest.py": """
|
2016-06-26 00:26:45 +08:00
|
|
|
pytest_plugins = ['ham']
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"ham.py": """
|
2016-06-26 00:26:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def check_first():
|
|
|
|
def check(values, value):
|
|
|
|
assert values.pop(0) == value
|
|
|
|
return check
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"test_foo.py": """
|
2016-06-26 00:26:45 +08:00
|
|
|
def test_foo(check_first):
|
|
|
|
check_first([10, 30], 30)
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
testdir.makepyfile(**contents)
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest_subprocess("--assert=%s" % mode)
|
|
|
|
if mode == "plain":
|
|
|
|
expected = "E AssertionError"
|
|
|
|
elif mode == "rewrite":
|
|
|
|
expected = "*assert 10 == 30*"
|
2016-06-26 00:26:45 +08:00
|
|
|
else:
|
|
|
|
assert 0
|
|
|
|
result.stdout.fnmatch_lines([expected])
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("mode", ["str", "list"])
|
2016-08-31 09:15:53 +08:00
|
|
|
def test_pytest_plugins_rewrite_module_names(self, testdir, mode):
|
|
|
|
"""Test that pluginmanager correct marks pytest_plugins variables
|
|
|
|
for assertion rewriting if they are defined as plain strings or
|
|
|
|
list of strings (#1888).
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
plugins = '"ham"' if mode == "str" else '["ham"]'
|
2016-08-31 09:15:53 +08:00
|
|
|
contents = {
|
2018-05-23 22:48:46 +08:00
|
|
|
"conftest.py": """
|
2016-08-31 09:15:53 +08:00
|
|
|
pytest_plugins = {plugins}
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
plugins=plugins
|
|
|
|
),
|
|
|
|
"ham.py": """
|
2016-08-31 09:15:53 +08:00
|
|
|
import pytest
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"test_foo.py": """
|
2016-08-31 09:15:53 +08:00
|
|
|
def test_foo(pytestconfig):
|
|
|
|
assert 'ham' in pytestconfig.pluginmanager.rewrite_hook._must_rewrite
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
testdir.makepyfile(**contents)
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest_subprocess("--assert=rewrite")
|
2016-08-31 09:15:53 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2017-11-22 08:49:46 +08:00
|
|
|
def test_pytest_plugins_rewrite_module_names_correctly(self, testdir):
|
|
|
|
"""Test that we match files correctly when they are marked for rewriting (#2939)."""
|
|
|
|
contents = {
|
2018-05-23 22:48:46 +08:00
|
|
|
"conftest.py": """
|
2017-11-22 08:49:46 +08:00
|
|
|
pytest_plugins = "ham"
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"ham.py": "",
|
|
|
|
"hamster.py": "",
|
|
|
|
"test_foo.py": """
|
2017-11-22 08:49:46 +08:00
|
|
|
def test_foo(pytestconfig):
|
|
|
|
assert pytestconfig.pluginmanager.rewrite_hook.find_module('ham') is not None
|
|
|
|
assert pytestconfig.pluginmanager.rewrite_hook.find_module('hamster') is None
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
testdir.makepyfile(**contents)
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest_subprocess("--assert=rewrite")
|
2017-11-22 08:49:46 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("mode", ["plain", "rewrite"])
|
|
|
|
@pytest.mark.parametrize("plugin_state", ["development", "installed"])
|
2018-12-09 18:53:41 +08:00
|
|
|
def test_installed_plugin_rewrite(self, testdir, mode, plugin_state, monkeypatch):
|
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
2016-06-22 18:42:11 +08:00
|
|
|
# Make sure the hook is installed early enough so that plugins
|
2017-11-27 03:46:06 +08:00
|
|
|
# installed via setuptools are rewritten.
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("hampkg").ensure(dir=1)
|
2016-06-26 00:26:45 +08:00
|
|
|
contents = {
|
2018-05-23 22:48:46 +08:00
|
|
|
"hampkg/__init__.py": """
|
2016-06-26 00:26:45 +08:00
|
|
|
import pytest
|
2016-06-22 18:42:11 +08:00
|
|
|
|
2016-06-26 00:26:45 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def check_first2():
|
|
|
|
def check(values, value):
|
|
|
|
assert values.pop(0) == value
|
|
|
|
return check
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"spamplugin.py": """
|
2016-06-22 18:42:11 +08:00
|
|
|
import pytest
|
|
|
|
from hampkg import check_first2
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def check_first():
|
|
|
|
def check(values, value):
|
|
|
|
assert values.pop(0) == value
|
|
|
|
return check
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"mainwrapper.py": """
|
2016-06-22 18:42:11 +08:00
|
|
|
import pytest, pkg_resources
|
|
|
|
|
2016-09-15 08:59:18 +08:00
|
|
|
plugin_state = "{plugin_state}"
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class DummyDistInfo(object):
|
2016-06-22 18:42:11 +08:00
|
|
|
project_name = 'spam'
|
|
|
|
version = '1.0'
|
|
|
|
|
|
|
|
def _get_metadata(self, name):
|
2016-09-15 08:59:18 +08:00
|
|
|
# 'RECORD' meta-data only available in installed plugins
|
|
|
|
if name == 'RECORD' and plugin_state == "installed":
|
|
|
|
return ['spamplugin.py,sha256=abc,123',
|
|
|
|
'hampkg/__init__.py,sha256=abc,123']
|
|
|
|
# 'SOURCES.txt' meta-data only available for plugins in development mode
|
|
|
|
elif name == 'SOURCES.txt' and plugin_state == "development":
|
|
|
|
return ['spamplugin.py',
|
|
|
|
'hampkg/__init__.py']
|
|
|
|
return []
|
2016-06-22 18:42:11 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class DummyEntryPoint(object):
|
2016-06-22 18:42:11 +08:00
|
|
|
name = 'spam'
|
|
|
|
module_name = 'spam.py'
|
|
|
|
attrs = ()
|
|
|
|
extras = None
|
|
|
|
dist = DummyDistInfo()
|
|
|
|
|
|
|
|
def load(self, require=True, *args, **kwargs):
|
|
|
|
import spamplugin
|
|
|
|
return spamplugin
|
|
|
|
|
2019-02-23 05:49:56 +08:00
|
|
|
def iter_entry_points(group, name=None):
|
2016-06-22 18:42:11 +08:00
|
|
|
yield DummyEntryPoint()
|
|
|
|
|
|
|
|
pkg_resources.iter_entry_points = iter_entry_points
|
|
|
|
pytest.main()
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
plugin_state=plugin_state
|
|
|
|
),
|
|
|
|
"test_foo.py": """
|
2016-06-22 18:42:11 +08:00
|
|
|
def test(check_first):
|
|
|
|
check_first([10, 30], 30)
|
|
|
|
|
|
|
|
def test2(check_first2):
|
|
|
|
check_first([10, 30], 30)
|
|
|
|
""",
|
2016-06-26 00:26:45 +08:00
|
|
|
}
|
|
|
|
testdir.makepyfile(**contents)
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.run(
|
|
|
|
sys.executable, "mainwrapper.py", "-s", "--assert=%s" % mode
|
|
|
|
)
|
|
|
|
if mode == "plain":
|
|
|
|
expected = "E AssertionError"
|
|
|
|
elif mode == "rewrite":
|
|
|
|
expected = "*assert 10 == 30*"
|
2016-06-22 18:42:11 +08:00
|
|
|
else:
|
|
|
|
assert 0
|
|
|
|
result.stdout.fnmatch_lines([expected])
|
|
|
|
|
2016-06-26 00:26:45 +08:00
|
|
|
def test_rewrite_ast(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("pkg").ensure(dir=1)
|
2016-06-26 00:26:45 +08:00
|
|
|
contents = {
|
2018-05-23 22:48:46 +08:00
|
|
|
"pkg/__init__.py": """
|
2016-06-26 00:26:45 +08:00
|
|
|
import pytest
|
|
|
|
pytest.register_assert_rewrite('pkg.helper')
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"pkg/helper.py": """
|
2016-06-26 00:26:45 +08:00
|
|
|
def tool():
|
|
|
|
a, b = 2, 3
|
|
|
|
assert a == b
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"pkg/plugin.py": """
|
2016-06-26 00:26:45 +08:00
|
|
|
import pytest, pkg.helper
|
|
|
|
@pytest.fixture
|
|
|
|
def tool():
|
|
|
|
return pkg.helper.tool
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"pkg/other.py": """
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [3, 2]
|
2016-06-26 00:26:45 +08:00
|
|
|
def tool():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values.pop() == 3
|
2016-06-26 00:26:45 +08:00
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"conftest.py": """
|
2016-06-26 00:26:45 +08:00
|
|
|
pytest_plugins = ['pkg.plugin']
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"test_pkg.py": """
|
2016-06-26 00:26:45 +08:00
|
|
|
import pkg.other
|
|
|
|
def test_tool(tool):
|
|
|
|
tool()
|
|
|
|
def test_other():
|
|
|
|
pkg.other.tool()
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
testdir.makepyfile(**contents)
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest_subprocess("--assert=rewrite")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
">*assert a == b*",
|
|
|
|
"E*assert 2 == 3*",
|
|
|
|
">*assert values.pop() == 3*",
|
|
|
|
"E*AssertionError",
|
|
|
|
]
|
|
|
|
)
|
2016-06-26 00:26:45 +08:00
|
|
|
|
2016-08-31 09:15:53 +08:00
|
|
|
def test_register_assert_rewrite_checks_types(self):
|
|
|
|
with pytest.raises(TypeError):
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest.register_assert_rewrite(["pytest_tests_internal_non_existing"])
|
|
|
|
pytest.register_assert_rewrite(
|
|
|
|
"pytest_tests_internal_non_existing", "pytest_tests_internal_non_existing2"
|
|
|
|
)
|
2016-08-31 09:15:53 +08:00
|
|
|
|
2016-06-22 18:42:11 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestBinReprIntegration(object):
|
2012-06-25 23:35:33 +08:00
|
|
|
def test_pytest_assertrepr_compare_called(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-06-25 23:35:33 +08:00
|
|
|
def pytest_assertrepr_compare(op, left, right):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append((op, left, right))
|
2016-07-12 09:03:53 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def list(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
return values
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-06-25 23:35:33 +08:00
|
|
|
def test_hello():
|
|
|
|
assert 0 == 1
|
2016-07-12 09:03:53 +08:00
|
|
|
def test_check(list):
|
|
|
|
assert list == [("==", 0, 1)]
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-06-25 23:35:33 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_hello*FAIL*", "*test_check*PASS*"])
|
2010-10-03 00:47:39 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def callequal(left, right, verbose=False):
|
|
|
|
config = mock_config()
|
|
|
|
config.verbose = verbose
|
2018-05-23 22:48:46 +08:00
|
|
|
return plugin.pytest_assertrepr_compare(config, "==", left, right)
|
2013-03-28 09:39:01 +08:00
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestAssert_reprcompare(object):
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_different_types(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert callequal([0, 1], "foo") is None
|
2010-10-03 00:47:39 +08:00
|
|
|
|
|
|
|
def test_summary(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
summary = callequal([0, 1], [0, 2])[0]
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(summary) < 65
|
|
|
|
|
|
|
|
def test_text_diff(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
diff = callequal("spam", "eggs")[1:]
|
|
|
|
assert "- spam" in diff
|
|
|
|
assert "+ eggs" in diff
|
2010-10-03 00:47:39 +08:00
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def test_text_skipping(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
lines = callequal("a" * 50 + "spam", "a" * 50 + "eggs")
|
|
|
|
assert "Skipping" in lines[1]
|
2013-03-28 09:39:01 +08:00
|
|
|
for line in lines:
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "a" * 50 not in line
|
2013-03-28 09:39:01 +08:00
|
|
|
|
|
|
|
def test_text_skipping_verbose(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
lines = callequal("a" * 50 + "spam", "a" * 50 + "eggs", verbose=True)
|
|
|
|
assert "- " + "a" * 50 + "spam" in lines
|
|
|
|
assert "+ " + "a" * 50 + "eggs" in lines
|
2013-03-28 09:39:01 +08:00
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_multiline_text_diff(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
left = "foo\nspam\nbar"
|
|
|
|
right = "foo\neggs\nbar"
|
2010-10-05 00:49:30 +08:00
|
|
|
diff = callequal(left, right)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "- spam" in diff
|
|
|
|
assert "+ eggs" in diff
|
2010-09-09 05:21:52 +08:00
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_list(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal([0, 1], [0, 2])
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2014-09-27 09:29:47 +08:00
|
|
|
@pytest.mark.parametrize(
|
2018-05-23 22:48:46 +08:00
|
|
|
["left", "right", "expected"],
|
|
|
|
[
|
|
|
|
(
|
|
|
|
[0, 1],
|
|
|
|
[0, 2],
|
|
|
|
"""
|
2014-09-27 09:29:47 +08:00
|
|
|
Full diff:
|
|
|
|
- [0, 1]
|
|
|
|
? ^
|
|
|
|
+ [0, 2]
|
|
|
|
? ^
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{0: 1},
|
|
|
|
{0: 2},
|
|
|
|
"""
|
2014-09-27 09:29:47 +08:00
|
|
|
Full diff:
|
|
|
|
- {0: 1}
|
|
|
|
? ^
|
|
|
|
+ {0: 2}
|
|
|
|
? ^
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{0, 1},
|
|
|
|
{0, 2},
|
|
|
|
"""
|
2014-09-27 09:29:47 +08:00
|
|
|
Full diff:
|
|
|
|
- set([0, 1])
|
|
|
|
? ^
|
|
|
|
+ set([0, 2])
|
|
|
|
? ^
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
if not PY3
|
|
|
|
else """
|
2014-09-27 09:29:47 +08:00
|
|
|
Full diff:
|
|
|
|
- {0, 1}
|
|
|
|
? ^
|
|
|
|
+ {0, 2}
|
|
|
|
? ^
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
),
|
|
|
|
],
|
2014-09-27 09:29:47 +08:00
|
|
|
)
|
|
|
|
def test_iterable_full_diff(self, left, right, expected):
|
|
|
|
"""Test the full diff assertion failure explanation.
|
|
|
|
|
|
|
|
When verbose is False, then just a -v notice to get the diff is rendered,
|
|
|
|
when verbose is True, then ndiff of the pprint is returned.
|
|
|
|
"""
|
|
|
|
expl = callequal(left, right, verbose=False)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert expl[-1] == "Use -v to get the full diff"
|
|
|
|
expl = "\n".join(callequal(left, right, verbose=True))
|
2014-09-27 09:29:47 +08:00
|
|
|
assert expl.endswith(textwrap.dedent(expected).strip())
|
|
|
|
|
2017-02-15 23:00:18 +08:00
|
|
|
def test_list_different_lengths(self):
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal([0, 1], [0, 1, 2])
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal([0, 1, 2], [0, 1])
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_dict(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = callequal({"a": 0}, {"a": 1})
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2013-08-01 20:48:34 +08:00
|
|
|
def test_dict_omitting(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1})
|
|
|
|
assert lines[1].startswith("Omitting 1 identical item")
|
|
|
|
assert "Common items" not in lines
|
2013-08-01 20:48:34 +08:00
|
|
|
for line in lines[1:]:
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "b" not in line
|
2013-08-01 20:48:34 +08:00
|
|
|
|
2016-09-19 19:17:26 +08:00
|
|
|
def test_dict_omitting_with_verbosity_1(self):
|
|
|
|
""" Ensure differing items are visible for verbosity=1 (#1512) """
|
2018-05-23 22:48:46 +08:00
|
|
|
lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1}, verbose=1)
|
|
|
|
assert lines[1].startswith("Omitting 1 identical item")
|
|
|
|
assert lines[2].startswith("Differing items")
|
2016-09-19 19:17:26 +08:00
|
|
|
assert lines[3] == "{'a': 0} != {'a': 1}"
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "Common items" not in lines
|
2016-09-19 19:17:26 +08:00
|
|
|
|
|
|
|
def test_dict_omitting_with_verbosity_2(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1}, verbose=2)
|
|
|
|
assert lines[1].startswith("Common items:")
|
|
|
|
assert "Omitting" not in lines[1]
|
2013-08-01 20:48:34 +08:00
|
|
|
assert lines[2] == "{'b': 1}"
|
|
|
|
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_set(self):
|
2018-05-18 05:31:16 +08:00
|
|
|
expl = callequal({0, 1}, {0, 2})
|
2010-10-05 00:49:30 +08:00
|
|
|
assert len(expl) > 1
|
|
|
|
|
2013-04-29 03:59:10 +08:00
|
|
|
def test_frozenzet(self):
|
2018-05-18 05:31:16 +08:00
|
|
|
expl = callequal(frozenset([0, 1]), {0, 2})
|
2013-08-01 20:48:34 +08:00
|
|
|
assert len(expl) > 1
|
|
|
|
|
|
|
|
def test_Sequence(self):
|
2019-01-21 03:59:48 +08:00
|
|
|
if sys.version_info >= (3, 3):
|
|
|
|
import collections.abc as collections_abc
|
|
|
|
else:
|
|
|
|
import collections as collections_abc
|
|
|
|
if not hasattr(collections_abc, "MutableSequence"):
|
2013-08-01 21:38:03 +08:00
|
|
|
pytest.skip("cannot import MutableSequence")
|
2019-01-21 03:59:48 +08:00
|
|
|
MutableSequence = collections_abc.MutableSequence
|
2013-08-01 21:38:03 +08:00
|
|
|
|
2013-08-01 20:48:34 +08:00
|
|
|
class TestSequence(MutableSequence): # works with a Sequence subclass
|
|
|
|
def __init__(self, iterable):
|
|
|
|
self.elements = list(iterable)
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.elements[item]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.elements)
|
|
|
|
|
|
|
|
def __setitem__(self, item, value):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __delitem__(self, item):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def insert(self, item, index):
|
|
|
|
pass
|
|
|
|
|
|
|
|
expl = callequal(TestSequence([0, 1]), list([0, 2]))
|
2013-04-29 03:59:10 +08:00
|
|
|
assert len(expl) > 1
|
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
def test_list_tuples(self):
|
2017-07-17 07:25:08 +08:00
|
|
|
expl = callequal([], [(1, 2)])
|
2010-10-03 00:47:39 +08:00
|
|
|
assert len(expl) > 1
|
2017-07-17 07:25:08 +08:00
|
|
|
expl = callequal([(1, 2)], [])
|
2010-10-05 00:49:30 +08:00
|
|
|
assert len(expl) > 1
|
|
|
|
|
2019-01-08 09:25:05 +08:00
|
|
|
def test_repr_verbose(self):
|
|
|
|
class Nums:
|
|
|
|
def __init__(self, nums):
|
|
|
|
self.nums = nums
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self.nums)
|
|
|
|
|
|
|
|
list_x = list(range(5000))
|
|
|
|
list_y = list(range(5000))
|
|
|
|
list_y[len(list_y) // 2] = 3
|
|
|
|
nums_x = Nums(list_x)
|
|
|
|
nums_y = Nums(list_y)
|
|
|
|
|
|
|
|
assert callequal(nums_x, nums_y) is None
|
|
|
|
|
|
|
|
expl = callequal(nums_x, nums_y, verbose=1)
|
|
|
|
assert "-" + repr(nums_x) in expl
|
|
|
|
assert "+" + repr(nums_y) in expl
|
|
|
|
|
|
|
|
expl = callequal(nums_x, nums_y, verbose=2)
|
|
|
|
assert "-" + repr(nums_x) in expl
|
|
|
|
assert "+" + repr(nums_y) in expl
|
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
def test_list_bad_repr(self):
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2010-10-05 00:49:30 +08:00
|
|
|
def __repr__(self):
|
|
|
|
raise ValueError(42)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
expl = callequal([], [A()])
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "ValueError" in "".join(expl)
|
|
|
|
expl = callequal({}, {"1": A()})
|
|
|
|
assert "faulty" in "".join(expl)
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2010-10-09 13:35:28 +08:00
|
|
|
def test_one_repr_empty(self):
|
|
|
|
"""
|
|
|
|
the faulty empty string repr did trigger
|
2018-05-13 18:06:09 +08:00
|
|
|
an unbound local error in _diff_text
|
2010-10-09 13:35:28 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-10-09 13:35:28 +08:00
|
|
|
class A(str):
|
|
|
|
def __repr__(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
return ""
|
|
|
|
|
|
|
|
expl = callequal(A(), "")
|
2010-10-09 13:35:28 +08:00
|
|
|
assert not expl
|
|
|
|
|
2011-02-16 07:24:18 +08:00
|
|
|
def test_repr_no_exc(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = " ".join(callequal("foo", "bar"))
|
|
|
|
assert "raised in repr()" not in expl
|
2011-02-16 07:24:18 +08:00
|
|
|
|
2013-11-29 08:29:14 +08:00
|
|
|
def test_unicode(self):
|
2018-08-23 10:21:00 +08:00
|
|
|
left = u"£€"
|
|
|
|
right = u"£"
|
2013-11-29 08:29:14 +08:00
|
|
|
expl = callequal(left, right)
|
2018-08-23 10:21:00 +08:00
|
|
|
assert expl[0] == u"'£€' == '£'"
|
|
|
|
assert expl[1] == u"- £€"
|
|
|
|
assert expl[2] == u"+ £"
|
2013-11-29 08:29:14 +08:00
|
|
|
|
2015-07-25 16:16:05 +08:00
|
|
|
def test_nonascii_text(self):
|
|
|
|
"""
|
|
|
|
:issue: 877
|
|
|
|
non ascii python2 str caused a UnicodeDecodeError
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-07-25 16:16:05 +08:00
|
|
|
class A(str):
|
|
|
|
def __repr__(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
return "\xff"
|
|
|
|
|
|
|
|
expl = callequal(A(), "1")
|
2015-07-25 16:16:05 +08:00
|
|
|
assert expl
|
|
|
|
|
2016-02-12 22:54:36 +08:00
|
|
|
def test_format_nonascii_explanation(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert util.format_explanation("λ")
|
2016-02-12 22:54:36 +08:00
|
|
|
|
2014-01-29 08:42:58 +08:00
|
|
|
def test_mojibake(self):
|
|
|
|
# issue 429
|
2018-11-05 09:43:24 +08:00
|
|
|
left = b"e"
|
|
|
|
right = b"\xc3\xa9"
|
2014-01-29 08:42:58 +08:00
|
|
|
expl = callequal(left, right)
|
|
|
|
for line in expl:
|
2018-08-23 10:26:11 +08:00
|
|
|
assert isinstance(line, six.text_type)
|
2018-08-23 10:21:00 +08:00
|
|
|
msg = u"\n".join(expl)
|
2014-01-29 08:42:58 +08:00
|
|
|
assert msg
|
|
|
|
|
2013-11-29 08:29:14 +08:00
|
|
|
|
2018-08-03 06:16:14 +08:00
|
|
|
class TestAssert_reprcompare_dataclass(object):
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
|
2018-11-13 05:36:16 +08:00
|
|
|
def test_dataclasses(self, testdir):
|
|
|
|
p = testdir.copy_example("dataclasses/test_compare_dataclasses.py")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.assert_outcomes(failed=1, passed=0)
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*Omitting 1 identical items, use -vv to show*",
|
|
|
|
"*Differing attributes:*",
|
2018-11-13 23:37:02 +08:00
|
|
|
"*field_b: 'b' != 'c'*",
|
2018-11-13 05:36:16 +08:00
|
|
|
]
|
|
|
|
)
|
2018-08-03 06:16:14 +08:00
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
|
2018-11-13 05:36:16 +08:00
|
|
|
def test_dataclasses_verbose(self, testdir):
|
|
|
|
p = testdir.copy_example("dataclasses/test_compare_dataclasses_verbose.py")
|
|
|
|
result = testdir.runpytest(p, "-vv")
|
|
|
|
result.assert_outcomes(failed=1, passed=0)
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2018-11-13 23:37:02 +08:00
|
|
|
"*Matching attributes:*",
|
2018-11-13 05:36:16 +08:00
|
|
|
"*['field_a']*",
|
|
|
|
"*Differing attributes:*",
|
2018-11-13 23:37:02 +08:00
|
|
|
"*field_b: 'b' != 'c'*",
|
2018-11-13 05:36:16 +08:00
|
|
|
]
|
|
|
|
)
|
2018-08-03 06:16:14 +08:00
|
|
|
|
2018-08-03 07:22:15 +08:00
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
|
2018-11-13 05:36:16 +08:00
|
|
|
def test_dataclasses_with_attribute_comparison_off(self, testdir):
|
|
|
|
p = testdir.copy_example(
|
|
|
|
"dataclasses/test_compare_dataclasses_field_comparison_off.py"
|
|
|
|
)
|
|
|
|
result = testdir.runpytest(p, "-vv")
|
|
|
|
result.assert_outcomes(failed=0, passed=1)
|
2018-08-03 06:16:14 +08:00
|
|
|
|
2018-08-03 07:22:15 +08:00
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
|
2018-11-13 05:36:16 +08:00
|
|
|
def test_comparing_two_different_data_classes(self, testdir):
|
|
|
|
p = testdir.copy_example(
|
|
|
|
"dataclasses/test_compare_two_different_dataclasses.py"
|
|
|
|
)
|
|
|
|
result = testdir.runpytest(p, "-vv")
|
|
|
|
result.assert_outcomes(failed=0, passed=1)
|
2018-08-03 06:16:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestAssert_reprcompare_attrsclass(object):
|
|
|
|
def test_attrs(self):
|
|
|
|
@attr.s
|
2018-08-04 00:29:45 +08:00
|
|
|
class SimpleDataObject(object):
|
2018-08-03 06:16:14 +08:00
|
|
|
field_a = attr.ib()
|
|
|
|
field_b = attr.ib()
|
|
|
|
|
|
|
|
left = SimpleDataObject(1, "b")
|
|
|
|
right = SimpleDataObject(1, "c")
|
|
|
|
|
|
|
|
lines = callequal(left, right)
|
|
|
|
assert lines[1].startswith("Omitting 1 identical item")
|
2018-11-13 23:37:02 +08:00
|
|
|
assert "Matching attributes" not in lines
|
2018-08-03 06:16:14 +08:00
|
|
|
for line in lines[1:]:
|
|
|
|
assert "field_a" not in line
|
|
|
|
|
|
|
|
def test_attrs_verbose(self):
|
|
|
|
@attr.s
|
2018-08-04 00:29:45 +08:00
|
|
|
class SimpleDataObject(object):
|
2018-08-03 06:16:14 +08:00
|
|
|
field_a = attr.ib()
|
|
|
|
field_b = attr.ib()
|
|
|
|
|
|
|
|
left = SimpleDataObject(1, "b")
|
|
|
|
right = SimpleDataObject(1, "c")
|
|
|
|
|
|
|
|
lines = callequal(left, right, verbose=2)
|
2018-11-13 23:37:02 +08:00
|
|
|
assert lines[1].startswith("Matching attributes:")
|
2018-08-03 06:16:14 +08:00
|
|
|
assert "Omitting" not in lines[1]
|
|
|
|
assert lines[2] == "['field_a']"
|
|
|
|
|
|
|
|
def test_attrs_with_attribute_comparison_off(self):
|
|
|
|
@attr.s
|
2018-08-04 00:29:45 +08:00
|
|
|
class SimpleDataObject(object):
|
2018-08-03 06:16:14 +08:00
|
|
|
field_a = attr.ib()
|
|
|
|
field_b = attr.ib(cmp=False)
|
|
|
|
|
|
|
|
left = SimpleDataObject(1, "b")
|
|
|
|
right = SimpleDataObject(1, "b")
|
|
|
|
|
|
|
|
lines = callequal(left, right, verbose=2)
|
2018-11-13 23:37:02 +08:00
|
|
|
assert lines[1].startswith("Matching attributes:")
|
2018-08-03 06:16:14 +08:00
|
|
|
assert "Omitting" not in lines[1]
|
|
|
|
assert lines[2] == "['field_a']"
|
|
|
|
for line in lines[2:]:
|
|
|
|
assert "field_b" not in line
|
|
|
|
|
2018-08-03 22:23:50 +08:00
|
|
|
def test_comparing_two_different_attrs_classes(self):
|
2018-08-03 06:16:14 +08:00
|
|
|
@attr.s
|
2018-08-04 00:29:45 +08:00
|
|
|
class SimpleDataObjectOne(object):
|
2018-08-03 06:16:14 +08:00
|
|
|
field_a = attr.ib()
|
|
|
|
field_b = attr.ib()
|
|
|
|
|
|
|
|
@attr.s
|
2018-08-04 00:29:45 +08:00
|
|
|
class SimpleDataObjectTwo(object):
|
2018-08-03 06:16:14 +08:00
|
|
|
field_a = attr.ib()
|
|
|
|
field_b = attr.ib()
|
|
|
|
|
|
|
|
left = SimpleDataObjectOne(1, "b")
|
|
|
|
right = SimpleDataObjectTwo(1, "c")
|
|
|
|
|
|
|
|
lines = callequal(left, right)
|
|
|
|
assert lines is None
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFormatExplanation(object):
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_special_chars_full(self, testdir):
|
2014-04-03 00:35:22 +08:00
|
|
|
# Issue 453, for the bug this would raise IndexError
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2014-04-03 00:35:22 +08:00
|
|
|
def test_foo():
|
2014-06-01 05:51:05 +08:00
|
|
|
assert '\\n}' == ''
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-04-03 00:35:22 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*AssertionError*"])
|
2014-04-03 00:35:22 +08:00
|
|
|
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_fmt_simple(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = "assert foo"
|
|
|
|
assert util.format_explanation(expl) == "assert foo"
|
2014-08-24 00:14:25 +08:00
|
|
|
|
|
|
|
def test_fmt_where(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = "\n".join(["assert 1", "{1 = foo", "} == 2"])
|
|
|
|
res = "\n".join(["assert 1 == 2", " + where 1 = foo"])
|
2014-08-24 00:14:25 +08:00
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_and(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = "\n".join(["assert 1", "{1 = foo", "} == 2", "{2 = bar", "}"])
|
|
|
|
res = "\n".join(["assert 1 == 2", " + where 1 = foo", " + and 2 = bar"])
|
2014-08-24 00:14:25 +08:00
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_where_nested(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = "\n".join(["assert 1", "{1 = foo", "{foo = bar", "}", "} == 2"])
|
|
|
|
res = "\n".join(["assert 1 == 2", " + where 1 = foo", " + where foo = bar"])
|
2014-08-24 00:14:25 +08:00
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_newline(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = "\n".join(['assert "foo" == "bar"', "~- foo", "~+ bar"])
|
|
|
|
res = "\n".join(['assert "foo" == "bar"', " - foo", " + bar"])
|
2014-08-24 00:14:25 +08:00
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_newline_escaped(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = "\n".join(["assert foo == bar", "baz"])
|
|
|
|
res = "assert foo == bar\\nbaz"
|
2014-08-24 00:14:25 +08:00
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_newline_before_where(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = "\n".join(
|
|
|
|
[
|
|
|
|
"the assertion message here",
|
|
|
|
">assert 1",
|
|
|
|
"{1 = foo",
|
|
|
|
"} == 2",
|
|
|
|
"{2 = bar",
|
|
|
|
"}",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
res = "\n".join(
|
|
|
|
[
|
|
|
|
"the assertion message here",
|
|
|
|
"assert 1 == 2",
|
|
|
|
" + where 1 = foo",
|
|
|
|
" + and 2 = bar",
|
|
|
|
]
|
|
|
|
)
|
2014-08-24 00:14:25 +08:00
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
|
|
|
def test_fmt_multi_newline_before_where(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = "\n".join(
|
|
|
|
[
|
|
|
|
"the assertion",
|
|
|
|
"~message here",
|
|
|
|
">assert 1",
|
|
|
|
"{1 = foo",
|
|
|
|
"} == 2",
|
|
|
|
"{2 = bar",
|
|
|
|
"}",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
res = "\n".join(
|
|
|
|
[
|
|
|
|
"the assertion",
|
|
|
|
" message here",
|
|
|
|
"assert 1 == 2",
|
|
|
|
" + where 1 = foo",
|
|
|
|
" + and 2 = bar",
|
|
|
|
]
|
|
|
|
)
|
2014-08-24 00:14:25 +08:00
|
|
|
assert util.format_explanation(expl) == res
|
|
|
|
|
2014-04-03 00:35:22 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestTruncateExplanation(object):
|
2016-09-22 07:06:45 +08:00
|
|
|
|
|
|
|
""" Confirm assertion output is truncated as expected """
|
|
|
|
|
|
|
|
# The number of lines in the truncation explanation message. Used
|
|
|
|
# to calculate that results have the expected length.
|
2016-10-11 07:17:15 +08:00
|
|
|
LINES_IN_TRUNCATION_MSG = 2
|
2016-09-22 07:06:45 +08:00
|
|
|
|
|
|
|
def test_doesnt_truncate_when_input_is_empty_list(self):
|
|
|
|
expl = []
|
|
|
|
result = truncate._truncate_explanation(expl, max_lines=8, max_chars=100)
|
|
|
|
assert result == expl
|
|
|
|
|
|
|
|
def test_doesnt_truncate_at_when_input_is_5_lines_and_LT_max_chars(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = ["a" * 100 for x in range(5)]
|
2017-07-17 07:25:08 +08:00
|
|
|
result = truncate._truncate_explanation(expl, max_lines=8, max_chars=8 * 80)
|
2016-09-22 07:06:45 +08:00
|
|
|
assert result == expl
|
|
|
|
|
|
|
|
def test_truncates_at_8_lines_when_given_list_of_empty_strings(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = ["" for x in range(50)]
|
2016-09-22 07:06:45 +08:00
|
|
|
result = truncate._truncate_explanation(expl, max_lines=8, max_chars=100)
|
|
|
|
assert result != expl
|
|
|
|
assert len(result) == 8 + self.LINES_IN_TRUNCATION_MSG
|
|
|
|
assert "Full output truncated" in result[-1]
|
|
|
|
assert "43 lines hidden" in result[-1]
|
2018-05-23 22:48:46 +08:00
|
|
|
last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1]
|
2016-09-22 07:06:45 +08:00
|
|
|
assert last_line_before_trunc_msg.endswith("...")
|
|
|
|
|
|
|
|
def test_truncates_at_8_lines_when_first_8_lines_are_LT_max_chars(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = ["a" for x in range(100)]
|
2017-07-17 07:25:08 +08:00
|
|
|
result = truncate._truncate_explanation(expl, max_lines=8, max_chars=8 * 80)
|
2016-09-22 07:06:45 +08:00
|
|
|
assert result != expl
|
|
|
|
assert len(result) == 8 + self.LINES_IN_TRUNCATION_MSG
|
|
|
|
assert "Full output truncated" in result[-1]
|
|
|
|
assert "93 lines hidden" in result[-1]
|
2018-05-23 22:48:46 +08:00
|
|
|
last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1]
|
2016-09-22 07:06:45 +08:00
|
|
|
assert last_line_before_trunc_msg.endswith("...")
|
|
|
|
|
|
|
|
def test_truncates_at_8_lines_when_first_8_lines_are_EQ_max_chars(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = ["a" * 80 for x in range(16)]
|
2017-07-17 07:25:08 +08:00
|
|
|
result = truncate._truncate_explanation(expl, max_lines=8, max_chars=8 * 80)
|
2016-09-22 07:06:45 +08:00
|
|
|
assert result != expl
|
|
|
|
assert len(result) == 8 + self.LINES_IN_TRUNCATION_MSG
|
|
|
|
assert "Full output truncated" in result[-1]
|
|
|
|
assert "9 lines hidden" in result[-1]
|
2018-05-23 22:48:46 +08:00
|
|
|
last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1]
|
2016-09-22 07:06:45 +08:00
|
|
|
assert last_line_before_trunc_msg.endswith("...")
|
|
|
|
|
|
|
|
def test_truncates_at_4_lines_when_first_4_lines_are_GT_max_chars(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = ["a" * 250 for x in range(10)]
|
2016-09-22 07:06:45 +08:00
|
|
|
result = truncate._truncate_explanation(expl, max_lines=8, max_chars=999)
|
|
|
|
assert result != expl
|
|
|
|
assert len(result) == 4 + self.LINES_IN_TRUNCATION_MSG
|
|
|
|
assert "Full output truncated" in result[-1]
|
|
|
|
assert "7 lines hidden" in result[-1]
|
2018-05-23 22:48:46 +08:00
|
|
|
last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1]
|
2016-09-22 07:06:45 +08:00
|
|
|
assert last_line_before_trunc_msg.endswith("...")
|
|
|
|
|
|
|
|
def test_truncates_at_1_line_when_first_line_is_GT_max_chars(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
expl = ["a" * 250 for x in range(1000)]
|
2016-09-22 07:06:45 +08:00
|
|
|
result = truncate._truncate_explanation(expl, max_lines=8, max_chars=100)
|
|
|
|
assert result != expl
|
|
|
|
assert len(result) == 1 + self.LINES_IN_TRUNCATION_MSG
|
|
|
|
assert "Full output truncated" in result[-1]
|
|
|
|
assert "1000 lines hidden" in result[-1]
|
2018-05-23 22:48:46 +08:00
|
|
|
last_line_before_trunc_msg = result[-self.LINES_IN_TRUNCATION_MSG - 1]
|
2016-09-22 07:06:45 +08:00
|
|
|
assert last_line_before_trunc_msg.endswith("...")
|
|
|
|
|
|
|
|
def test_full_output_truncated(self, monkeypatch, testdir):
|
|
|
|
""" Test against full runpytest() output. """
|
|
|
|
|
|
|
|
line_count = 7
|
|
|
|
line_len = 100
|
|
|
|
expected_truncated_lines = 2
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
r"""
|
2016-09-22 07:06:45 +08:00
|
|
|
def test_many_lines():
|
|
|
|
a = list([str(i)[0] * %d for i in range(%d)])
|
|
|
|
b = a[::2]
|
|
|
|
a = '\n'.join(map(str, a))
|
|
|
|
b = '\n'.join(map(str, b))
|
|
|
|
assert a == b
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
% (line_len, line_count)
|
|
|
|
)
|
|
|
|
monkeypatch.delenv("CI", raising=False)
|
2016-09-22 07:06:45 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest()
|
|
|
|
# without -vv, truncate the message showing a few diff lines only
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*- 1*",
|
|
|
|
"*- 3*",
|
|
|
|
"*- 5*",
|
|
|
|
"*truncated (%d lines hidden)*use*-vv*" % expected_truncated_lines,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
result = testdir.runpytest("-vv")
|
|
|
|
result.stdout.fnmatch_lines(["* 6*"])
|
|
|
|
|
|
|
|
monkeypatch.setenv("CI", "1")
|
2016-09-22 07:06:45 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 6*"])
|
2016-09-22 07:06:45 +08:00
|
|
|
|
|
|
|
|
2013-02-13 05:43:33 +08:00
|
|
|
def test_python25_compile_issue257(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-02-13 05:43:33 +08:00
|
|
|
def test_rewritten():
|
|
|
|
assert 1 == 2
|
|
|
|
# some comment
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-02-13 05:43:33 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2013-02-13 05:43:33 +08:00
|
|
|
*E*assert 1 == 2*
|
|
|
|
*1 failed*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-02-13 05:43:33 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-07-04 08:28:48 +08:00
|
|
|
def test_rewritten(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2011-07-04 08:28:48 +08:00
|
|
|
def test_rewritten():
|
|
|
|
assert "@py_builtins" in globals()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2011-07-04 08:28:48 +08:00
|
|
|
assert testdir.runpytest().ret == 0
|
2011-05-27 09:06:11 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-07-22 22:10:44 +08:00
|
|
|
def test_reprcompare_notin():
|
|
|
|
config = mock_config()
|
|
|
|
detail = plugin.pytest_assertrepr_compare(config, "not in", "foo", "aaafoobbb")[1:]
|
2018-05-23 22:48:46 +08:00
|
|
|
assert detail == ["'foo' is contained here:", " aaafoobbb", "? +++"]
|
2010-12-10 09:03:26 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-07-22 22:10:44 +08:00
|
|
|
def test_reprcompare_whitespaces():
|
|
|
|
config = mock_config()
|
|
|
|
detail = plugin.pytest_assertrepr_compare(config, "==", "\r\n", "\n")
|
2018-06-26 21:35:27 +08:00
|
|
|
assert detail == [
|
|
|
|
r"'\r\n' == '\n'",
|
|
|
|
r"Strings contain only whitespace, escaping them using repr()",
|
|
|
|
r"- '\r\n'",
|
|
|
|
r"? --",
|
|
|
|
r"+ '\n'",
|
|
|
|
]
|
2018-05-04 06:51:41 +08:00
|
|
|
|
|
|
|
|
2010-10-03 01:00:47 +08:00
|
|
|
def test_pytest_assertrepr_compare_integration(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2010-10-03 00:47:39 +08:00
|
|
|
def test_hello():
|
|
|
|
x = set(range(100))
|
|
|
|
y = x.copy()
|
|
|
|
y.remove(50)
|
|
|
|
assert x == y
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-10-03 00:47:39 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*def test_hello():*", "*assert x == y*", "*E*Extra items*left*", "*E*50*"]
|
|
|
|
)
|
2010-09-18 20:03:28 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
def test_sequence_comparison_uses_repr(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2010-10-05 00:49:30 +08:00
|
|
|
def test_hello():
|
|
|
|
x = set("hello x")
|
|
|
|
y = set("hello y")
|
|
|
|
assert x == y
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-10-05 00:49:30 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*def test_hello():*",
|
|
|
|
"*assert x == y*",
|
|
|
|
"*E*Extra items*left*",
|
|
|
|
"*E*'x'*",
|
|
|
|
"*E*Extra items*right*",
|
|
|
|
"*E*'y'*",
|
|
|
|
]
|
|
|
|
)
|
2010-10-05 00:49:30 +08:00
|
|
|
|
2012-06-27 23:26:55 +08:00
|
|
|
|
2011-10-16 18:51:15 +08:00
|
|
|
def test_assertrepr_loaded_per_dir(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(test_base=["def test_base(): assert 1 == 2"])
|
|
|
|
a = testdir.mkdir("a")
|
|
|
|
a_test = a.join("test_a.py")
|
|
|
|
a_test.write("def test_a(): assert 1 == 2")
|
|
|
|
a_conftest = a.join("conftest.py")
|
2011-10-16 18:51:15 +08:00
|
|
|
a_conftest.write('def pytest_assertrepr_compare(): return ["summary a"]')
|
2018-05-23 22:48:46 +08:00
|
|
|
b = testdir.mkdir("b")
|
|
|
|
b_test = b.join("test_b.py")
|
|
|
|
b_test.write("def test_b(): assert 1 == 2")
|
|
|
|
b_conftest = b.join("conftest.py")
|
2011-10-16 18:51:15 +08:00
|
|
|
b_conftest.write('def pytest_assertrepr_compare(): return ["summary b"]')
|
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*def test_base():*",
|
|
|
|
"*E*assert 1 == 2*",
|
|
|
|
"*def test_a():*",
|
|
|
|
"*E*assert summary a*",
|
|
|
|
"*def test_b():*",
|
|
|
|
"*E*assert summary b*",
|
|
|
|
]
|
|
|
|
)
|
2011-10-16 18:51:15 +08:00
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
|
2011-05-27 03:34:27 +08:00
|
|
|
def test_assertion_options(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_hello():
|
|
|
|
x = 3
|
|
|
|
assert x == 4
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-28 17:54:46 +08:00
|
|
|
result = testdir.runpytest()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert "3 == 4" in result.stdout.str()
|
2016-06-25 17:27:10 +08:00
|
|
|
result = testdir.runpytest_subprocess("--assert=plain")
|
|
|
|
assert "3 == 4" not in result.stdout.str()
|
2011-05-27 03:34:27 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-07-29 18:55:39 +08:00
|
|
|
def test_triple_quoted_string_issue113(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2010-07-29 18:55:39 +08:00
|
|
|
def test_hello():
|
|
|
|
assert "" == '''
|
2018-05-23 22:48:46 +08:00
|
|
|
'''"""
|
|
|
|
)
|
2010-07-29 18:55:39 +08:00
|
|
|
result = testdir.runpytest("--fulltrace")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed*"])
|
|
|
|
assert "SyntaxError" not in result.stdout.str()
|
2010-07-29 18:55:39 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_traceback_failure(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
p1 = testdir.makepyfile(
|
|
|
|
"""
|
2009-09-06 22:59:39 +08:00
|
|
|
def g():
|
|
|
|
return 2
|
|
|
|
def f(x):
|
|
|
|
assert x == g()
|
|
|
|
def test_onefails():
|
|
|
|
f(3)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-06-29 19:32:53 +08:00
|
|
|
result = testdir.runpytest(p1, "--tb=long")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_traceback_failure.py F*",
|
|
|
|
"====* FAILURES *====",
|
|
|
|
"____*____",
|
|
|
|
"",
|
|
|
|
" def test_onefails():",
|
|
|
|
"> f(3)",
|
|
|
|
"",
|
|
|
|
"*test_*.py:6: ",
|
|
|
|
"_ _ _ *",
|
|
|
|
# "",
|
|
|
|
" def f(x):",
|
|
|
|
"> assert x == g()",
|
|
|
|
"E assert 3 == 2",
|
|
|
|
"E + where 2 = g()",
|
|
|
|
"",
|
|
|
|
"*test_traceback_failure.py:4: AssertionError",
|
|
|
|
]
|
|
|
|
)
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
result = testdir.runpytest(p1) # "auto"
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_traceback_failure.py F*",
|
|
|
|
"====* FAILURES *====",
|
|
|
|
"____*____",
|
|
|
|
"",
|
|
|
|
" def test_onefails():",
|
|
|
|
"> f(3)",
|
|
|
|
"",
|
|
|
|
"*test_*.py:6: ",
|
|
|
|
"",
|
|
|
|
" def f(x):",
|
|
|
|
"> assert x == g()",
|
|
|
|
"E assert 3 == 2",
|
|
|
|
"E + where 2 = g()",
|
|
|
|
"",
|
|
|
|
"*test_traceback_failure.py:4: AssertionError",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info[:2] <= (3, 3),
|
|
|
|
reason="Python 3.4+ shows chained exceptions on multiprocess",
|
|
|
|
)
|
2016-10-28 07:15:05 +08:00
|
|
|
def test_exception_handling_no_traceback(testdir):
|
|
|
|
"""
|
|
|
|
Handle chain exceptions in tasks submitted by the multiprocess module (#1984).
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
p1 = testdir.makepyfile(
|
|
|
|
"""
|
2016-10-28 07:15:05 +08:00
|
|
|
from multiprocessing import Pool
|
|
|
|
|
|
|
|
def process_task(n):
|
|
|
|
assert n == 10
|
|
|
|
|
|
|
|
def multitask_job():
|
|
|
|
tasks = [1]
|
|
|
|
with Pool(processes=1) as pool:
|
|
|
|
pool.map(process_task, tasks)
|
|
|
|
|
|
|
|
def test_multitask_job():
|
|
|
|
multitask_job()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-10-28 07:15:05 +08:00
|
|
|
result = testdir.runpytest(p1, "--tb=long")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"====* FAILURES *====",
|
|
|
|
"*multiprocessing.pool.RemoteTraceback:*",
|
|
|
|
"Traceback (most recent call last):",
|
|
|
|
"*assert n == 10",
|
|
|
|
"The above exception was the direct cause of the following exception:",
|
|
|
|
"> * multitask_job()",
|
|
|
|
]
|
|
|
|
)
|
2016-10-28 07:15:05 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
"'__pypy__' in sys.builtin_module_names or sys.platform.startswith('java')"
|
|
|
|
)
|
2010-12-09 18:00:31 +08:00
|
|
|
def test_warn_missing(testdir):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makepyfile("")
|
2010-12-09 18:00:31 +08:00
|
|
|
result = testdir.run(sys.executable, "-OO", "-m", "pytest", "-h")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(["*WARNING*assert statements are not executed*"])
|
2016-06-25 17:27:10 +08:00
|
|
|
result = testdir.run(sys.executable, "-OO", "-m", "pytest")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(["*WARNING*assert statements are not executed*"])
|
2013-04-30 18:05:58 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-04-30 18:05:58 +08:00
|
|
|
def test_recursion_source_decode(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-04-30 18:05:58 +08:00
|
|
|
def test_something():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2013-04-30 18:05:58 +08:00
|
|
|
[pytest]
|
|
|
|
python_files = *.py
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-08-01 23:32:19 +08:00
|
|
|
result = testdir.runpytest("--collect-only")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2013-04-30 18:05:58 +08:00
|
|
|
<Module*>
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-12-12 13:41:48 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-12-12 13:41:48 +08:00
|
|
|
def test_AssertionError_message(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-12-12 13:41:48 +08:00
|
|
|
def test_hello():
|
|
|
|
x,y = 1,2
|
|
|
|
assert 0, (x,y)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-12-12 13:41:48 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2013-12-12 13:41:48 +08:00
|
|
|
*def test_hello*
|
|
|
|
*assert 0, (x,y)*
|
|
|
|
*AssertionError: (1, 2)*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-06-18 04:31:31 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(PY3, reason="This bug does not exist on PY3")
|
2015-06-18 04:31:31 +08:00
|
|
|
def test_set_with_unsortable_elements():
|
|
|
|
# issue #718
|
|
|
|
class UnsortableKey(object):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
raise RuntimeError()
|
|
|
|
|
|
|
|
def __repr__(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
return "repr({})".format(self.name)
|
2015-06-18 04:31:31 +08:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.name == other.name
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
|
|
|
|
2018-05-18 05:31:16 +08:00
|
|
|
left_set = {UnsortableKey(str(i)) for i in range(1, 3)}
|
|
|
|
right_set = {UnsortableKey(str(i)) for i in range(2, 4)}
|
2015-06-18 04:31:31 +08:00
|
|
|
expl = callequal(left_set, right_set, verbose=True)
|
|
|
|
# skip first line because it contains the "construction" of the set, which does not have a guaranteed order
|
|
|
|
expl = expl[1:]
|
2018-05-23 22:48:46 +08:00
|
|
|
dedent = textwrap.dedent(
|
|
|
|
"""
|
2015-06-18 04:31:31 +08:00
|
|
|
Extra items in the left set:
|
|
|
|
repr(1)
|
|
|
|
Extra items in the right set:
|
|
|
|
repr(3)
|
|
|
|
Full diff (fallback to calling repr on each item):
|
|
|
|
- repr(1)
|
|
|
|
repr(2)
|
|
|
|
+ repr(3)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
).strip()
|
|
|
|
assert "\n".join(expl) == dedent
|
2016-06-21 22:48:29 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-06-21 22:48:29 +08:00
|
|
|
def test_diff_newline_at_end(monkeypatch, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
r"""
|
2016-06-21 22:48:29 +08:00
|
|
|
def test_diff():
|
|
|
|
assert 'asdf' == 'asdf\n'
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-21 22:48:29 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
r"""
|
2016-06-21 22:48:29 +08:00
|
|
|
*assert 'asdf' == 'asdf\n'
|
|
|
|
* - asdf
|
|
|
|
* + asdf
|
|
|
|
* ? +
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-26 00:45:47 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-09-02 08:58:48 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2016-06-26 00:45:47 +08:00
|
|
|
def test_assert_tuple_warning(testdir):
|
2018-09-05 00:41:11 +08:00
|
|
|
msg = "assertion is always true"
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-06-26 00:45:47 +08:00
|
|
|
def test_tuple():
|
|
|
|
assert(False, 'you shall not pass')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-09-05 00:41:11 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["*test_assert_tuple_warning.py:2:*{}*".format(msg)])
|
|
|
|
|
|
|
|
# tuples with size != 2 should not trigger the warning
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
def test_tuple():
|
|
|
|
assert ()
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2018-09-05 00:41:11 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert msg not in result.stdout.str()
|
2016-06-26 08:21:51 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-06-26 08:21:51 +08:00
|
|
|
def test_assert_indirect_tuple_no_warning(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-06-26 08:21:51 +08:00
|
|
|
def test_tuple():
|
|
|
|
tpl = ('foo', 'bar')
|
|
|
|
assert tpl
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-rw")
|
|
|
|
output = "\n".join(result.stdout.lines)
|
|
|
|
assert "WR1" not in output
|
2016-08-26 00:04:14 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-08-26 00:04:14 +08:00
|
|
|
def test_assert_with_unicode(monkeypatch, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
u"""
|
2016-08-26 00:04:14 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
def test_unicode():
|
|
|
|
assert u'유니코드' == u'Unicode'
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-08-26 00:04:14 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*AssertionError*"])
|
2016-09-20 03:16:04 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-03-04 16:26:46 +08:00
|
|
|
def test_raise_unprintable_assertion_error(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
r"""
|
2017-03-04 16:26:46 +08:00
|
|
|
def test_raise_assertion_error():
|
|
|
|
raise AssertionError('\xff')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-03-04 16:26:46 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[r"> raise AssertionError('\xff')", "E AssertionError: *"]
|
|
|
|
)
|
2017-03-04 16:26:46 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-03-04 16:26:46 +08:00
|
|
|
def test_raise_assertion_error_raisin_repr(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
u"""
|
2017-03-04 16:26:46 +08:00
|
|
|
class RaisingRepr(object):
|
|
|
|
def __repr__(self):
|
|
|
|
raise Exception()
|
|
|
|
def test_raising_repr():
|
|
|
|
raise AssertionError(RaisingRepr())
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-03-04 16:26:46 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["E AssertionError: <unprintable AssertionError object>"]
|
|
|
|
)
|
2017-03-04 16:26:46 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-09-20 03:16:04 +08:00
|
|
|
def test_issue_1944(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-09-20 03:16:04 +08:00
|
|
|
def f():
|
|
|
|
return
|
|
|
|
|
|
|
|
assert f() == 10
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-09-20 03:16:04 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["*1 error*"])
|
2018-06-26 21:35:27 +08:00
|
|
|
assert (
|
|
|
|
"AttributeError: 'Module' object has no attribute '_obj'"
|
|
|
|
not in result.stdout.str()
|
|
|
|
)
|