Skip to main content

A small assertion library

Project description

Fluent Assertions

A DSL like assertion library that enables concise high quality assertions in your tests

Assertions

Assertions follow a simple pattern

assert_that(subject).matches(comparison) # object or criteria
assert_that(subject).does_not_match(comparison) # object or criteria

# Same as above
assert_that(subject) == comparison # object or criteria
assert_that(subject) != comparison # object or criteria

Using matchers without criteria, just use classic asserts

assert 2 == is_greater_than(1)

Critera Objects

Critera helps define complex assertions in a concise manner

Some examples of criteria

assert_that(5).matches(is_greater_than(4))
assert_that(5).matches(is_greater_than_or_equal(5))
assert_that(5).matches(is_odd())

Logical criteria operations

assert_that(5).matches(is_odd() & is_greater_than(4)) # And
assert_that(5).matches(is_odd() | is_greater_than(10)) # Or
assert_that(5).matches(~is_even()) # Inverted

Matching

Basic

is_equal_to

assert_that(5).matches(5)
assert_that(5).matches(is_equal_to(5))

assert_that("hello").matches("hello")
assert_that("hello").matches(is_equal_to("hello"))

is_greater_than

assert_that(5).matches(is_greater_than(3))
assert_that(5) == is_greater_than(3)

is_greater_than_or_equal

assert_that(5).matches(is_greater_than_or_equal(5))

is_less_than

assert_that(5).matches(is_less_than(10))

is_less_than_or_equal

assert_that(5).matches(is_less_than_or_equal(10))
assert_that(5).matches(is_less_than_or_equal(5))

is_between

assert_that(5).matches(is_between(5, 10)) # passes
assert_that(5).matches(is_between(5, 10).exclusive) # fails
assert_that(5).matches(is_between(5, 10).inclusive) # passes

is_same_instance_as

x = "hello"
y = x

assert_that(x).matches(is_same_instance_as(y)) # passes

as_string_matches

When the subject called with the str the subject matches the criteria

assert_that(5).matches(as_string_matches("5")) # passes
assert_that(12345).matches(as_string_matches(ends_with("45"))) # passes

Exceptions

Exceptions can be tested in two different ways.

  1. As a callable
assert_that(lambda: my_exception_raising_method(123)).matches(raises_exception(Exception))
  1. As a context manager
with raises_exception(Exception):
    my_exception_raising_method(123)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

assertive-0.0.3.tar.gz (10.5 kB view hashes)

Uploaded Source

Built Distribution

assertive-0.0.3-py3-none-any.whl (14.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page