Skip to content

xtr_mime.testing

Checks on the emails a test builds: their headers, subject, bodies and attachments.

Each check takes the message first and raises :class:AssertionError when the email is not what the test expects, saying what was expected and, where it helps, what was found::

email = Email().to("fabien@example.com").subject("Your order shipped").text("Hello")

assert_email_header_same(email, "To", "fabien@example.com")
assert_email_subject_contains(email, "order")
assert_email_text_body_contains(email, "Hello")

A check asked about something a message cannot have — the headers of raw bytes, the subject of a message that is not an :class:~xtr_mime.Email — raises :class:~xtr_mime.exception.LogicError instead: the test is wrong, not the email.

Nothing here needs a test framework, and the package itself never imports this module.

assert_email_attachment_count

assert_email_attachment_count(message: RawMessage, expected_value: int) -> None

Check message carries expected_value attachments, inline images included.

Raises:

assert_email_html_body_contains

assert_email_html_body_contains(message: RawMessage, expected_text: str) -> None

Check the HTML body of message contains expected_text, markup included.

Raises:

  • AssertionError –

    When it does not.

  • LogicError –

    When message is not an :class:~xtr_mime.Email.

  • InvalidArgumentError –

    When the body is bytes or a file in a charset Python does not know, as rendering the email reports it.

assert_email_html_body_not_contains

assert_email_html_body_not_contains(message: RawMessage, expected_text: str) -> None

Check the HTML body of message does not contain expected_text.

Raises:

  • AssertionError –

    When it does.

  • LogicError –

    When message is not an :class:~xtr_mime.Email.

  • InvalidArgumentError –

    When the body is bytes or a file in a charset Python does not know, as rendering the email reports it.

assert_email_subject_contains

assert_email_subject_contains(message: RawMessage, expected_value: str) -> None

Check the subject of message contains expected_value; a missing one reads empty.

Raises:

assert_email_subject_not_contains

assert_email_subject_not_contains(message: RawMessage, expected_value: str) -> None

Check the subject of message does not contain expected_value.

Raises:

assert_email_text_body_contains

assert_email_text_body_contains(message: RawMessage, expected_text: str) -> None

Check the text body of message contains expected_text; a missing one reads empty.

A body given as bytes is read in its charset, one given as a file from its start.

Raises:

  • AssertionError –

    When it does not.

  • LogicError –

    When message is not an :class:~xtr_mime.Email.

  • InvalidArgumentError –

    When the body is bytes or a file in a charset Python does not know, as rendering the email reports it.

assert_email_text_body_not_contains

assert_email_text_body_not_contains(message: RawMessage, expected_text: str) -> None

Check the text body of message does not contain expected_text.

Raises:

  • AssertionError –

    When it does.

  • LogicError –

    When message is not an :class:~xtr_mime.Email.

  • InvalidArgumentError –

    When the body is bytes or a file in a charset Python does not know, as rendering the email reports it.

assert_email_address_contains

assert_email_address_contains(message: RawMessage, header_name: str, expected_value: str) -> None

Check the header_name address header of message holds expected_value.

expected_value is the bare address, fabien@example.com, whatever name it is written with.

Raises:

  • AssertionError –

    When no address of the header is expected_value.

  • LogicError –

    When message is raw bytes, or the header is missing or holds no address.

assert_email_has_header

assert_email_has_header(message: RawMessage, header_name: str) -> None

Check message has a header_name header.

Raises:

assert_email_header_not_same

assert_email_header_not_same(message: RawMessage, header_name: str, expected_value: str) -> None

Check the header_name header of message is missing or reads another value.

Raises:

  • AssertionError –

    When the header reads expected_value.

  • LogicError –

    When message is raw bytes, with no headers to read.

assert_email_header_same

assert_email_header_same(message: RawMessage, header_name: str, expected_value: str) -> None

Check the header_name header of message reads expected_value.

A text header is compared by its value, any other by its body as written on the wire — "Fabien <fabien@example.com>" for an address.

Raises:

  • AssertionError –

    When the header reads anything else, or is missing.

  • LogicError –

    When message is raw bytes, with no headers to read.

assert_email_not_has_header

assert_email_not_has_header(message: RawMessage, header_name: str) -> None

Check message has no header_name header.

Raises: