python-hospital/hospital

Context manager and utilities to collect/combine results of multiple assertions

Opened this issue · 1 comments

In order to replace things like:

@hospital.healthcheck
def test_multiple():
    errors = []
    for domain in 'example.com', 'www.example.com':
        try:
            hospital.assert_ping(domain)
        except AssertionError, e:
            errors.append(e)
    if errors:
        raise AssertionError('\n'.join(errors))

With something like:

@hospital.healthcheck
def test_multiple():
    with hospital.multiple_assertions:
        for domain in 'example.com', 'www.example.com':
            hospital.assert_ping(domain)

Ideas:

  • assert_multiple() context manager automatically raises MultipleAssertionsError on exit.
  • with assert_multiple(collect_only=True) as assertions does not raise on exit but has a errors attribute.