コード例 #1
0
def test_regex():
    '''
    Test using `RaiseAssertor` specifying regex pattern for exception message.
    '''
    with RaiseAssertor(Exception, re.compile(r'^123\w*?456$')):
        raise TypeError('123qwerty456')

    with RaiseAssertor(Failure):
        with RaiseAssertor(Exception, re.compile('^ooga b?ooga$')):
            raise TypeError('123qwerty456')

    with RaiseAssertor(Failure):
        with RaiseAssertor(OSError, re.compile(r'^123\w*?456$')):
            raise SyntaxError('123qwerty456')
コード例 #2
0
def test_assert_exact_type():
    '''Test `RaiseAssertor`'s `assert_exact_type` option.'''
    with RaiseAssertor(LookupError):
        raise KeyError("Look at me, I'm a KeyError")

    error_message = (
        "was raised, and it *is* an instance of the LookupError we were "
        "expecting; but its type is not LookupError, it's KeyError, which "
        "is a subclass of LookupError, and you specified "
        "`assert_exact_type=True`, so subclasses aren't acceptable.")

    with RaiseAssertor(Failure, error_message):
        with RaiseAssertor(LookupError, assert_exact_type=True):
            raise KeyError("Look at me, I'm a KeyError")
コード例 #3
0
def test_string():
    '''
    Test using `RaiseAssertor` specifying sub-string of the exception message.
    '''
    with RaiseAssertor(Exception, 'wer'):
        raise TypeError('123qwerty456')

    with RaiseAssertor(Failure):
        with RaiseAssertor(Exception, 'ooga booga'):
            raise TypeError('123qwerty456')

    with RaiseAssertor(Failure):
        with RaiseAssertor(OSError, 'wer'):
            raise SyntaxError('123qwerty456')
コード例 #4
0
def test():
    '''Test the basic workings of `assert_same_signature`.'''
    def f(a, b=1, **kwargs):
        pass

    def g(a, b=1, **kwargs):
        pass

    def h(z):
        pass

    assert_same_signature(f, g)
    with RaiseAssertor(Failure):
        assert_same_signature(f, h)
    with RaiseAssertor(Failure):
        assert_same_signature(g, h)

    new_f = decorator_module.decorator(lambda *args, **kwargs: None, f)

    assert_same_signature(f, g, new_f)
    with RaiseAssertor(Failure):
        assert_same_signature(new_f, h)

    new_h = decorator_module.decorator(lambda *args, **kwargs: None, h)

    assert_same_signature(h, new_h)
    with RaiseAssertor(Failure):
        assert_same_signature(new_h, new_f)
    with RaiseAssertor(Failure):
        assert_same_signature(new_h, new_f, g)
    with RaiseAssertor(Failure):
        assert_same_signature(new_h, f)

    assert_same_signature(new_h, h, new_h, new_h)
コード例 #5
0
def test_basic():
    '''Test the basic workings of `RaiseAssertor`.'''
    with RaiseAssertor(Exception):
        raise Exception
    with RaiseAssertor(Exception):
        raise TypeError

    def f():
        with RaiseAssertor(ZeroDivisionError):
            raise MyException

    pytest.raises(Failure, f)
    with RaiseAssertor(Failure):
        f()

    def g():
        with RaiseAssertor(Exception):
            pass

    pytest.raises(Failure, g)
    with RaiseAssertor(Failure):
        g()

    def h():
        with RaiseAssertor(RuntimeError, 'booga'):
            pass

    pytest.raises(Failure, h)
    with RaiseAssertor(Failure):
        h()

    with RaiseAssertor(Failure) as raise_assertor:
        assert isinstance(raise_assertor, RaiseAssertor)
        with RaiseAssertor(RuntimeError):
            {}[0]

    assert isinstance(raise_assertor.exception, Exception)
コード例 #6
0
 def h():
     with RaiseAssertor(RuntimeError, 'booga'):
         pass
コード例 #7
0
 def g():
     with RaiseAssertor(Exception):
         pass
コード例 #8
0
 def f():
     with RaiseAssertor(ZeroDivisionError):
         raise MyException