How to use remove_non_data_nodes_and_assert method in Robotframework

Best Python code snippet using robotframework

test_statements_in_invalid_position.py

Source:test_statements_in_invalid_position.py Github

copy

Full Screen

1import unittest2from robot.parsing import get_model, Token3from robot.parsing.model.statements import ReturnStatement, Break, Continue4from parsing_test_utils import assert_model, RemoveNonDataTokensVisitor5def remove_non_data_nodes_and_assert(node, expected, data_only):6 if not data_only:7 RemoveNonDataTokensVisitor().visit(node)8 assert_model(node, expected)9class TestReturn(unittest.TestCase):10 def test_in_test_case_body(self):11 for data_only in [True, False]:12 with self.subTest(data_only=data_only):13 model = get_model('''\14*** Test Cases ***15Example16 RETURN''', data_only=data_only)17 node = model.sections[0].body[0].body[0]18 expected = ReturnStatement(19 [Token(Token.RETURN_STATEMENT, 'RETURN', 3, 4)],20 errors=('RETURN can only be used inside a user keyword.',)21 )22 remove_non_data_nodes_and_assert(node, expected, data_only)23 def test_in_test_case_body_inside_for(self):24 for data_only in [True, False]:25 with self.subTest(data_only=data_only):26 model = get_model('''\27*** Test Cases ***28Example29 FOR ${i} IN 1 230 RETURN31 END32 ''', data_only=data_only)33 node = model.sections[0].body[0].body[0].body[0]34 expected = ReturnStatement(35 [Token(Token.RETURN_STATEMENT, 'RETURN', 4, 8)],36 errors=('RETURN can only be used inside a user keyword.',)37 )38 remove_non_data_nodes_and_assert(node, expected, data_only)39 def test_in_test_case_body_inside_while(self):40 for data_only in [True, False]:41 with self.subTest(data_only=data_only):42 model = get_model('''\43*** Test Cases ***44Example45 WHILE True46 RETURN47 END48 ''', data_only=data_only)49 node = model.sections[0].body[0].body[0].body[0]50 expected = ReturnStatement(51 [Token(Token.RETURN_STATEMENT, 'RETURN', 4, 8)],52 errors=('RETURN can only be used inside a user keyword.',)53 )54 remove_non_data_nodes_and_assert(node, expected, data_only)55 def test_in_test_case_body_inside_if_else(self):56 for data_only in [True, False]:57 with self.subTest(data_only=data_only):58 model = get_model('''\59*** Test Cases ***60Example61 IF True62 RETURN63 ELSE IF False64 RETURN65 ELSE66 RETURN67 END68 ''', data_only=data_only)69 ifroot = model.sections[0].body[0].body[0]70 node = ifroot.body[0]71 expected = ReturnStatement(72 [Token(Token.RETURN_STATEMENT, 'RETURN', 4, 8)],73 errors=('RETURN can only be used inside a user keyword.',)74 )75 remove_non_data_nodes_and_assert(node, expected, data_only)76 expected.tokens[0].lineno = 677 remove_non_data_nodes_and_assert(ifroot.orelse.body[0], expected, data_only)78 expected.tokens[0].lineno = 879 remove_non_data_nodes_and_assert(ifroot.orelse.orelse.body[0], expected, data_only)80 def test_in_test_case_body_inside_try_except(self):81 for data_only in [True, False]:82 with self.subTest(data_only=data_only):83 model = get_model('''\84*** Test Cases ***85Example86 TRY87 RETURN88 EXCEPT89 RETURN90 ELSE91 RETURN92 FINALLY93 RETURN94 END95 ''', data_only=data_only)96 tryroot = model.sections[0].body[0].body[0]97 node = tryroot.body[0]98 expected = ReturnStatement(99 [Token(Token.RETURN_STATEMENT, 'RETURN', 4, 8)],100 errors=('RETURN can only be used inside a user keyword.',)101 )102 remove_non_data_nodes_and_assert(node, expected, data_only)103 expected.tokens[0].lineno = 6104 remove_non_data_nodes_and_assert(tryroot.next.body[0], expected, data_only)105 expected.tokens[0].lineno = 8106 remove_non_data_nodes_and_assert(tryroot.next.next.body[0], expected, data_only)107 expected.tokens[0].lineno = 10108 remove_non_data_nodes_and_assert(tryroot.next.next.next.body[0], expected, data_only)109 def test_in_finally_in_uk(self):110 for data_only in [True, False]:111 with self.subTest(data_only=data_only):112 model = get_model('''\113*** Keywords ***114Example115 TRY116 No operation117 EXCEPT118 No operation119 FINALLY120 RETURN121 END122 ''', data_only=data_only)123 node = model.sections[0].body[0].body[0].next.next.body[0]124 expected = ReturnStatement(125 [Token(Token.RETURN_STATEMENT, 'RETURN', 8, 8)],126 errors=('RETURN cannot be used in FINALLY branch.',)127 )128 remove_non_data_nodes_and_assert(node, expected, data_only)129 def test_in_nested_finally_in_uk(self):130 for data_only in [True, False]:131 with self.subTest(data_only=data_only):132 model = get_model('''\133*** Keywords ***134Example135 IF True136 TRY137 No operation138 EXCEPT139 No operation140 FINALLY141 RETURN142 END143 END''', data_only=data_only)144 node = model.sections[0].body[0].body[0].body[0].next.next.body[0]145 expected = ReturnStatement(146 [Token(Token.RETURN_STATEMENT, 'RETURN', 9, 12)],147 errors=('RETURN cannot be used in FINALLY branch.',)148 )149 remove_non_data_nodes_and_assert(node, expected, data_only)150class TestBreak(unittest.TestCase):151 def test_in_test_case_body(self):152 for data_only in [True, False]:153 with self.subTest(data_only=data_only):154 model = get_model('''\155*** Test Cases ***156Example157 BREAK''', data_only=data_only)158 node = model.sections[0].body[0].body[0]159 expected = Break(160 [Token(Token.BREAK, 'BREAK', 3, 4)],161 errors=('BREAK can only be used inside a loop.',)162 )163 remove_non_data_nodes_and_assert(node, expected, data_only)164 def test_in_if_test_case_body(self):165 for data_only in [True, False]:166 with self.subTest(data_only=data_only):167 model = get_model('''\168*** Test Cases ***169Example170 IF True171 BREAK172 END''', data_only=data_only)173 node = model.sections[0].body[0].body[0].body[0]174 expected = Break(175 [Token(Token.BREAK, 'BREAK', 4, 8)],176 errors=('BREAK can only be used inside a loop.',)177 )178 remove_non_data_nodes_and_assert(node, expected, data_only)179 def test_in_try_test_case_body(self):180 for data_only in [True, False]:181 with self.subTest(data_only=data_only):182 model = get_model('''\183*** Test Cases ***184Example185 TRY 186 BREAK187 EXCEPT188 no operation189 END''', data_only=data_only)190 node = model.sections[0].body[0].body[0].body[0]191 expected = Break(192 [Token(Token.BREAK, 'BREAK', 4, 8)],193 errors=('BREAK can only be used inside a loop.',)194 )195 remove_non_data_nodes_and_assert(node, expected, data_only)196 def test_in_finally_inside_loop(self):197 for data_only in [True, False]:198 with self.subTest(data_only=data_only):199 model = get_model('''\200*** Test Cases ***201Example202 WHILE True203 TRY 204 Fail205 EXCEPT206 no operation207 FINALLY208 BREAK209 END 210 END''', data_only=data_only)211 node = model.sections[0].body[0].body[0].body[0].next.next.body[0]212 expected = Break(213 [Token(Token.BREAK, 'BREAK', 9, 11)],214 errors=('BREAK cannot be used in FINALLY branch.',)215 )216 remove_non_data_nodes_and_assert(node, expected, data_only)217 def test_in_uk_body(self):218 for data_only in [True, False]:219 with self.subTest(data_only=data_only):220 model = get_model('''\221*** Keywords ***222Example223 BREAK''', data_only=data_only)224 node = model.sections[0].body[0].body[0]225 expected = Break(226 [Token(Token.BREAK, 'BREAK', 3, 4)],227 errors=('BREAK can only be used inside a loop.',)228 )229 remove_non_data_nodes_and_assert(node, expected, data_only)230 def test_in_if_uk_body(self):231 for data_only in [True, False]:232 with self.subTest(data_only=data_only):233 model = get_model('''\234*** Keywords ***235Example236 IF True237 BREAK238 END''', data_only=data_only)239 node = model.sections[0].body[0].body[0].body[0]240 expected = Break(241 [Token(Token.BREAK, 'BREAK', 4, 8)],242 errors=('BREAK can only be used inside a loop.',)243 )244 remove_non_data_nodes_and_assert(node, expected, data_only)245 def test_in_try_uk_body(self):246 for data_only in [True, False]:247 with self.subTest(data_only=data_only):248 model = get_model('''\249*** Keywords ***250Example251 TRY 252 BREAK253 EXCEPT254 no operation255 END''', data_only=data_only)256 node = model.sections[0].body[0].body[0].body[0]257 expected = Break(258 [Token(Token.BREAK, 'BREAK', 4, 8)],259 errors=('BREAK can only be used inside a loop.',)260 )261 remove_non_data_nodes_and_assert(node, expected, data_only)262class TestContinue(unittest.TestCase):263 def test_in_test_case_body(self):264 for data_only in [True, False]:265 with self.subTest(data_only=data_only):266 model = get_model('''\267*** Test Cases ***268Example269 CONTINUE''', data_only=data_only)270 node = model.sections[0].body[0].body[0]271 expected = Continue(272 [Token(Token.CONTINUE, 'CONTINUE', 3, 4)],273 errors=('CONTINUE can only be used inside a loop.',)274 )275 remove_non_data_nodes_and_assert(node, expected, data_only)276 def test_in_if_test_case_body(self):277 for data_only in [True, False]:278 with self.subTest(data_only=data_only):279 model = get_model('''\280*** Test Cases ***281Example282 IF True283 CONTINUE284 END''', data_only=data_only)285 node = model.sections[0].body[0].body[0].body[0]286 expected = Continue(287 [Token(Token.CONTINUE, 'CONTINUE', 4, 8)],288 errors=('CONTINUE can only be used inside a loop.',)289 )290 remove_non_data_nodes_and_assert(node, expected, data_only)291 def test_in_try_test_case_body(self):292 for data_only in [True, False]:293 with self.subTest(data_only=data_only):294 model = get_model('''\295*** Test Cases ***296Example297 TRY 298 CONTINUE299 EXCEPT300 no operation301 END''', data_only=data_only)302 node = model.sections[0].body[0].body[0].body[0]303 expected = Continue(304 [Token(Token.CONTINUE, 'CONTINUE', 4, 8)],305 errors=('CONTINUE can only be used inside a loop.',)306 )307 remove_non_data_nodes_and_assert(node, expected, data_only)308 def test_in_finally_inside_loop(self):309 for data_only in [True, False]:310 with self.subTest(data_only=data_only):311 model = get_model('''\312*** Test Cases ***313Example314 WHILE True315 TRY 316 Fail317 EXCEPT318 no operation319 FINALLY320 CONTINUE321 END 322 END''', data_only=data_only)323 node = model.sections[0].body[0].body[0].body[0].next.next.body[0]324 expected = Continue(325 [Token(Token.CONTINUE, 'CONTINUE', 9, 11)],326 errors=('CONTINUE cannot be used in FINALLY branch.',)327 )328 remove_non_data_nodes_and_assert(node, expected, data_only)329 def test_in_uk_body(self):330 for data_only in [True, False]:331 with self.subTest(data_only=data_only):332 model = get_model('''\333*** Keywords ***334Example335 CONTINUE''', data_only=data_only)336 node = model.sections[0].body[0].body[0]337 expected = Continue(338 [Token(Token.CONTINUE, 'CONTINUE', 3, 4)],339 errors=('CONTINUE can only be used inside a loop.',)340 )341 remove_non_data_nodes_and_assert(node, expected, data_only)342 def test_in_if_uk_body(self):343 for data_only in [True, False]:344 with self.subTest(data_only=data_only):345 model = get_model('''\346*** Keywords ***347Example348 IF True349 CONTINUE350 END''', data_only=data_only)351 node = model.sections[0].body[0].body[0].body[0]352 expected = Continue(353 [Token(Token.CONTINUE, 'CONTINUE', 4, 8)],354 errors=('CONTINUE can only be used inside a loop.',)355 )356 remove_non_data_nodes_and_assert(node, expected, data_only)357 def test_in_try_uk_body(self):358 for data_only in [True, False]:359 with self.subTest(data_only=data_only):360 model = get_model('''\361*** Keywords ***362Example363 TRY 364 CONTINUE365 EXCEPT366 no operation367 END''', data_only=data_only)368 node = model.sections[0].body[0].body[0].body[0]369 expected = Continue(370 [Token(Token.CONTINUE, 'CONTINUE', 4, 8)],371 errors=('CONTINUE can only be used inside a loop.',)372 )373 remove_non_data_nodes_and_assert(node, expected, data_only)374if __name__ == '__main__':...

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 Robotframework 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