def test_kw(self): def to_test(**kw): raise ValueError('%r'%kw) should_raise( to_test, ValueError("{'x': 1}") )(x=1)
def test_kw_to_args(self): def to_test(x): raise ValueError('%s'%x) should_raise( to_test, ValueError('1') )(x=1)
def test_both(self): def to_test(*args,**kw): raise ValueError('%r %r'%(args,kw)) should_raise( to_test, ValueError("(1,) {'x': 2}") )(1,x=2)
def test_formatting_info(self): r = TestReader() f = Mock() r.formatting_info = True r.setup(('Sheet1',[['R1C1','R1C2']])) # at this point you can now manipulate the xf index as follows: book = r.books[0][0] sx,rx,cx = 0,0,0 book.sheet_by_index(sx)._cell_xf_indexes[rx][cx]=42 # NB: cells where you haven't specified an xf index manually as # above will have an xf index of 0: compare(book.sheet_by_index(0).cell(0,1).xf_index,0) # and no matching style: should_raise(book.xf_list,IndexError)[0] r(f) compare([ ('start', (), {}), ('workbook',(C('xlutils.tests.fixtures.DummyBook'), 'test.xls'),{}), ('sheet', (C('xlrd.sheet.Sheet'), 'Sheet1'), {}), ('row', (0, 0), {}), ('cell', (0, 0, 0, 0), {}), ('cell', (0, 1, 0, 1), {}), ('finish', (), {})],f.method_calls) compare(book.sheet_by_index(0).cell(0,0).xf_index,42)
def test_class_class(self): class Test: def __init__(self, x): # The TypeError is raised due to the mis-matched parameters # so the pass never gets executed pass # pragma: no cover should_raise(TypeError)(Test)()
def test_wrong_exception(self): def to_test(): raise ValueError('bar') with ShouldAssert( "ValueError('foo',) (expected) != ValueError('bar',) (raised)" ): should_raise(ValueError('foo'))(to_test)()
def test_args(self): def to_test(*args): raise ValueError('%s'%repr(args)) should_raise( to_test, ValueError('(1,)') )(1)
def test_method_args(self): class X: def to_test(self, *args): self.args = args raise ValueError() x = X() should_raise(ValueError)(x.to_test)(1, 2, 3) self.assertEqual(x.args, (1, 2, 3))
def test_method_kw(self): class X: def to_test(self, **kw): self.kw = kw raise ValueError() x = X() should_raise(ValueError)(x.to_test)(x=1, y=2) self.assertEqual(x.kw, {'x': 1, 'y': 2})
def test_no_supplied_or_raised(self): # effectvely we're saying "something should be raised!" # but we want to inspect s.raised rather than making # an up-front assertion def to_test(): pass with ShouldAssert("No exception raised!"): should_raise()(to_test)()
def test_wrong_exception(self): def to_test(): raise ValueError('bar') if PY_37_PLUS: expected = "ValueError('foo') (expected) != ValueError('bar') (raised)" else: expected = "ValueError('foo',) (expected) != ValueError('bar',) (raised)" with ShouldAssert(expected): should_raise(ValueError('foo'))(to_test)()
def test_not_there(self): o = object() @replace("testfixtures.tests.sample1.bad", o) def test_something(r): pass # pragma: no cover should_raise(test_something, AttributeError("Original 'bad' not found"))()
def test_empty_sheet_name(self): r = TestReader( ('',([['S1R0C0']]),), ) book = tuple(r.get_workbooks())[0][0] # fire methods on writer should_raise(r,ValueError( 'Empty sheet name will result in invalid Excel file!' ))(TestWriter())
def test_excessive_length_sheet_name(self): r = TestReader( ('X'*32,([['S1R0C0']]),), ) book = tuple(r.get_workbooks())[0][0] # fire methods on writer should_raise(r,ValueError( 'Sheet name cannot be more than 31 characters long, ' 'supplied name was 32 characters long!' ))(TestWriter())
def test_bogus_sheet_name(self): r = TestReader( ('sheet',([['S1R0C0']]),), ('Sheet',([['S2R0C0']]),), ) book = tuple(r.get_workbooks())[0][0] # fire methods on writer should_raise(r,ValueError( "A sheet named 'sheet' has already been added!" ))(TestWriter())
def test_wrong_exception_type(self): expected_exception = ValueError('bar') def to_test(): raise expected_exception try: should_raise(KeyError('foo'))(to_test)() except ValueError as actual_exception: assert actual_exception is expected_exception else: # pragma: no cover self.fail(('Wrong exception raised'))
def test_no_supplied_or_raised(self): def to_test(): pass try: should_raise()(to_test)() except AssertionError as e: self.assertEqual(e, C(AssertionError('No exception raised!'))) else: self.fail('No exception raised!')
def test_wrong_exception(self): def to_test(): raise ValueError('bar') try: should_raise(to_test,ValueError('foo'))() except AssertionError,e: self.assertEqual( e, C(AssertionError("ValueError('bar',) raised, ValueError('foo',) expected")) )
def test_no_exception(self): def to_test(): pass try: should_raise(to_test,ValueError())() except AssertionError,e: self.assertEqual( e, C(AssertionError('None raised, ValueError() expected')) )
def test_method_both(self): class X: def to_test(self, *args, **kw): self.args = args self.kw = kw raise ValueError() x = X() should_raise(ValueError)(x.to_test)(1, y=2) self.assertEqual(x.args, (1, )) self.assertEqual(x.kw, {'y': 2})
def test_not_there(self): o = object() @replace('testfixtures.tests.sample1.bad', o) def test_something(r): pass # pragma: no cover should_raise(test_something, AttributeError("Original 'bad' not found"))()
def test_wrong_exception_class(self): def to_test(): raise ValueError('bar') if PY3: message = ("<class 'KeyError'> (expected) != " "ValueError('bar',) (raised)") else: message = ("<type 'exceptions.KeyError'> (expected) != " "ValueError('bar',) (raised)") with ShouldAssert(message): should_raise(KeyError)(to_test)()
def test_no_exception(self): def to_test(): pass try: should_raise(ValueError())(to_test)() except AssertionError as e: self.assertEqual( e, C(AssertionError('None raised, ValueError() expected'))) else: self.fail('No exception raised!')
def test_wrong_exception(self): def to_test(): raise ValueError('bar') try: should_raise(ValueError('foo'))(to_test)() except AssertionError as e: self.assertEqual( e, C(AssertionError("ValueError('bar',) raised, ValueError('foo',) expected")) ) else: self.fail('No exception raised!')
def test_no_exception(self): def to_test(): pass try: should_raise(ValueError())(to_test)() except AssertionError as e: self.assertEqual( e, C(AssertionError('None raised, ValueError() expected')) ) else: self.fail('No exception raised!')
def test_no_supplied_or_raised(self): # effectvely we're saying "something should be raised!" # but we want to inspect s.raised rather than making # an up-front assertion def to_test(): pass try: should_raise(to_test)() except AssertionError,e: self.assertEqual( e, C(AssertionError("No exception raised!")) )
def test_no_supplied_or_raised(self): # effectvely we're saying "something should be raised!" # but we want to inspect s.raised rather than making # an up-front assertion def to_test(): pass try: should_raise()(to_test)() except AssertionError as e: self.assertEqual(e, C(AssertionError("No exception raised!"))) else: self.fail('No exception raised!')
def test_wrong_exception(self): def to_test(): raise ValueError('bar') try: should_raise(ValueError('foo'))(to_test)() except AssertionError as e: self.assertEqual( e, C(AssertionError( "ValueError('bar',) raised, ValueError('foo',) expected" ))) else: self.fail('No exception raised!')
def test_wrong_exception_class(self): def to_test(): raise ValueError('bar') if PY_37_PLUS: message = ("<class 'KeyError'> (expected) != " "ValueError('bar') (raised)") elif PY3: message = ("<class 'KeyError'> (expected) != " "ValueError('bar',) (raised)") else: message = ("<type 'exceptions.KeyError'> (expected) != " "ValueError('bar',) (raised)") with ShouldAssert(message): should_raise(KeyError)(to_test)()
def test_gotcha_import(self): # standard `replace` caveat, make sure you # patch all revelent places where datetime # has been imported: @replace('datetime.datetime',test_datetime()) def test_something(): from datetime import datetime compare(datetime.now(),d(2001,1,1,0,0,0)) compare(sample1.str_now_1(),'2001-01-01 00:00:10') s = should_raise(test_something,AssertionError) s() # This convoluted check is because we can't stub # out the datetime, since we're testing stubbing out # the datetime ;-) j,dt1,j,dt2,j = s.raised.args[0].split("'") if '.' in dt1: dt1,ms = dt1.split('.') # check ms is just an int int(ms) # check we can parse the date dt1 = strptime(dt1,'%Y-%m-%d %H:%M:%S') # check the dt2 bit was as it should be compare(dt2,'2001-01-01 00:00:10') # What you need to do is replace the imported type: @replace('testfixtures.tests.sample1.datetime',test_datetime()) def test_something(): compare(sample1.str_now_1(),'2001-01-01 00:00:00') test_something()
def test_raises(self): def test_z(): return 'replacement z' compare(sample1.z(), 'original z') @replace('testfixtures.tests.sample1.z', test_z) def test_something(): compare(sample1.z(), 'replacement z') raise Exception() compare(sample1.z(), 'original z') should_raise(test_something)() compare(sample1.z(), 'original z')
def test_multiple_names_for_xfi_bad_2(self): self.wb.style_name_map = { 'A':(0,0), '':(0,0), } styles = should_raise(Styles,AssertionError) styles(self.wb)
def test_gotcha_import(self): # standard `replace` caveat, make sure you # patch all revelent places where time # has been imported: @replace('time.time',test_time()) def test_something(): from time import time compare(time(),978307200.0) compare(sample1.str_time(),'978307201.0') s = should_raise(test_something,AssertionError) s() # This convoluted check is because we can't stub # out time, since we're testing stubbing out time ;-) j,t1,j,t2,j = s.raised.args[0].split("'") # check we can parse the time t1 = float(t1) # check the t2 bit was as it should be compare(t2,'978307201.0') # What you need to do is replace the imported type: @replace('testfixtures.tests.sample1.time',test_time()) def test_something(): compare(sample1.str_time(),'978307200.0') test_something()
def test_multiple_names_for_xfi_bad_2(self): self.wb.style_name_map = { 'A': (0, 0), '': (0, 0), } styles = should_raise(Styles, AssertionError) styles(self.wb)
def test_gotcha_import_and_obtain(self): # Another gotcha is where people have locally obtained # a class attributes, where the normal patching doesn't # work: @replace('testfixtures.tests.sample1.datetime',test_datetime()) def test_something(): compare(sample1.str_now_2(),'2001-01-01 00:00:00') s = should_raise(test_something,AssertionError) s() # This convoluted check is because we can't stub # out the datetime, since we're testing stubbing out # the datetime ;-) j,dt1,j,dt2,j = s.raised.args[0].split("'") if '.' in dt1: dt1,ms = dt1.split('.') # check ms is just an int int(ms) # check we can parse the date dt1 = strptime(dt1,'%Y-%m-%d %H:%M:%S') # check the dt2 bit was as it should be compare(dt2,'2001-01-01 00:00:00') # What you need to do is replace the imported name: @replace('testfixtures.tests.sample1.now',test_datetime().now) def test_something(): compare(sample1.str_now_2(),'2001-01-01 00:00:00') test_something()
def test_raised(self): def to_test(): raise ValueError('wrong value supplied') s = should_raise(to_test) s() self.assertEqual(s.raised, C(ValueError('wrong value supplied')))
def test_gotcha_import(self): # standard `replace` caveat, make sure you # patch all revelent places where date # has been imported: @replace('datetime.date', test_date()) def test_something(): from datetime import date compare(date.today(), d(2001, 1, 1)) compare(sample1.str_today_1(), '2001-01-02') s = should_raise(test_something, AssertionError) s() # This convoluted check is because we can't stub # out the date, since we're testing stubbing out # the date ;-) j, dt1, j, dt2, j = s.raised.args[0].split("'") # check we can parse the date dt1 = strptime(dt1, '%Y-%m-%d') # check the dt2 bit was as it should be compare(dt2, '2001-01-02') # What you need to do is replace the imported type: @replace('testfixtures.tests.sample1.date', test_date()) def test_something(): compare(sample1.str_today_1(), '2001-01-01') test_something()
def test_gotcha_import_and_obtain(self): # Another gotcha is where people have locally obtained # a class attributes, where the normal patching doesn't # work: @replace('testfixtures.tests.sample1.date', test_date()) def test_something(): compare(sample1.str_today_2(), '2001-01-01') s = should_raise(test_something, AssertionError) s() # This convoluted check is because we can't stub # out the date, since we're testing stubbing out # the date ;-) j, dt1, j, dt2, j = s.raised.args[0].split("'") # check we can parse the date dt1 = strptime(dt1, '%Y-%m-%d') # check the dt2 bit was as it should be compare(dt2, '2001-01-01') # What you need to do is replace the imported name: @replace('testfixtures.tests.sample1.today', test_date().today) def test_something(): compare(sample1.str_today_2(), '2001-01-01') test_something()
def test_raises(self): def test_z(): return "replacement z" compare(sample1.z(), "original z") @replace("testfixtures.tests.sample1.z", test_z) def test_something(): compare(sample1.z(), "replacement z") raise Exception() compare(sample1.z(), "original z") should_raise(test_something)() compare(sample1.z(), "original z")
def test_check_all_with_slash_at_start(self): with TempDirectory() as d: checkall = should_raise( d.check_all, ValueError( 'Attempt to read or write outside the temporary Directory') ) checkall('/some/folder')
def test_write_with_slash_at_start(self): with TempDirectory() as d: write = should_raise( d.write, ValueError( 'Attempt to read or write outside the temporary Directory') ) write('/some/folder', 'stuff')
def test_read_with_slash_at_start(self): with TempDirectory() as d: read = should_raise( d.read, ValueError( 'Attempt to read or write outside the temporary Directory') ) read('/some/folder')
def test_listdir_with_slash_at_start(self): with TempDirectory() as d: listdir = should_raise( d.listdir, ValueError( 'Attempt to read or write outside the temporary Directory') ) listdir('/some/folder')
def test_return(self): # return of a should_raise is always None! def to_test(): raise ValueError('wrong value supplied') s = should_raise(to_test) r = s() self.assertEqual(s.raised,C(ValueError('wrong value supplied'))) self.failUnless(r is None)
def test_raises(self): def test_z(): return 'replacement z' compare(sample1.z(),'original z') @replace('testfixtures.tests.sample1.z',test_z) def test_something(): compare(sample1.z(),'replacement z') raise Exception() compare(sample1.z(),'original z') should_raise(test_something)() compare(sample1.z(),'original z')
def test_class_class(self): class Test: def __init__(self, x): # The TypeError is raised due to the mis-matched parameters # so the pass never gets executed pass # pragma: no cover r = should_raise(Test, TypeError)() self.assertEqual(r, None)
def test_return(self): # return of a should_raise is always None! def to_test(): raise ValueError('wrong value supplied') s = should_raise(to_test) r = s() self.assertEqual(s.raised, C(ValueError('wrong value supplied'))) self.failUnless(r is None)
def test_not_same(self,d): d.write('something', b'stuff') check = should_raise(d.check,AssertionError( "Sequence not as expected:\n\nsame:\n()\n\nfirst:\n('.svn', 'something')\n\nsecond:\n('something',)" )) check( '.svn', 'something', )
def test_both(self): def to_test(*args, **kw): raise ValueError('%r %r' % (args, kw)) should_raise(ValueError("(1,) {'x': 2}"))(to_test)(1, x=2)
def test_kw(self): def to_test(**kw): raise ValueError('%r' % kw) should_raise(ValueError("{'x': 1}"))(to_test)(x=1)
def test_no_params(self): def to_test(): raise ValueError('wrong value supplied') should_raise(ValueError('wrong value supplied'))(to_test)()
def test_kw_to_args(self): def to_test(x): raise ValueError('%s' % x) should_raise(ValueError('1'))(to_test)(x=1)
def test_args(self): def to_test(*args): raise ValueError('%s' % repr(args)) should_raise(ValueError('(1,)'))(to_test)(1)
def test_only_exception_class(self): def to_test(): raise ValueError('bar') should_raise(ValueError)(to_test)()