本文整理汇总了Python中pytest.raises方法的典型用法代码示例。如果您正苦于以下问题:Python pytest.raises方法的具体用法?Python pytest.raises怎么用?Python pytest.raises使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pytest
的用法示例。
在下文中一共展示了pytest.raises方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ask_and_validate
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_ask_and_validate(io):
error = "This is not a color!"
def validator(color):
if color not in ["white", "black"]:
raise Exception(error)
return color
question = Question("What color was the white horse of Henry IV?", "white")
question.set_validator(validator)
question.set_max_attempts(2)
io.set_input("\nblack\n")
assert "white" == question.ask(io)
assert "black" == question.ask(io)
io.set_input("green\nyellow\norange\n")
with pytest.raises(Exception) as e:
question.ask(io)
assert error == str(e.value)
示例2: test_render_debug_better_error_message_recursion_error_with_multiple_duplicated_frames
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_render_debug_better_error_message_recursion_error_with_multiple_duplicated_frames():
io = BufferedIO()
io.set_verbosity(VERBOSE)
with pytest.raises(RecursionError) as e:
first()
trace = ExceptionTrace(e.value)
trace.render(io)
expected = r"... Previous 2 frames repeated \d+ times".format(
filename=re.escape(trace._get_relative_file_path(__file__)),
)
assert re.search(expected, io.fetch_output()) is not None
示例3: test_invalid_text_colour
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_invalid_text_colour():
"""Test valid text colour."""
sys.modules['spidev'] = mock.MagicMock()
import unicornhathd
unicornhathd.setup()
with pytest.raises(ValueError):
unicornhathd.set_pixel(0, 0, "Octarine")
示例4: test_model_serializer_mapping_is_none
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_model_serializer_mapping_is_none(self):
class EmptyPolymorphicSerializer(PolymorphicSerializer):
pass
with pytest.raises(ImproperlyConfigured) as excinfo:
EmptyPolymorphicSerializer()
assert str(excinfo.value) == (
'`EmptyPolymorphicSerializer` is missing a '
'`EmptyPolymorphicSerializer.model_serializer_mapping` attribute'
)
示例5: test_resource_type_field_name_is_not_string
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_resource_type_field_name_is_not_string(self, mocker):
class NotStringPolymorphicSerializer(PolymorphicSerializer):
model_serializer_mapping = mocker.MagicMock
resource_type_field_name = 1
with pytest.raises(ImproperlyConfigured) as excinfo:
NotStringPolymorphicSerializer()
assert str(excinfo.value) == (
'`NotStringPolymorphicSerializer.resource_type_field_name` must '
'be a string'
)
示例6: test_filter_codes_ValueError
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_filter_codes_ValueError():
"""Setting invalid filter codes on the Genome raises a ValueError."""
with pytest.raises(ValueError):
genome = Genome()
genome.filter_codes = ("asdf",)
示例7: test_targets_TypeError
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_targets_TypeError():
"""Targets with a NoneType source_file raises a TypeError."""
with pytest.raises(TypeError):
genome = Genome()
_ = genome.targets
示例8: test_covered_targets_source_file_TypeError
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_covered_targets_source_file_TypeError():
"""Targets with a NoneType source_file raises a TypeError."""
with pytest.raises(TypeError):
genome = Genome()
_ = genome.covered_targets
示例9: test_covered_targets_coverage_file_TypeError
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_covered_targets_coverage_file_TypeError(binop_file):
"""Targets with a NoneType coverage_file but valid source_file raises a TypeError."""
with pytest.raises(TypeError):
genome = Genome(binop_file)
genome.coverage_file = None
_ = genome.covered_targets
示例10: test_mutate_MutationException
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_mutate_MutationException(binop_file, mock_LocIdx):
"""Mutate with an invalid operation raises a mutation exception."""
genome = Genome(binop_file)
with pytest.raises(MutationException):
_ = genome.mutate(target_idx=mock_LocIdx, mutation_op="badoperation", write_cache=False)
示例11: test_mutate_ValueError_target
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_mutate_ValueError_target(binop_file, mock_LocIdx):
"""Mutate with a target_idx not in the targets raises a ValueError."""
genome = Genome(binop_file)
with pytest.raises(ValueError):
_ = genome.mutate(target_idx=mock_LocIdx, mutation_op=ast.Div, write_cache=False)
示例12: test_init_GenomeGroup_raise_TypeError
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_init_GenomeGroup_raise_TypeError():
"""Initialization with an non-file non-dir raises a TypeError."""
with pytest.raises(TypeError):
_ = GenomeGroup("somethingrandom")
示例13: test_GenomeGroup_folder_exception
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_GenomeGroup_folder_exception():
"""Invalid folders raise a type error."""
with pytest.raises(TypeError):
ggrp = GenomeGroup()
ggrp.add_folder("somethingrandom")
示例14: test_GenomeGroup_key_TypeError
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_GenomeGroup_key_TypeError(key, binop_file):
"""Values that are not Path type keys raise a type error."""
with pytest.raises(TypeError):
ggrp = GenomeGroup()
ggrp[key] = Genome(binop_file)
示例15: test_GenomeGroup_value_TypeError
# 需要导入模块: import pytest [as 别名]
# 或者: from pytest import raises [as 别名]
def test_GenomeGroup_value_TypeError(value, binop_file):
"""Non-Genome values raise a type error."""
with pytest.raises(TypeError):
ggrp = GenomeGroup()
ggrp[binop_file] = value