How to use test_traceback method in assertpy

Best Python code snippet using assertpy_python

test_traceback.py

Source:test_traceback.py Github

copy

Full Screen

1#####################################################################################2#3# Copyright (c) Microsoft Corporation. All rights reserved.4#5# This source code is subject to terms and conditions of the Microsoft Public License. A6# copy of the license can be found in the License.html file at the root of this distribution. If7# you cannot locate the Microsoft Public License, please send an email to8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound9# by the terms of the Microsoft Public License.10#11# You must not remove this notice, or any other, from this software.12#13#14#####################################################################################15# !!! DO NOT MOVE OR CHANGE THE FOLLOWING LINES16def _raise_exception():17 raise Exception()18_retb = (18, 0, 'test_traceback.py', '_raise_exception')19from iptest.assert_util import *20from iptest.file_util import *21if not is_cli: import os22def _raise_exception_with_finally():23 try:24 raise Exception()25 finally:26 pass27_rewftb= (27, 0, 'test_traceback.py', '_raise_exception_with_finally')28def assert_traceback(expected):29 import sys30 tb = sys.exc_info()[2]31 32 if expected is None:33 AreEqual(None, expected)34 else:35 tb_list = []36 while tb is not None :37 f = tb.tb_frame38 co = f.f_code39 filename = co.co_filename.lower()40 name = co.co_name41 tb_list.append((tb.tb_lineno, tb.tb_lasti, filename, name))42 tb = tb.tb_next43 44 #print tb_list45 46 AreEqual(len(tb_list), len(expected))47 48 for x in range(len(expected)):49 AreEqual(tb_list[x][0], expected[x][0])50 AreEqual(tb_list[x][2:], expected[x][2:])51 52def test_no_traceback():53 #assert_traceback(None)54 try:55 _raise_exception()56 except:57 pass58 assert_traceback(None)59FILE="test_traceback.py"60LINE100 = 7061def test_catch_others_exception():62 try:63 _raise_exception()64 except:65 assert_traceback([(LINE100 + 2, 0, FILE, 'test_catch_others_exception'), _retb])66LINE110 = 7867def test_catch_its_own_exception():68 try:69 raise Exception()70 except:71 assert_traceback([(LINE110 + 2, 0, FILE, 'test_catch_its_own_exception')])72LINE120 = 8673def test_catch_others_exception_with_finally():74 try:75 _raise_exception_with_finally()76 except:77 assert_traceback([(LINE120 + 2, 0, FILE, 'test_catch_others_exception_with_finally'), _rewftb])78LINE130 = 9479def test_nested_caught_outside():80 try:81 x = 282 try:83 _raise_exception()84 except NameError:85 Assert(False, "unhittable")86 y = 287 except:88 assert_traceback([(LINE130 + 4, 0, FILE, 'test_nested_caught_outside'), _retb])89LINE140 = 10890def test_nested_caught_inside():91 try:92 x = 293 try:94 _raise_exception()95 except:96 assert_traceback([(LINE140 + 4, 0, FILE, 'test_nested_caught_inside'), _retb])97 y = 298 except:99 assert_traceback(None)100LINE150 = 120101def test_throw_in_except():102 try:103 _raise_exception()104 except:105 assert_traceback([(LINE150+2, 0, FILE, 'test_throw_in_except'), _retb])106 try:107 assert_traceback([(LINE150+2, 0, FILE, 'test_throw_in_except'), _retb])108 _raise_exception()109 except:110 assert_traceback([(LINE150+7, 0, FILE, 'test_throw_in_except'), _retb])111 assert_traceback([(LINE150+7, 0, FILE, 'test_throw_in_except'), _retb])112LINE160 = 134113class C1:114 def M(self):115 try:116 _raise_exception()117 except:118 assert_traceback([(LINE160 + 3, 0, FILE, 'M'), _retb])119def test_throw_in_method():120 c = C1()121 c.M()122LINE170 = 147123def test_throw_when_defining_class():124 class C2(object):125 try:126 _raise_exception()127 except:128 assert_traceback([(LINE170 + 3, 0, FILE, 'C2'), _retb])129def throw_when_defining_class_directly():130 class C3(C1):131 _raise_exception()132LINE180 = 160133def test_throw_when_defining_class_directly():134 try:135 throw_when_defining_class_directly()136 except:137 assert_traceback([(LINE180 + 2, 0, FILE, 'test_throw_when_defining_class_directly'),138 (LINE180 - 5, 0, FILE, 'throw_when_defining_class_directly'),139 (LINE180 - 4, 0, FILE, 'C3'), _retb])140LINE200 = 169141def test_compiled_code():142 try:143 codeobj = compile('\nraise Exception()', '<mycode>', 'exec')144 exec(codeobj, {})145 except:146 assert_traceback([(LINE200+3, 0, FILE, 'test_compiled_code'), (2, 0, '<mycode>', '<module>')])147def generator_throw_before_yield():148 _raise_exception()149 yield 1150 151LINE210 = 181152def test_throw_before_yield():153 try:154 for x in generator_throw_before_yield():155 pass156 except:157 assert_traceback([(LINE210+3, 0, FILE, 'test_throw_before_yield'), (LINE210-4, 2, 'test_traceback.py', 'generator_throw_before_yield'), _retb])158def generator_throw_after_yield():159 yield 1160 _raise_exception()161LINE220 = 194162def test_throw_while_yield():163 try:164 for x in generator_throw_while_yield():165 pass166 except:167 assert_traceback([(LINE220+3, 0, FILE, 'test_throw_while_yield')])168def generator_yield_inside_try():169 try:170 yield 1171 yield 2172 _raise_exception()173 except NameError:174 pass175LINE230 = 211176def test_yield_inside_try():177 try:178 for x in generator_yield_inside_try():179 pass180 except:181 assert_traceback([(LINE230+3, 0, FILE, 'test_yield_inside_try'), (LINE230-5, 2, 'test_traceback.py', 'generator_yield_inside_try'), _retb])182LINE240 = 221183def test_throw_and_throw():184 try:185 _raise_exception()186 except:187 assert_traceback([(LINE240 + 2, 0, FILE, 'test_throw_and_throw'), _retb])188 try:189 _raise_exception()190 except:191 assert_traceback([(LINE240 + 6, 0, FILE, 'test_throw_and_throw'), _retb])192LINE250 = 233193def test_throw_in_another_file():194 if is_cli: _f_file = path_combine(get_full_dir_name(testpath.public_testdir), 'foo.py')195 else: _f_file = os.getcwd() + '\\foo.py'196 write_to_file(_f_file, '''197def another_raise():198 raise Exception()199''');200 try:201 import foo202 foo.another_raise()203 except:204 assert_traceback([(LINE250 + 8, 0, FILE, 'test_throw_in_another_file'), (3, 0, _f_file.lower(), 'another_raise')])205 finally:206 nt.remove(_f_file)207class MyException(Exception): pass208Line260 = 250209def catch_MyException():210 try:211 _raise_exception()212 except MyException:213 assert_traceback([]) # UNREACABLE. THIS TRICK SIMPLIFIES THE CHECK214def test_catch_MyException():215 try:216 catch_MyException()217 except:218 assert_traceback([(Line260+8, 0, FILE, 'test_catch_MyException'), (Line260+2, 0, FILE, 'catch_MyException'), _retb])219Line263 = 263220@skip("silverlight")221def test_cp11923_first():222 try:223 _t_test = testpath.public_testdir + "\\cp11923.py"224 write_to_file(_t_test, """def f():225 x = 'something bad'226 raise Exception(x)""")227 228 import cp11923229 for i in xrange(3):230 try:231 cp11923.f()232 except:233 assert_traceback([(Line263 + 11, 69, 'test_traceback.py', 'test_cp11923_first'), (3, 22, get_full_dir_name(_t_test).lower(), 'f')])234 reload(cp11923)235 236 finally:237 import nt238 nt.unlink(_t_test)239###############################################################################240##TESTS BEYOND THIS POINT SHOULD NOT DEPEND ON LINE NUMBERS IN THIS FILE#######241###############################################################################242@skip("silverlight")243def test_cp11923_second():244 import nt245 import sys246 old_path = [x for x in sys.path]247 sys.path.append(nt.getcwd())248 249 try:250 #Test setup251 _t_test = testpath.public_testdir + "\\cp11116_main.py"252 write_to_file(_t_test, """import cp11116_a253try:254 cp11116_a.a()255except:256 pass257cp11116_a.a()258""")259 260 _t_test_a = testpath.public_testdir + "\\cp11116_a.py"261 write_to_file(_t_test_a, """def a():262 raise None263""") 264 265 #Actual test266 t_out, t_in, t_err = nt.popen3(sys.executable + " " + nt.getcwd() + r"\cp11116_main.py")267 lines = t_err.readlines()268 t_err.close()269 t_out.close()270 t_in.close()271 272 #Verification273 Assert("cp11116_main.py\", line 7, in" in lines[1], lines[1])274 line_num = 3275 if is_cli:276 line_num -= 1277 Assert(lines[line_num].rstrip().endswith("cp11116_a.py\", line 2, in a"), lines[line_num])278 279 finally:280 sys.path = old_path281 nt.unlink(_t_test)282 nt.unlink(_t_test_a)283Line331 = 332284def test_reraise():285 def g():286 f()287 288 def f():289 try:290 raise Exception291 except:292 raise293 try:294 g()295 except:296 assert_traceback([(Line331+9, 0, 'test_traceback.py', 'test_reraise'), (Line331, 0, 'test_traceback.py', 'g'), (Line331+4, 0, 'test_traceback.py', 'f')])297def test_reraise_finally():298 def g():299 f()300 301 def f():302 try:303 raise Exception304 finally:305 raise306 try:307 g()308 except:309 assert_traceback([(Line331+25, 30, 'test_traceback.py', 'test_reraise_finally'), (Line331+16, 3, 'test_traceback.py', 'g'), (Line331+22,13, 'test_traceback.py', 'f')])310Line361 = 361311def test_xafter_finally_raise():312 def g():313 raise Exception314 315 def nop(): pass316 317 def f():318 try:319 nop()320 finally:321 nop()322 323 try:324 g()325 except Exception, e:326 assert_traceback([(Line361+14, 30, 'test_traceback.py', 'f'), (Line361+3, 3, 'test_traceback.py', 'g')])327 f()328Line381 = 381329def test_uncaught_exception_thru_try():330 def baz():331 raise StopIteration332 333 def f():334 try:335 baz()336 except TypeError:337 pass338 try:339 f()340 except:341 assert_traceback([(Line381+11, 30, 'test_traceback.py', 'test_uncaught_exception_thru_try'), (Line381+7, 3, 'test_traceback.py', 'f'), (Line381+3, 3, 'test_traceback.py', 'baz')])342Line397=397343def test_with_traceback():344 from thread import allocate_lock345 def f():346 g()347 348 def g():349 h()350 351 def h():352 raise Exception('hello!!')353 354 try:355 with allocate_lock():356 f()357 except:358 assert_traceback([(Line397+14, 30, 'test_traceback.py', 'test_with_traceback'), 359 (Line397+4, 3, 'test_traceback.py', 'f'), 360 (Line397+7, 3, 'test_traceback.py', 'g'),361 (Line397+10, 3, 'test_traceback.py', 'h')])362 363Line419=419364def test_xraise_again():365 def f():366 g()367 368 def g():369 h()370 371 def h():372 raise Exception('hello!!')373 374 try:375 try:376 f()377 except Exception, e:378 raise e379 except:380 assert_traceback([(Line419+15, 30, 'test_traceback.py', 'test_xraise_again'), ])381Line438=438382def test_with_traceback_enter_throws():383 class ctx_mgr(object):384 def __enter__(*args):385 raise Exception('hello')386 def __exit__(*args):387 pass388 389 def h():390 raise Exception('hello!!')391 392 393 try:394 with ctx_mgr():395 h()396 except:397 assert_traceback([(Line438+13, 30, 'test_traceback.py', 'test_with_traceback_enter_throws'), 398 (Line438+4, 3, 'test_traceback.py', '__enter__')])399 400Line457=457401def test_with_traceback_exit_throws():402 class ctx_mgr(object):403 def __enter__(*args):404 pass405 def __exit__(*args):406 raise Exception('hello')407 408 def h():409 raise Exception('hello!!')410 411 try:412 with ctx_mgr():413 h()414 except:415 assert_traceback([(Line457+13, 30, 'test_traceback.py', 'test_with_traceback_exit_throws'), 416 (Line457+6, 3, 'test_traceback.py', '__exit__')])417Line475=475418def test_with_traceback_ctor_throws():419 class ctx_mgr(object):420 def __init__(self):421 raise Exception('hello')422 def __enter__(*args):423 pass424 def __exit__(*args):425 pass426 427 def h():428 raise Exception('hello!!')429 430 try:431 with ctx_mgr():432 h()433 except:434 assert_traceback([(Line475+14, 30, 'test_traceback.py', 'test_with_traceback_ctor_throws'), 435 (Line475+4, 3, 'test_traceback.py', '__init__')])436Line496=496437def test_with_mixed_stack():438 """tests a stack which is mixed w/ interpreted and non-interpreted frames439because f() has a loop in it"""440 def a():441 with xxx() as abc:442 f()443 444 def f():445 for z in ():446 pass447 448 1/0449 450 class xxx(object):451 def __enter__(*args): pass452 def __exit__(*args): pass453 454 try:455 a()456 except:457 assert_traceback([(Line496+19, 30, 'test_traceback.py', 'test_with_mixed_stack'), 458 (Line496+6, 3, 'test_traceback.py', 'a'),459 (Line496+12, 3, 'test_traceback.py', 'f')])460 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run assertpy automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful