john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

retry decorator test

import pytest
from retry_decorator import retry

def test_retry_no_exception_raised():
    @retry( Exception , tries =  4 )
    def test_success( text ):
        return text

    assert test_success( 'success' ) == 'success'


def test_no_retry_required():
    @retry( Exception , tries = 2 )
    def succeeds( counter ):
        counter += 1
        return counter

    assert( succeeds( 0 ), 1)


def test_retry_with_exception():
    @retry( Exception , tries = 2 )
    def test_fail( text ):
        raise Exception( text )

    with pytest.raises( Exception ):
        test_fail( 'fail' )

# - - - - - NOT SURE IF THIS IS WORKING - - - - - -

def test_no_retry_required_class():
    class mycount:
        counter = 0
        def __init__( self , counter ):
            self.counter = counter

        @retry( Exception , tries = 2 )
        def succeeds( self ):
            self.counter += 1

    tester = mycount( 0 )
    tester.succeeds()
    assert( tester.counter , 1)


"""
def test_raise_unexpected_error():
    @retry( IOError , tries = 2 )
    def raise_unexpected_error():
        raise AssertionError( 'AssertionError not retried' )

    with pytest.raises( AssertionError ):
        raise_unexpected_error()
"""

"""
def test_retry_once_class():

    class mycount:
        counter = 0
        def __init__( self , counter ):
            self.counter = counter

        @retry( Exception , tries = 2 )
        def succeeds( self ):
            self.counter += 1
            raise Exception( 'failed' )

    tester = mycount( 0 )
    try:
        tester.succeeds()
    except Exception as error:
        pass

    assert( tester.counter , None )
    assert( tester.counter , 0 )    # why do all of these asserts pass?
    assert( tester.counter , 1 )
"""

  • « pyinstaller request sslerror manual cacert solution
  • oxygen exception exit decorator »

Published

Mar 9, 2013

Category

python-oxygencloud-snapshot

~156 words

Tags

  • decorator 8
  • python 180
  • retry 4
  • snapshot 12
  • test 29