示例#1
0
def test_does_not_contain_single_item_failure():
    try:
        assert_that(['a', 'b', 'c']).does_not_contain('a')
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            "Expected <['a', 'b', 'c']> to not contain item <a>, but did.")
示例#2
0
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(36)

            assert_that(frames[1][0]).ends_with('base.py')
            assert_that(frames[1][1]).is_equal_to('is_equal_to')
            assert_that(frames[1][2]).is_greater_than(40)

            assert_that(frames[2][0]).ends_with('assertpy.py')
            assert_that(frames[2][1]).is_equal_to('error')
            assert_that(frames[2][2]).is_greater_than(100)
示例#3
0
def test_extracting_bad_property_failure():
    try:
        assert_that(people).extracting('foo')
        fail('should have raised error')
    except ValueError as ex:
        assert_that(str(ex)).is_equal_to(
            'val does not have property or zero-arg method <foo>')
示例#4
0
 def test_snapshot_v2():
     try:
         assert_that(None).snapshot()
         fail('should have raised error')
     except NotImplementedError as ex:
         assert_that(
             str(ex)).is_equal_to('snapshot testing requires Python 3')
示例#5
0
def test_is_not_in_failure():
    try:
        assert_that(1).is_not_in(1, 2, 3)
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            'Expected <1> to not be in <1, 2, 3>, but was.')
示例#6
0
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')
示例#7
0
def test_failure_very_deep_dict():
    try:
        assert_that({
            'a': 1,
            'b': {
                'c': 2,
                'd': {
                    'e': 3,
                    'f': {
                        'x': 4,
                        'y': 5
                    }
                }
            }
        }).is_equal_to({
            'a': 1,
            'b': {
                'c': 2,
                'd': {
                    'e': 3,
                    'f': {
                        'x': 4,
                        'y': 6
                    }
                }
            }
        })
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            "Expected <{.., 'b': {.., 'd': {.., 'f': {.., 'y': 5}}}}> to be equal to <{.., 'b': {.., 'd': {.., 'f': {.., 'y': 6}}}}>, but was not."
        )
示例#8
0
def test_contains_single_item_failure():
    try:
        assert_that(['a', 'b', 'c']).contains('x')
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            "Expected <['a', 'b', 'c']> to contain item <x>, but did not.")
示例#9
0
def test_described_as():
    try:
        assert_that(1).described_as('extra msg').is_equal_to(2)
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            '[extra msg] Expected <1> to be equal to <2>, but was not.')
示例#10
0
def test_ends_with_failure():
    try:
        assert_that(['a', 'b', 'c']).ends_with('d')
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            "Expected ['a', 'b', 'c'] to end with <d>, but did not.")
示例#11
0
def test_is_length_failure():
    try:
        assert_that(['a', 'b', 'c']).is_length(4)
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            "Expected <['a', 'b', 'c']> to be of length <4>, but was <3>.")
示例#12
0
def test_is_empty_failure():
    try:
        assert_that(['a', 'b']).is_empty()
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            "Expected <['a', 'b']> to be empty, but was not.")
示例#13
0
def test_does_not_contain_duplicates_failure():
    try:
        assert_that([1, 2, 3, 3]).does_not_contain_duplicates()
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            'Expected <[1, 2, 3, 3]> to not contain duplicates, but did.')
示例#14
0
def test_contains_sequence_failure():
    try:
        assert_that([1, 2, 3]).contains_sequence(4, 5)
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            'Expected <[1, 2, 3]> to contain sequence <4, 5>, but did not.')
示例#15
0
def test_extracting_iterable_failure_set():
    try:
        assert_that([set([1])]).extracting(0).contains(1, 4, 7)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(
            str(ex)).is_equal_to('item <set> does not have [] accessor')
示例#16
0
 def test_snapshot_custom_id_int():
     try:
         assert_that('foo').snapshot(id=123)
         fail('should have raised error')
     except ValueError as ex:
         assert_that(
             str(ex)).starts_with('failed to create snapshot filename')
示例#17
0
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')
示例#18
0
 def test_snapshot_custom_path_none():
     try:
         assert_that('foo').snapshot(path=None)
         fail('should have raised error')
     except ValueError as ex:
         assert_that(
             str(ex)).starts_with('failed to create snapshot filename')
示例#19
0
def test_failure_single_entry():
    try:
        assert_that({'a': 1}).is_equal_to({'a': 2})
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).is_equal_to(
            "Expected <{'a': 1}> to be equal to <{'a': 2}>, but was not.")
示例#20
0
def test_extracting_too_many_args_method_failure():
    try:
        assert_that(people).extracting('say_hello')
        fail('should have raised error')
    except ValueError as ex:
        assert_that(str(ex)).is_equal_to(
            'val method <say_hello()> exists, but is not zero-arg method')
def test_expected_exception_no_arg_missing_raises_failure():
    try:
        assert_that(func_noop).when_called_with()
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).contains(
            'expected exception not set, raises() must be called first')
示例#22
0
def test_is_less_than_timedelta_bad_arg_type_failure():
    try:
        assert_that(t1).is_less_than(123)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to(
            'given arg must be <timedelta>, but was <int>')
示例#23
0
def test_is_not_close_to_bad_arg_type_failure():
    try:
        assert_that(d1).is_not_close_to(123, 456)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(
            str(ex)).is_equal_to('given arg must be datetime, but was <int>')
示例#24
0
def test_is_before_bad_val_type_failure():
    try:
        assert_that(123).is_before(123)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(
            str(ex)).is_equal_to('val must be datetime, but was type <int>')
示例#25
0
def test_is_not_between_bad_arg1_type_failure():
    try:
        assert_that(d1).is_not_between(123, 456)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to(
            'given low arg must be <datetime>, but was <int>')
示例#26
0
def test_is_greater_than_or_equal_to_bad_arg_type_failure():
    try:
        assert_that(d1).is_greater_than_or_equal_to(123)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(
            str(ex)).is_equal_to('given arg must be <datetime>, but was <int>')
示例#27
0
def test_is_equal_to_ignoring_time_bad_arg_type_failure():
    try:
        assert_that(d1).is_equal_to_ignoring_time(123)
        fail('should have raised error')
    except TypeError as ex:
        assert_that(str(ex)).is_equal_to(
            'given arg must be datetime, but was type <int>')
示例#28
0
def test_soft_assertions():
    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.'
        )
示例#29
0
def test_strings():
    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
示例#30
0
def test_contains_multi_item_failure():
    try:
        assert_that({'a': 1, 'b': 2, 'c': 3}).contains('a', 'x', 'z')
        fail('should have raised error')
    except AssertionError as ex:
        assert_that(str(ex)).contains(
            "to contain keys <'a', 'x', 'z'>, but did not contain keys <'x', 'z'>."
        )