Python pytest.raises() Examples

The following are 30 code examples of pytest.raises(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pytest , or try the search function .
Example #1
Source File: test_question.py    From clikit with MIT License 8 votes vote down vote up
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) 
Example #2
Source File: test_exception_trace.py    From clikit with MIT License 7 votes vote down vote up
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 
Example #3
Source File: test_args_format_builder.py    From clikit with MIT License 6 votes vote down vote up
def test_add_command_fails_if_option_with_same_long_name_as_option_in_base_format(
    base_format_builder,
):
    base_format_builder.add_option(Option("option", "a"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(CannotAddOptionException):
        builder.add_command_option(CommandOption("option", "b")) 
Example #4
Source File: test_argument.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_multi_values_argument_and_default_value_is_not_list():
    with pytest.raises(ValueError):
        Argument("argument", Argument.MULTI_VALUED, default="Default") 
Example #5
Source File: test_option.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_long_name_starts_with_number():
    with pytest.raises(ValueError):
        Option("1option") 
Example #6
Source File: test_option.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_long_name_is_one_character():
    with pytest.raises(ValueError):
        Option("o") 
Example #7
Source File: test_option.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_long_name_starts_with_three_hyphens():
    with pytest.raises(ValueError):
        Option("-option") 
Example #8
Source File: test_option.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_invalid_flag_combination(flags):
    with pytest.raises(ValueError):
        Option("option", "o", flags) 
Example #9
Source File: test_console_aplication.py    From clikit with MIT License 5 votes vote down vote up
def test_fails_if_no_command_name(config):
    config.add_command_config(CommandConfig())

    with pytest.raises(CannotAddCommandException):
        ConsoleApplication(config) 
Example #10
Source File: test_argument.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_invalid_flags_combination(flags):
    with pytest.raises(ValueError):
        Argument("argument", flags) 
Example #11
Source File: test_argument.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_required_argument_and_default_value():
    with pytest.raises(ValueError):
        Argument("argument", Argument.REQUIRED, default="Default") 
Example #12
Source File: test_option.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_long_name_is_not_a_string():
    with pytest.raises(ValueError):
        Option(1234) 
Example #13
Source File: test_console_aplication.py    From clikit with MIT License 5 votes vote down vote up
def test_get_command_fails_if_command_not_found(config):
    app = ConsoleApplication(config)

    with pytest.raises(NoSuchCommandException):
        app.get_command("foobar") 
Example #14
Source File: test_table.py    From clikit with MIT License 5 votes vote down vote up
def test_set_header_row_fails_if_too_many_cells():
    table = Table()

    table.add_row(["a", "b", "c"])

    with pytest.raises(ValueError):
        table.set_header_row(["a", "b", "c", "d"]) 
Example #15
Source File: test_table.py    From clikit with MIT License 5 votes vote down vote up
def test_add_row_fails_if_too_many_cells():
    table = Table()

    table.set_header_row(["a", "b", "c"])

    with pytest.raises(ValueError):
        table.add_row(["a", "b", "c", "d"]) 
Example #16
Source File: test_table.py    From clikit with MIT License 5 votes vote down vote up
def test_set_header_row_fails_if_too_missing_cells():
    table = Table()

    table.add_row(["a", "b", "c"])

    with pytest.raises(ValueError):
        table.set_header_row(["a", "b"]) 
Example #17
Source File: test_argument.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_name_starts_with_number():
    with pytest.raises(ValueError):
        Argument("1argument") 
Example #18
Source File: test_option.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_long_name_contains_spaces():
    with pytest.raises(ValueError):
        Option("foo bar") 
Example #19
Source File: test_option.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_short_name_is_not_string():
    with pytest.raises(ValueError):
        Option("option", 1234) 
Example #20
Source File: test_option.py    From clikit with MIT License 5 votes vote down vote up
def test_fail_if_long_name_is_empty():
    with pytest.raises(ValueError):
        Option("") 
Example #21
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_get_option_fails_if_in_base_format_but_include_base_disabled(
    base_format_builder,
):
    base_format_builder.add_option(Option("option"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(NoSuchOptionException):
        builder.get_option("option", False) 
Example #22
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_get_option_fails_with_unknown_name(builder):
    with pytest.raises(NoSuchOptionException):
        builder.get_option("foo") 
Example #23
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_add_option_fails_if_same_short_name_in_base_format(base_format_builder):
    base_format_builder.add_option(Option("option1", "a"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option2", "a")) 
Example #24
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_add_option_fails_if_same_short_name_as_command_option(builder):
    builder.add_option(CommandOption("option1", "a"))

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option1", "a")) 
Example #25
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_add_option_fails_if_same_short_name(builder):
    builder.add_option(Option("option1", "a"))

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option2", "a")) 
Example #26
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_add_option_fails_if_same_long_name_as_command_option_in_base_format(
    base_format_builder,
):
    base_format_builder.add_option(Option("option", "a"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option", "b")) 
Example #27
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_add_option_fails_if_same_long_name_in_base_format(base_format_builder):
    base_format_builder.add_option(Option("option", "a"))

    builder = ArgsFormatBuilder(base_format_builder.format)

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option", "b")) 
Example #28
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_add_option_fails_if_same_long_name_as_command_option_alias(builder):
    builder.add_command_option(CommandOption("option", "a", ["alias"]))

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("alias", "b")) 
Example #29
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_add_option_fails_if_same_long_name_as_command_option(builder):
    builder.add_option(CommandOption("option", "a"))

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option", "b")) 
Example #30
Source File: test_args_format_builder.py    From clikit with MIT License 5 votes vote down vote up
def test_add_option_fails_if_same_long_name(builder):
    builder.add_option(Option("option", "a"))

    with pytest.raises(CannotAddOptionException):
        builder.add_option(Option("option", "b"))