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)
assert_that(subject).does_not_match(comparison)
assert_that(subject) == comparison
assert_that(subject) != comparison
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))
assert_that(5).matches(is_odd() | is_greater_than(10))
assert_that(5).matches(~is_even())
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))
assert_that(5).matches(is_between(5, 10).exclusive)
assert_that(5).matches(is_between(5, 10).inclusive)
is_same_instance_as
x = "hello"
y = x
assert_that(x).matches(is_same_instance_as(y))
as_string_matches
When the subject called with the str the subject matches the criteria
assert_that(5).matches(as_string_matches("5"))
assert_that(12345).matches(as_string_matches(ends_with("45")))
Exceptions
Exceptions can be tested in two different ways.
- As a callable
assert_that(lambda: my_exception_raising_method(123)).matches(raises_exception(Exception))
- As a context manager
with raises_exception(Exception):
my_exception_raising_method(123)