failure_demo.py


Overview

`failure_demo.py` is a Python test suite primarily designed to demonstrate various failure cases and assertion error scenarios using the `pytest` testing framework. The file contains multiple test classes and functions that intentionally fail or raise exceptions to illustrate how `pytest` reports failures, compares data structures, and handles error conditions.

This file serves as a comprehensive showcase of different assertion patterns, error types, and test parametrizations, which can be valuable for:


Detailed Breakdown

1. Functions

otherfunc(a, b)

somefunc(x, y)

otherfunc_multi(a, b)

globf(x)


2. Test Functions (Outside Classes)

test_generative(param1, param2)

test_attribute(), test_attribute_instance(), test_attribute_failure(), test_attribute_multiple()

test_dynamic_compile_shows_nicely()


3. Test Classes

class TestFailing

class TestSpecialisedExplanations

class TestRaises

class TestMoreErrors

class TestCustomAssertMsg


Important Implementation Details


Interaction With Other Parts of the System


Usage Examples

To run the tests and see the failure reports:

pytest failure_demo.py

You will observe multiple assertion failures, unpacking errors, exception mismatches, and detailed comparison diffs generated by pytest.


Visual Diagram

classDiagram
    class TestFailing {
        +test_simple()
        +test_simple_multiline()
        +test_not()
    }

    class TestSpecialisedExplanations {
        +test_eq_text()
        +test_eq_similar_text()
        +test_eq_multiline_text()
        +test_eq_long_text()
        +test_eq_long_text_multiline()
        +test_eq_list()
        +test_eq_list_long()
        +test_eq_dict()
        +test_eq_set()
        +test_eq_longer_list()
        +test_in_list()
        +test_not_in_text_multiline()
        +test_not_in_text_single()
        +test_not_in_text_single_long()
        +test_not_in_text_single_long_term()
        +test_eq_dataclass()
        +test_eq_attrs()
    }

    class TestRaises {
        +test_raises()
        +test_raises_doesnt()
        +test_raise()
        +test_tupleerror()
        +test_reinterpret_fails_with_print_for_the_fun_of_it()
        +test_some_error()
        +func1()
    }

    class TestMoreErrors {
        +test_complex_error()
        +test_z1_unpack_error()
        +test_z2_type_error()
        +test_startswith()
        +test_startswith_nested()
        +test_global_func()
        +test_instance()
        +test_compare()
        +test_try_finally()
    }

    class TestCustomAssertMsg {
        +test_single_line()
        +test_multiline()
        +test_custom_repr()
    }

    TestFailing --> otherfunc_multi
    TestMoreErrors --> somefunc

Summary

`failure_demo.py` is a deliberately failure-focused test suite that exercises many pytest assertion failure modes and exception handling scenarios. It is primarily a diagnostic or educational tool showcasing pytest’s detailed and user-friendly failure reports, rather than a functional test suite verifying application logic.

By exploring this file, developers and testers can gain insight into:


End of Documentation for failure_demo.py