def test_check_dict_like_no_values_callable(): try: ab = assert_that(None) ab._check_dict_like(CustomDictNoValuesCallable()) fail('should have raised error') except TypeError as e: assert_that(str(e)).contains('is not dict-like: missing values()')
def test_is_type_of_subclass_failure(self): try: assert_that(Bar()).is_type_of(Foo) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).starts_with('Expected <') assert_that(str(ex)).ends_with(':Bar> to be of type <Foo>, but was not.')
def test_is_after_failure(self): try: d2 = datetime.datetime.today() assert_that(self.d1).is_after(d2) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be after <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}>, but was not.')
def test_check_dict_like_no_getitem(): try: ab = assert_that(None) ab._check_dict_like(CustomDictNoGetitem()) fail('should have raised error') except TypeError as e: assert_that(str(e)).contains('is not dict-like: missing [] accessor')
def test_is_equal_to_ignoring_milliseconds_failure(self): try: d2 = datetime.datetime.today() + datetime.timedelta(days=1) assert_that(self.d1).is_equal_to_ignoring_milliseconds(d2) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be equal to <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}>, but was not.')
def test_is_close_to_failure(self): try: d2 = self.d1 + datetime.timedelta(minutes=5) assert_that(self.d1).is_close_to(d2, datetime.timedelta(minutes=1)) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be close to <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> within tolerance <\d+:\d{2}:\d{2}>, but was not.')
def test_is_close_to_bad_tolerance_arg_type_failure(self): try: d2 = datetime.datetime.today() assert_that(self.d1).is_close_to(d2, 123) fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('given tolerance arg must be timedelta, but was <int>')
def test_is_less_than_or_equal_to_failure(self): try: d2 = datetime.datetime.today() assert_that(d2).is_less_than_or_equal_to(self.d1) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be less than or equal to <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}>, but was not.')
def test_is_before_failure(): try: d2 = datetime.datetime.today() assert_that(d2).is_before(d1) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be before <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}>, but was not.')
def test_traceback(): try: assert_that('foo').is_equal_to('bar') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to be equal to <bar>, but was not.') assert_that(ex).is_type_of(AssertionError) # extract all stack frames from the traceback _, _, tb = sys.exc_info() assert_that(tb).is_not_none() # walk_tb added in 3.5 if sys.version_info[0] == 3 and sys.version_info[1] >= 5: frames = [(f.f_code.co_filename, f.f_code.co_name, lineno) for f,lineno in traceback.walk_tb(tb)] assert_that(frames).is_length(3) assert_that(frames[0][0]).ends_with('test_traceback.py') assert_that(frames[0][1]).is_equal_to('test_traceback') assert_that(frames[0][2]).is_equal_to(35) assert_that(frames[1][0]).ends_with('assertpy.py') assert_that(frames[1][1]).is_equal_to('is_equal_to') assert_that(frames[1][2]).is_greater_than(160) assert_that(frames[2][0]).ends_with('assertpy.py') assert_that(frames[2][1]).is_equal_to('_err') assert_that(frames[2][2]).is_greater_than(1000)
def step_impl( context, ammount ) : for i in range( 0, ammount ): id = 'sim-sampler-' + str( i ) try: context.driver.find_element_by_id( id ) except: fail( 'live plot ' + id + ' is not found' )
def test_check_iterable_no_getitem(): try: ab = assert_that(None) ab._check_iterable(set([1]), name='my-set') fail('should have raised error') except TypeError as e: assert_that(str(e)).contains('my-set <set> does not have [] accessor')
def test_check_iterable_not_iterable(): try: ab = assert_that(None) ab._check_iterable(123, name='my-int') fail('should have raised error') except TypeError as e: assert_that(str(e)).contains('my-int <int> is not iterable')
def test_is_less_than_or_equal_to_failure(self): try: t2 = datetime.timedelta(seconds=90) assert_that(t2).is_less_than_or_equal_to(self.t1) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <\d{1,2}:\d{2}:\d{2}> to be less than or equal to <\d{1,2}:\d{2}:\d{2}>, but was not.')
def test_is_greater_than_failure(self): try: t2 = datetime.timedelta(seconds=90) assert_that(self.t1).is_greater_than(t2) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <\d{1,2}:\d{2}:\d{2}> to be greater than <\d{1,2}:\d{2}:\d{2}>, but was not.')
def test_expected_exception_one_arg_failure(): try: assert_that(func_noop).raises(RuntimeError).when_called_with('foo') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to( "Expected <func_noop> to raise <RuntimeError> when called with ('foo').")
def test_expected_exception_all_failure(): try: assert_that(func_noop).raises(RuntimeError).when_called_with('a', 'b', 3, 4, foo=1, bar=2, baz='dog') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to( "Expected <func_noop> to raise <RuntimeError> when called with ('a', 'b', 3, 4, 'bar': 2, 'baz': 'dog', 'foo': 1).")
def test_extracting_dict_missing_key_failure(self): people_as_dicts = [{'first_name': p.first_name, 'last_name': p.last_name} for p in self.people] try: assert_that(people_as_dicts).extracting('foo') fail('should have raised error') except ValueError as ex: assert_that(str(ex)).matches(r'item keys \[.*\] did not contain key <foo>')
def test_is_not_same_as_failure(): for obj in [object(), 1, 'foo', True, None, 123.456]: try: assert_that(obj).is_not_same_as(obj) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <.+> to be not identical to <.+>, but was.')
def test_is_file_directory_failure(self): try: dirname = os.path.dirname(self.tmp.name) assert_that(dirname).is_file() fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <.*> to be a file, but was not.')
def test_soft_assertions(self): try: with soft_assertions(): assert_that('foo').is_length(4) assert_that('foo').is_empty() assert_that('foo').is_false() assert_that('foo').is_digit() assert_that('123').is_alpha() assert_that('foo').is_upper() assert_that('FOO').is_lower() assert_that('foo').is_equal_to('bar') assert_that('foo').is_not_equal_to('foo') assert_that('foo').is_equal_to_ignoring_case('BAR') fail('should have raised error') except AssertionError as e: assert_that(str(e)).contains('1. Expected <foo> to be of length <4>, but was <3>.') assert_that(str(e)).contains('2. Expected <foo> to be empty string, but was not.') assert_that(str(e)).contains('3. Expected <False>, but was not.') assert_that(str(e)).contains('4. Expected <foo> to contain only digits, but did not.') assert_that(str(e)).contains('5. Expected <123> to contain only alphabetic chars, but did not.') assert_that(str(e)).contains('6. Expected <foo> to contain only uppercase chars, but did not.') assert_that(str(e)).contains('7. Expected <FOO> to contain only lowercase chars, but did not.') assert_that(str(e)).contains('8. Expected <foo> to be equal to <bar>, but was not.') assert_that(str(e)).contains('9. Expected <foo> to be not equal to <foo>, but was.') assert_that(str(e)).contains('10. Expected <foo> to be case-insensitive equal to <BAR>, but was not.')
def test_failure(): try: with soft_assertions(): assert_that('foo').is_length(4) assert_that('foo').is_empty() assert_that('foo').is_false() assert_that('foo').is_digit() assert_that('123').is_alpha() assert_that('foo').is_upper() assert_that('FOO').is_lower() assert_that('foo').is_equal_to('bar') assert_that('foo').is_not_equal_to('foo') assert_that('foo').is_equal_to_ignoring_case('BAR') assert_that({'a': 1}).has_a(2) assert_that({'a': 1}).has_foo(1) fail('should have raised error') except AssertionError as e: out = str(e) assert_that(out).contains('Expected <foo> to be of length <4>, but was <3>.') assert_that(out).contains('Expected <foo> to be empty string, but was not.') assert_that(out).contains('Expected <False>, but was not.') assert_that(out).contains('Expected <foo> to contain only digits, but did not.') assert_that(out).contains('Expected <123> to contain only alphabetic chars, but did not.') assert_that(out).contains('Expected <foo> to contain only uppercase chars, but did not.') assert_that(out).contains('Expected <FOO> to contain only lowercase chars, but did not.') assert_that(out).contains('Expected <foo> to be equal to <bar>, but was not.') assert_that(out).contains('Expected <foo> to be not equal to <foo>, but was.') assert_that(out).contains('Expected <foo> to be case-insensitive equal to <BAR>, but was not.') assert_that(out).contains('Expected <1> to be equal to <2> on key <a>, but was not.') assert_that(out).contains('Expected key <foo>, but val has no key <foo>.')
def test_is_between_bad_arg2_type_failure(self): try: d2 = datetime.datetime.today() assert_that(self.d1).is_between(d2, 123) fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('given high arg must be datetime, but was <int>')
def test_strings(self): assert_that('').is_not_none() assert_that('').is_empty() assert_that('').is_false() assert_that('').is_type_of(str) assert_that('').is_instance_of(str) assert_that('foo').is_length(3) assert_that('foo').is_not_empty() assert_that('foo').is_true() assert_that('foo').is_alpha() assert_that('123').is_digit() assert_that('foo').is_lower() assert_that('FOO').is_upper() assert_that('foo').is_iterable() assert_that('foo').is_equal_to('foo') assert_that('foo').is_not_equal_to('bar') assert_that('foo').is_equal_to_ignoring_case('FOO') if sys.version_info[0] == 3: assert_that('foo').is_unicode() else: assert_that(u'foo').is_unicode() assert_that('foo').contains('f') assert_that('foo').contains('f','oo') assert_that('foo').contains_ignoring_case('F','oO') assert_that('foo').does_not_contain('x') assert_that('foo').contains_only('f','o') assert_that('foo').contains_sequence('o','o') assert_that('foo').contains_duplicates() assert_that('fox').does_not_contain_duplicates() assert_that('foo').is_in('foo','bar','baz') assert_that('foo').is_not_in('boo','bar','baz') assert_that('foo').is_subset_of('abcdefghijklmnopqrstuvwxyz') assert_that('foo').starts_with('f') assert_that('foo').ends_with('oo') assert_that('foo').matches(r'\w') assert_that('123-456-7890').matches(r'\d{3}-\d{3}-\d{4}') assert_that('foo').does_not_match(r'\d+') # partial matches, these all pass assert_that('foo').matches(r'\w') assert_that('foo').matches(r'oo') assert_that('foo').matches(r'\w{2}') # match the entire string with an anchored regex pattern, passes assert_that('foo').matches(r'^\w{3}$') # fails try: assert_that('foo').matches(r'^\w{2}$') fail('should have raised error') except AssertionError: pass
def test_fail_with_msg(): try: with soft_assertions(): fail('foobar') fail('should have raised error') except AssertionError as e: out = str(e) assert_that(out).is_equal_to('Fail: foobar!')
def test_contains_single_item_dict_like_failure(): if sys.version_info[0] == 3: ordered = collections.OrderedDict([('z',9),('x',7),('y',8)]) try: assert_that(ordered).contains('a') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).ends_with('to contain key <a>, but did not.')
def test_is_same_as_failure(): try: obj = object() other = object() assert_that(obj).is_same_as(other) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).matches('Expected <.+> to be identical to <.+>, but was not.')
def test_described_as_with_extract(self): try: assert_that(self.people).described_as("extra msg").extract("first_name").contains("Fred", "Bob") fail("should have raised error") except AssertionError as ex: assert_that(str(ex)).is_equal_to( "[extra msg] Expected <['Fred', 'John']> to contain items ('Fred', 'Bob'), but did not contain <Bob>." )
def test_extracting_sort_custom_func_failure(): def _f(x): raise RuntimeError('foobar!') try: assert_that(users).extracting('user', sort=_f) fail('should have raised error') except RuntimeError as ex: assert_that(str(ex)).is_equal_to("foobar!")
def test_soft_fail_with_msg_without_context(): try: soft_fail('some msg') fail('should have raised error') except AssertionError as e: out = str(e) assert_that(out).is_equal_to('Fail: some msg!') assert_that(out).does_not_contain('should have raised error')
def test_is_upper_empty_value_failure(): try: assert_that('').is_upper() fail('should have raised error') except ValueError as ex: assert_that(str(ex)).is_equal_to('val is empty')
def test_does_not_match_bad_arg_empty_failure(): try: assert_that('fred').does_not_match('') fail('should have raised error') except ValueError as ex: assert_that(str(ex)).is_equal_to('given pattern arg must not be empty')
def test_is_length_failure(): try: assert_that('foo').is_length(4) fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to be of length <4>, but was <3>.')
def test_contains_ignoring_case_type_failure(): try: assert_that(123).contains_ignoring_case('f') fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('val is not a string or iterable')
def test_ends_with_bad_arg_type_failure(): try: assert_that('fred').ends_with(123) fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('given suffix arg must be a string')
def test_does_not_match_bad_arg_type_failure(): try: assert_that('fred').does_not_match(123) fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('given pattern arg must be a string')
def test_is_unicode_failure(): try: assert_that(123).is_unicode() fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <123> to be unicode, but was <int>.')
def test_ends_with_bad_value_type_failure(): try: assert_that(123).ends_with(12) fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('val is not a string or iterable')
def test_contains_single_item_failure(): try: assert_that('foo').contains('x') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to contain item <x>, but did not.')
def test_does_not_contain_key_bad_val_failure(): try: assert_that(123).does_not_contain_key(1) fail('should have raised error') except TypeError as ex: assert_that(str(ex)).contains('is not dict-like')
def test_contains_ignoring_case_single_item_failure(): try: assert_that('foo').contains_ignoring_case('X') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to case-insensitive contain item <X>, but did not.')
def test_contains_ignoring_case_missinge_item_failure(): try: assert_that('foo').contains_ignoring_case() fail('should have raised error') except ValueError as ex: assert_that(str(ex)).is_equal_to('one or more args must be given')
def test_is_upper_bad_value_type_failure(): try: assert_that(123).is_upper() fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('val is not a string')
def test_is_upper_failure(): try: assert_that('foo').is_upper() fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to contain only uppercase chars, but did not.')
def test_does_not_match_failure(): try: assert_that('fred').does_not_match(r'\w+') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <fred> to not match pattern <\\w+>, but did.')
def test_is_digit_punctuation_failure(): try: assert_that('-123').is_digit() fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <-123> to contain only digits, but did not.')
def test_matches_failure(): try: assert_that('fred').matches(r'\d+') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <fred> to match pattern <\\d+>, but did not.')
def test_is_digit_space_failure(): try: assert_that('1 000 000').is_digit() fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <1 000 000> to contain only digits, but did not.')
def test_ends_with_bad_arg_empty_failure(): try: assert_that('fred').ends_with('') fail('should have raised error') except ValueError as ex: assert_that(str(ex)).is_equal_to('given suffix arg must not be empty')
def test_is_alpha_punctuation_failure(): try: assert_that('foo,bar').is_alpha() fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <foo,bar> to contain only alphabetic chars, but did not.')
def test_starts_with_bad_arg_none_failure(): try: assert_that('fred').starts_with(None) fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('given prefix arg must not be none')
def test_contains_multi_item_single_failure(): try: assert_that('foo').contains('f','o', 'x') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to("Expected <foo> to contain items <'f', 'o', 'x'>, but did not contain <x>.")
def test_is_equal_ignoring_case_failure(): try: assert_that('foo').is_equal_to_ignoring_case('bar') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to be case-insensitive equal to <bar>, but was not.')
def test_ends_with_failure(): try: assert_that('fred').ends_with('bar') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <fred> to end with <bar>, but did not.')
def test_does_not_contain_list_multi_item_failure(): try: assert_that('foo').does_not_contain('x','f','o') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to("Expected <foo> to not contain items <'x', 'f', 'o'>, but did contain <'f', 'o'>.")
def test_is_equal_ignoring_case_bad_arg_type_failure(): try: assert_that('fred').is_equal_to_ignoring_case(12) fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('given arg must be a string')
def test_contains_ignoring_case_list_multi_item_type_failure(): try: assert_that(['foo','bar']).contains_ignoring_case('F', 12) fail('should have raised error') except TypeError as ex: assert_that(str(ex)).is_equal_to('given args must all be strings')
def test_is_not_empty_failure(): try: assert_that('').is_not_empty() fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected not empty string, but was empty.')
def test_contains_ignoring_case_list_multi_item_failure(): try: assert_that(['foo','bar']).contains_ignoring_case('Foo','X','Y') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to("Expected <['foo', 'bar']> to case-insensitive contain items <'Foo', 'X', 'Y'>, but did not contain <'X', 'Y'>.")
def test_does_not_contain_single_item_failure(): try: assert_that('foo').does_not_contain('f') fail('should have raised error') except AssertionError as ex: assert_that(str(ex)).is_equal_to('Expected <foo> to not contain item <f>, but did.')