2014-01-18 19:31:33 +08:00
|
|
|
from pytest import raises
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2008-08-16 23:26:59 +08:00
|
|
|
import py
|
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
def otherfunc(a, b):
|
|
|
|
assert a == b
|
|
|
|
|
|
|
|
|
|
|
|
def somefunc(x, y):
|
|
|
|
otherfunc(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
def otherfunc_multi(a, b):
|
|
|
|
assert a == b
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
|
2009-08-19 01:04:57 +08:00
|
|
|
def test_generative(param1, param2):
|
|
|
|
assert param1 * 2 < param2
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2009-08-19 01:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
2018-05-23 22:48:46 +08:00
|
|
|
if "param1" in metafunc.fixturenames:
|
2009-08-19 01:04:57 +08:00
|
|
|
metafunc.addcall(funcargs=dict(param1=3, param2=6))
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
class TestFailing(object):
|
|
|
|
def test_simple(self):
|
|
|
|
def f():
|
|
|
|
return 42
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
def g():
|
|
|
|
return 43
|
|
|
|
|
|
|
|
assert f() == g()
|
|
|
|
|
|
|
|
def test_simple_multiline(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
otherfunc_multi(42, 6 * 9)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def test_not(self):
|
|
|
|
def f():
|
|
|
|
return 42
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
assert not f()
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-09-16 08:07:53 +08:00
|
|
|
class TestSpecialisedExplanations(object):
|
|
|
|
def test_eq_text(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "spam" == "eggs"
|
2010-09-16 08:07:53 +08:00
|
|
|
|
|
|
|
def test_eq_similar_text(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "foo 1 bar" == "foo 2 bar"
|
2010-09-16 08:07:53 +08:00
|
|
|
|
|
|
|
def test_eq_multiline_text(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "foo\nspam\nbar" == "foo\neggs\nbar"
|
2010-09-16 08:07:53 +08:00
|
|
|
|
2010-10-01 06:15:41 +08:00
|
|
|
def test_eq_long_text(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
a = "1" * 100 + "a" + "2" * 100
|
|
|
|
b = "1" * 100 + "b" + "2" * 100
|
2010-10-01 06:15:41 +08:00
|
|
|
assert a == b
|
|
|
|
|
|
|
|
def test_eq_long_text_multiline(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
a = "1\n" * 100 + "a" + "2\n" * 100
|
|
|
|
b = "1\n" * 100 + "b" + "2\n" * 100
|
2010-10-01 06:15:41 +08:00
|
|
|
assert a == b
|
|
|
|
|
2010-09-16 08:07:53 +08:00
|
|
|
def test_eq_list(self):
|
|
|
|
assert [0, 1, 2] == [0, 1, 3]
|
|
|
|
|
2010-10-01 06:15:41 +08:00
|
|
|
def test_eq_list_long(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
a = [0] * 100 + [1] + [3] * 100
|
|
|
|
b = [0] * 100 + [2] + [3] * 100
|
2010-10-01 06:15:41 +08:00
|
|
|
assert a == b
|
|
|
|
|
2010-09-16 08:07:53 +08:00
|
|
|
def test_eq_dict(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0}
|
2010-09-16 08:07:53 +08:00
|
|
|
|
|
|
|
def test_eq_set(self):
|
2018-05-18 05:31:16 +08:00
|
|
|
assert {0, 10, 11, 12} == {0, 20, 21}
|
2010-09-16 08:07:53 +08:00
|
|
|
|
2010-10-05 00:49:30 +08:00
|
|
|
def test_eq_longer_list(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert [1, 2] == [1, 2, 3]
|
2010-10-05 00:49:30 +08:00
|
|
|
|
2010-10-01 06:15:41 +08:00
|
|
|
def test_in_list(self):
|
|
|
|
assert 1 in [0, 2, 3, 4, 5]
|
|
|
|
|
2010-12-10 09:03:26 +08:00
|
|
|
def test_not_in_text_multiline(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
text = "some multiline\ntext\nwhich\nincludes foo\nand a\ntail"
|
|
|
|
assert "foo" not in text
|
2010-12-10 09:03:26 +08:00
|
|
|
|
|
|
|
def test_not_in_text_single(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
text = "single foo line"
|
|
|
|
assert "foo" not in text
|
2010-12-10 09:03:26 +08:00
|
|
|
|
|
|
|
def test_not_in_text_single_long(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
text = "head " * 50 + "foo " + "tail " * 20
|
|
|
|
assert "foo" not in text
|
2010-12-10 09:03:26 +08:00
|
|
|
|
|
|
|
def test_not_in_text_single_long_term(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
text = "head " * 50 + "f" * 70 + "tail " * 20
|
|
|
|
assert "f" * 70 not in text
|
2010-12-10 09:03:26 +08:00
|
|
|
|
2010-09-16 08:07:53 +08:00
|
|
|
|
2010-10-07 01:20:09 +08:00
|
|
|
def test_attribute():
|
|
|
|
class Foo(object):
|
|
|
|
b = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-10-07 01:20:09 +08:00
|
|
|
i = Foo()
|
|
|
|
assert i.b == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_attribute_instance():
|
|
|
|
class Foo(object):
|
|
|
|
b = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-10-07 01:20:09 +08:00
|
|
|
assert Foo().b == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_attribute_failure():
|
|
|
|
class Foo(object):
|
|
|
|
def _get_b(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
raise Exception("Failed to get attrib")
|
|
|
|
|
2010-10-07 01:20:09 +08:00
|
|
|
b = property(_get_b)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-10-07 01:20:09 +08:00
|
|
|
i = Foo()
|
|
|
|
assert i.b == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_attribute_multiple():
|
|
|
|
class Foo(object):
|
|
|
|
b = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-10-07 01:20:09 +08:00
|
|
|
class Bar(object):
|
|
|
|
b = 2
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-10-07 01:20:09 +08:00
|
|
|
assert Foo().b == Bar().b
|
|
|
|
|
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
def globf(x):
|
2018-05-23 22:48:46 +08:00
|
|
|
return x + 1
|
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRaises(object):
|
2010-11-25 19:11:10 +08:00
|
|
|
def test_raises(self):
|
2018-05-24 00:12:04 +08:00
|
|
|
s = "qwe" # NOQA
|
2010-11-25 19:11:10 +08:00
|
|
|
raises(TypeError, "int(s)")
|
|
|
|
|
|
|
|
def test_raises_doesnt(self):
|
|
|
|
raises(IOError, "int('3')")
|
|
|
|
|
|
|
|
def test_raise(self):
|
|
|
|
raise ValueError("demo error")
|
|
|
|
|
|
|
|
def test_tupleerror(self):
|
2018-05-24 00:12:04 +08:00
|
|
|
a, b = [1] # NOQA
|
2010-11-25 19:11:10 +08:00
|
|
|
|
|
|
|
def test_reinterpret_fails_with_print_for_the_fun_of_it(self):
|
2018-05-24 00:12:04 +08:00
|
|
|
items = [1, 2, 3]
|
|
|
|
print("items is %r" % items)
|
|
|
|
a, b = items.pop()
|
2010-11-25 19:11:10 +08:00
|
|
|
|
|
|
|
def test_some_error(self):
|
2018-05-24 00:12:04 +08:00
|
|
|
if namenotexi: # NOQA
|
2010-11-25 19:11:10 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
def func1(self):
|
|
|
|
assert 41 == 42
|
|
|
|
|
|
|
|
|
|
|
|
# thanks to Matthew Scott for this test
|
|
|
|
def test_dynamic_compile_shows_nicely():
|
2018-01-30 04:23:17 +08:00
|
|
|
import imp
|
|
|
|
import sys
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
src = "def foo():\n assert 1 == 0\n"
|
|
|
|
name = "abc-123"
|
2017-12-27 11:47:26 +08:00
|
|
|
module = imp.new_module(name)
|
2018-05-23 22:48:46 +08:00
|
|
|
code = _pytest._code.compile(src, name, "exec")
|
2010-11-25 19:11:10 +08:00
|
|
|
py.builtin.exec_(code, module.__dict__)
|
2017-12-27 11:47:26 +08:00
|
|
|
sys.modules[name] = module
|
2010-11-25 19:11:10 +08:00
|
|
|
module.foo()
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestMoreErrors(object):
|
2010-11-25 19:11:10 +08:00
|
|
|
def test_complex_error(self):
|
|
|
|
def f():
|
|
|
|
return 44
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
def g():
|
|
|
|
return 43
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
somefunc(f(), g())
|
|
|
|
|
|
|
|
def test_z1_unpack_error(self):
|
2018-05-24 00:12:04 +08:00
|
|
|
items = []
|
|
|
|
a, b = items
|
2010-11-25 19:11:10 +08:00
|
|
|
|
|
|
|
def test_z2_type_error(self):
|
2018-05-24 00:12:04 +08:00
|
|
|
items = 3
|
|
|
|
a, b = items
|
2010-11-25 19:11:10 +08:00
|
|
|
|
|
|
|
def test_startswith(self):
|
|
|
|
s = "123"
|
|
|
|
g = "456"
|
|
|
|
assert s.startswith(g)
|
|
|
|
|
|
|
|
def test_startswith_nested(self):
|
|
|
|
def f():
|
|
|
|
return "123"
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
def g():
|
|
|
|
return "456"
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-11-25 19:11:10 +08:00
|
|
|
assert f().startswith(g())
|
|
|
|
|
|
|
|
def test_global_func(self):
|
|
|
|
assert isinstance(globf(42), float)
|
|
|
|
|
|
|
|
def test_instance(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
self.x = 6 * 7
|
2010-11-25 19:11:10 +08:00
|
|
|
assert self.x != 42
|
|
|
|
|
|
|
|
def test_compare(self):
|
|
|
|
assert globf(10) < 5
|
|
|
|
|
|
|
|
def test_try_finally(self):
|
|
|
|
x = 1
|
|
|
|
try:
|
|
|
|
assert x == 0
|
|
|
|
finally:
|
|
|
|
x = 0
|
|
|
|
|
2014-08-24 00:14:25 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestCustomAssertMsg(object):
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_single_line(self):
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2014-08-24 00:14:25 +08:00
|
|
|
a = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2014-08-24 00:14:25 +08:00
|
|
|
b = 2
|
|
|
|
assert A.a == b, "A.a appears not to be b"
|
|
|
|
|
|
|
|
def test_multiline(self):
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2014-08-24 00:14:25 +08:00
|
|
|
a = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2014-08-24 00:14:25 +08:00
|
|
|
b = 2
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
A.a == b
|
|
|
|
), "A.a appears not to be b\n" "or does not appear to be b\none of those"
|
2014-08-24 00:14:25 +08:00
|
|
|
|
|
|
|
def test_custom_repr(self):
|
2017-02-17 02:41:51 +08:00
|
|
|
class JSON(object):
|
2014-08-24 00:14:25 +08:00
|
|
|
a = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2014-08-24 00:14:25 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "This is JSON\n{\n 'foo': 'bar'\n}"
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2014-08-24 00:14:25 +08:00
|
|
|
a = JSON()
|
|
|
|
b = 2
|
|
|
|
assert a.a == b, a
|