本文整理汇总了Python中pytest.raises_regexp函数的典型用法代码示例。如果您正苦于以下问题:Python raises_regexp函数的具体用法?Python raises_regexp怎么用?Python raises_regexp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了raises_regexp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_int_in_list_in_dict_in_list_in_dict
def test_int_in_list_in_dict_in_list_in_dict(self):
spec = translate({'foo': [{'bar': [int]}]})
with raises_regexp(ValidationError,
"'foo' value must be list"):
spec({'foo': None})
with raises_regexp(ValidationError,
"'foo' value item #0: 'bar' value must be list"):
spec({'foo': [{'bar': None}]})
with raises_regexp(ValidationError,
"'foo' value lacks item: must be dict"):
spec({'foo': []})
with raises_regexp(ValidationError,
"'foo' value item #0: 'bar' value lacks item: must be int"):
spec({'foo': [{'bar': []}]})
spec({'foo': [{'bar': [1]}]})
spec({'foo': [{'bar': [1, 2]}]})
with raises_regexp(ValidationError,
"'foo' value item #0: 'bar' value item #1: must be int"):
spec({'foo': [{'bar': [1, 'bogus']}]})
示例2: test_custom_validators_in_dict_keys
def test_custom_validators_in_dict_keys(self):
# FIXME this is one of those cases where "human-readable" errors
# are much less readable than "mechanical" ones (as before).
# Can we do anything?
day_note_schema = translate({
InRange(2000, 2020): {
InRange(1, 12): {
InRange(1, 31): str,
},
},
})
good_note = {2013: {12: {9: 'it is a good day today'}}}
bad_note1 = {1999: {12: {9: 'wrong year: below min'}}}
bad_note2 = {2013: {13: {9: 'wrong month: above max'}}}
bad_note3 = {2013: {12: {40: 'wrong day of month: above max'}}}
day_note_schema(good_note)
#with raises_regexp(InvalidKeys, '^1999$'):
with raises_regexp(ValidationError,
"^must not have keys like 1999$"):
day_note_schema(bad_note1)
#with raises_regexp(InvalidKeys, '^2013: 13$'):
with raises_regexp(ValidationError,
"^2013 value must not have keys like 13$"):
day_note_schema(bad_note2)
#with raises_regexp(InvalidKeys, '^2013: 12: 40$'):
with raises_regexp(ValidationError,
"^in 2013 \(12 value must not have keys like 40\)$"):
day_note_schema(bad_note3)
示例3: test_int_in_list
def test_int_in_list(self):
spec = ListOf(int)
# outer value is missing
with raises_regexp(ValidationError, 'must be list'):
spec(None)
# outer value is present, inner value is missing
with raises_regexp(ValidationError, 'lacks item: must be int'):
spec([])
# outer value is present, inner optional value is missing
relaxed_spec = ListOf(int) | None
relaxed_spec([])
# inner value is present but is None
with raises_regexp(ValidationError, 'item #0: must be int'):
spec([None])
# inner value is present
spec([123])
# multiple inner values are present
spec([123, 456])
# one of the inner values is of a wrong type
with raises_regexp(ValidationError, 'item #1: must be int'):
spec([123, 'bogus'])
示例4: test_schemed_dict_in_list
def test_schemed_dict_in_list(self):
spec = ListOf({'foo': int})
# dict in list: missing key
with raises(ValidationError):
spec([{}])
with raises_regexp(ValidationError, "item #1: must have keys: 'foo'"):
spec([{'foo': 123}, {}])
# dict in list: missing value
with raises_regexp(ValidationError, "item #0: 'foo' value must be int"):
spec([{'foo': None}])
with raises_regexp(ValidationError, "item #1: 'foo' value must be int"):
spec([{'foo': 123}, {'foo': None}])
# multiple innermost values are present
spec([{'foo': 123}])
spec([{'foo': 123}, {'foo': 456}])
# one of the innermost values is of a wrong type
with raises_regexp(ValidationError, "item #2: 'foo' value must be int"):
spec([{'foo': 123}, {'foo': 456}, {'foo': 'bogus'}])
示例5: test_exists
def test_exists():
must_exist = Exists()
assert repr(must_exist) == 'must exist'
with raises_regexp(ValidationError, 'must exist'):
must_exist(MISSING)
must_exist(None)
must_exist(0)
must_exist(False)
must_exist('foo')
must_exist([])
# negated
must_not_exist = ~Exists()
assert repr(must_not_exist) == 'must not exist'
must_not_exist(MISSING)
with raises_regexp(ValidationError, 'must not exist'):
must_not_exist(None)
with raises_regexp(ValidationError, 'must not exist'):
must_not_exist('foo')
示例6: test_list_of_any
def test_list_of_any():
v = ListOfAny(IsA(str))
assert repr(v) == 'ListOfAny(must be str)'
with raises_regexp(ValidationError, '^must be list$'):
v('foo')
with raises_regexp(ValidationError, '^lacks item: must be str$'):
v([])
# positive
v(['foo'])
v(['foo', 123])
with raises_regexp(ValidationError,
'^item #0: must be str or item #1: must be str$'):
v([123, 5.5])
# negated
v = ~v
with raises_regexp(ValidationError, '^~ListOfAny\(must be str\)$'):
v(['foo'])
with raises_regexp(ValidationError, '^~ListOfAny\(must be str\)$'):
v(['foo', 123])
v([123, 5.5])
示例7: test_pytest_raises_regexp
def test_pytest_raises_regexp():
def f():
raise ValueError('f')
pytest.raises_regexp(ValueError, 'f', f)
with pytest.raises_regexp(ValueError, 'f'):
f()
示例8: test_functional_syntax
def test_functional_syntax():
raises_regexp(
ExpectedException,
r"Args and kwargs raised: .*param1.*param4",
function_to_test_with_args_and_kwargs,
'param1', 'param2', kwarg1='param3', kwarg2='param4'
)
示例9: test_source_branch_does_not_exist_caught
def test_source_branch_does_not_exist_caught(self, empty_dendrifier):
repo = empty_dendrifier.repo
# Doesn't really matter that these branches are unrelated:
dendrify.create_base(repo, 'test-base')
pytest.raises_regexp(ValueError, 'source branch "linear" does not exist',
empty_dendrifier.dendrify,
'dendrified', 'test-base', 'linear')
示例10: test_nonlinear_ancestry
def test_nonlinear_ancestry(self, empty_dendrifier):
repo = empty_dendrifier.repo
populate_repo(repo, ['.develop', '.', '.', '[', '.', '.', ']'])
empty_dendrifier.dendrify('dendrified', 'develop', 'linear')
pytest.raises_regexp(ValueError, 'ancestry of "dendrified" is not linear',
empty_dendrifier.dendrify,
're-dendrified', 'develop', 'dendrified')
示例11: test_corrupt_index
def test_corrupt_index(self, tmpfile):
with io.open(tmpfile, 'wb') as f:
f.write(FORMAT_STRING)
write_i64(f, FORMAT_VERSION)
f.write(b'foo')
pytest.raises_regexp(IOError, 'unable to read index',
File, tmpfile)
示例12: test_list_of_all
def test_list_of_all():
v = ListOfAll(IsA(str))
assert repr(v) == 'ListOfAll(must be str)'
with raises_regexp(ValidationError, '^must be list$'):
v('foo')
with raises_regexp(ValidationError, '^lacks item: must be str$'):
v([])
# positive
v(['foo'])
v(['foo', 'bar'])
with raises_regexp(ValidationError, '^item #2: must be str$'):
v(['foo', 'bar', 123])
# negated
v = ~v
with raises_regexp(ValidationError, '^~ListOfAll\(must be str\)$'):
v(['foo'])
with raises_regexp(ValidationError, '^~ListOfAll\(must be str\)$'):
v(['foo', 'bar'])
v(['foo', 'bar', 123])
示例13: test_fail_on_write
def test_fail_on_write(self, tmpfile):
with File(tmpfile, 'w') as f:
f.write_json('a', 42)
pytest.raises_regexp(ValueError, 'unable to serialize: invalid dtype',
f.write_array, 'b', {})
with File(tmpfile) as f:
assert list(f) == ['a']
assert f.read('a') == 42
示例14: test_linear_ancestry_reaches_root
def test_linear_ancestry_reaches_root(self, empty_dendrifier):
repo = empty_dendrifier.repo
populate_repo(repo, ['.develop', '.', '.'])
# Deliberately swap args to linear_ancestry() such that the
# 'base' is not an ancestor of the branch:
pytest.raises_regexp(ValueError, '"linear" is not an ancestor of "develop"',
empty_dendrifier.linear_ancestry,
'linear', 'develop')
示例15: test_dendrified_ancestry_reaches_root
def test_dendrified_ancestry_reaches_root(self, empty_dendrifier):
repo = empty_dendrifier.repo
populate_repo(repo, ['.develop', '.', '.', '[', '.', '.', ']'])
empty_dendrifier.dendrify('dendrified', 'develop', 'linear')
# Deliberately swap args so that base is not ancestor of branch:
pytest.raises_regexp(ValueError, '"dendrified" is not an ancestor of "develop"',
empty_dendrifier.linearize,
'linear-1', 'dendrified', 'develop')