How to use teamcity_parse method in Contexts

Best Python code snippet using Contexts

teamcity_tests.py

Source:teamcity_tests.py Github

copy

Full Screen

...14 self.outputs = []15 def parse_line(self, n):16 if n < 0: # to hide the fact that the last line will be empty17 n -= 118 return teamcity_parse(self.stringio.getvalue().split('\n')[n])19class WhenLocatingThePlugin:20 def when_framework_asks_where_the_plugin_wants_to_be(self):21 self.result = teamcity.TeamCityReporter.locate()22 def it_should_not_override_the_argv_forwarder(self):23 assert self.result[0] == argv_forwarder.ArgvForwarder24 def it_should_override_the_command_line_reporter(self):25 assert self.result[1] == cli.DotsReporter26###########################################################27# Suite tests28###########################################################29class WhenASuiteStartsInTeamCity(TeamCitySharedContext):30 def context(self):31 self.module = types.ModuleType('test_suite')32 def because_the_suite_starts(self):33 self.reporter.suite_started(self.module)34 def it_should_tell_team_city_the_suite_started(self):35 assert teamcity_parse(self.stringio.getvalue()) == ("testSuiteStarted", {'name': self.module.__name__})36class WhenASuiteEndsInTeamCity(TeamCitySharedContext):37 def context(self):38 self.module = types.ModuleType('mah_suite')39 def because_the_suite_ends(self):40 self.reporter.suite_ended(self.module)41 def it_should_tell_team_city_the_suite_ended(self):42 assert teamcity_parse(self.stringio.getvalue()) == ("testSuiteFinished", {'name': self.module.__name__})43###########################################################44# Suite tests45###########################################################46class WhenATestClassStartsInTeamCity(TeamCitySharedContext):47 def context(self):48 self.class_name = 'abc'49 def because_the_suite_starts(self):50 self.reporter.test_class_started(type(self.class_name, (), {}))51 def it_should_tell_team_city_the_class_started(self):52 assert teamcity_parse(self.stringio.getvalue()) == ("testClassStarted", {'name': self.class_name})53class WhenATestClassEndsInTeamCity(TeamCitySharedContext):54 def context(self):55 self.class_name = 'abc'56 def because_the_suite_ends(self):57 self.reporter.test_class_ended(type(self.class_name, (), {}))58 def it_should_tell_team_city_the_class_ended(self):59 assert teamcity_parse(self.stringio.getvalue()) == ("testClassFinished", {'name': self.class_name})60class WhenATestClassErrorsInTeamCity(TeamCitySharedContext):61 def context(self):62 self.class_name = 'abc'63 tb = [('made_up_file.py', 3, 'made_up_function', 'frame1'),64 ('another_made_up_file.py', 2, 'another_made_up_function', 'frame2')]65 self.exception = tools.build_fake_exception(tb, "Gotcha")66 self.formatted_tb = (67 'Traceback (most recent call last):|n'68 ' File "made_up_file.py", line 3, in made_up_function|n'69 ' frame1|n'70 ' File "another_made_up_file.py", line 2, in another_made_up_function|n'71 ' frame2|n'72 'plugin_tests.tools.FakeException: Gotcha')73 def because_the_suite_ends(self):74 self.reporter.test_class_errored(type(self.class_name, (), {}), self.exception)75 def it_should_tell_team_city_a_test_started(self):76 assert self.parse_line(0) == ("testStarted", {'name': self.class_name})77 def it_should_tell_team_city_the_test_failed(self):78 assert self.parse_line(1) == (79 "testFailed",80 {81 'name': self.class_name,82 'message': 'plugin_tests.tools.FakeException: Gotcha',83 'details': self.formatted_tb84 })85 def it_should_tell_team_city_the_test_finished(self):86 assert self.parse_line(2) == ("testFinished", {'name': self.class_name})87 def it_should_tell_team_city_the_class_finished(self):88 assert self.parse_line(3) == ("testClassFinished", {'name': self.class_name})89###########################################################90# assertion_started tests91###########################################################92class WhenAnAssertionStartsInTeamCity(TeamCitySharedContext):93 def establish_that_a_context_is_running(self):94 context = tools.create_context('MyNiceContext')95 self.reporter.context_started(context.cls, context.example)96 self.assertion = lambda: None97 self.assertion.__name__ = 'aLovelyAssertion'98 def because_the_assertion_starts(self):99 self.reporter.assertion_started(self.assertion)100 def it_should_tell_team_city_it_started(self):101 assert self.parse_line(0) == ("testStarted", {'name': 'My nice context -> a lovely assertion'})102class WhenAnAssertionInAContextWithExamplesStartsInTeamCity(TeamCitySharedContext):103 @setup104 def establish_that_a_context_with_an_example_is_running(self):105 context = tools.create_context('ContextWithExamples', 12.3)106 self.reporter.context_started(context.cls, context.example)107 self.assertion = lambda: None108 self.assertion.__name__ = 'aLovelyAssertion'109 def because_the_assertion_starts(self):110 self.reporter.assertion_started(self.assertion)111 @assertion112 def it_should_report_the_example(self):113 assert self.parse_line(0)[1]['name'] == 'Context with examples -> 12.3 -> a lovely assertion'114###########################################################115# assertion_passed tests116###########################################################117class WhenAnAssertionPassesInTeamCity(TeamCitySharedContext):118 def establish_that_a_context_is_running(self):119 context = tools.create_context('MyNiceContext')120 self.reporter.context_started(context.cls, context.example)121 self.assertion = lambda: None122 self.assertion.__name__ = 'aLovelyAssertion'123 def because_the_assertion_ends(self):124 self.reporter.assertion_passed(self.assertion)125 def it_should_tell_team_city_it_passed(self):126 assert self.parse_line(0) == ("testFinished", {'name': 'My nice context -> a lovely assertion'})127class WhenAnAssertionInAContextWithExamplesPassesInTeamCity(TeamCitySharedContext):128 @setup129 def establish_that_a_context_with_an_example_is_running(self):130 context = tools.create_context('ContextWithExamples', 12.3)131 self.reporter.context_started(context.cls, context.example)132 self.assertion = lambda: None133 self.assertion.__name__ = 'aLovelyAssertion'134 def because_the_assertion_passes(self):135 self.reporter.assertion_passed(self.assertion)136 @assertion137 def it_should_report_the_example(self):138 assert self.parse_line(0)[1]['name'] == 'Context with examples -> 12.3 -> a lovely assertion'139class WhenSomethingGetsPrintedDuringAPassingAssertionInTeamCity(TeamCitySharedContext):140 def establish_that_something_has_been_printed(self):141 self.real_stdout, self.real_stderr = sys.stdout, sys.stderr142 sys.stdout, sys.stderr = self.fake_stdout, self.fake_stderr = StringIO(), StringIO()143 context = tools.create_context('Context')144 self.reporter.context_started(context.cls, context.example)145 print("to stdout")146 print("to stderr", file=sys.stderr)147 self.assertion = lambda: None148 self.assertion.__name__ = "assertion"149 def because_the_assertion_passes(self):150 self.reporter.assertion_passed(self.assertion)151 def it_should_not_print_anything_to_stdout(self):152 assert self.fake_stdout.getvalue() == ''153 def it_should_not_print_anything_to_stderr(self):154 assert self.fake_stderr.getvalue() == ''155 def it_should_report_what_went_to_stdout(self):156 assert self.parse_line(0) == ("testStdOut", {'name': 'Context -> assertion', 'out': 'to stdout|n'})157 def it_should_report_what_went_to_stderr(self):158 assert self.parse_line(1) == ("testStdErr", {'name': 'Context -> assertion', 'out': 'to stderr|n'})159 def cleanup_stdout_and_stderr(self):160 sys.stdout, sys.stderr = self.real_stdout, self.real_stderr161###########################################################162# assertion_failed tests163###########################################################164class WhenAnAssertionFailsInTeamCity(TeamCitySharedContext):165 def establish_that_a_context_is_running(self):166 context = tools.create_context('FakeContext')167 tb = [('made_up_file.py', 3, 'made_up_function', 'frame1'),168 ('another_made_up_file.py', 2, 'another_made_up_function', 'frame2')]169 self.exception = tools.build_fake_assertion_error(tb, "Gotcha")170 self.formatted_tb = (171 'Traceback (most recent call last):|n'172 ' File "made_up_file.py", line 3, in made_up_function|n'173 ' frame1|n'174 ' File "another_made_up_file.py", line 2, in another_made_up_function|n'175 ' frame2|n'176 'plugin_tests.tools.FakeAssertionError: Gotcha')177 self.reporter.context_started(context.cls, context.example)178 self.assertion = lambda: None179 self.assertion.__name__ = 'Fake_assertion'180 def because_the_assertion_fails(self):181 self.reporter.assertion_failed(self.assertion, self.exception)182 def it_should_tell_team_city_it_failed(self):183 assert self.parse_line(0) == (184 "testFailed",185 {186 'name': 'Fake context -> Fake assertion',187 'message': 'plugin_tests.tools.FakeAssertionError: Gotcha',188 'details': self.formatted_tb189 })190 def it_should_tell_team_city_it_finished(self):191 assert self.parse_line(1) == ("testFinished", {'name': 'Fake context -> Fake assertion'})192class WhenAnAssertionInAContextWithExamplesFailsInTeamCity(TeamCitySharedContext):193 @setup194 def establish_that_a_context_with_an_example_is_running(self):195 ctx = tools.create_context('ContextWithExamples', 12.3)196 self.reporter.context_started(ctx.cls, ctx.example)197 self.assertion = lambda: None198 self.assertion.__name__ = 'aLovelyAssertion'199 def because_the_assertion_fails(self):200 self.reporter.assertion_failed(self.assertion, Exception())201 @assertion202 def it_should_report_the_example(self):203 assert self.parse_line(0)[1]['name'] == 'Context with examples -> 12.3 -> a lovely assertion'204class WhenSomethingGetsPrintedDuringAFailingAssertionInTeamCity(TeamCitySharedContext):205 def establish_that_something_has_been_printed(self):206 self.real_stdout, self.real_stderr = sys.stdout, sys.stderr207 sys.stdout, sys.stderr = self.fake_stdout, self.fake_stderr = StringIO(), StringIO()208 context = tools.create_context()209 self.reporter.context_started(context.cls, context.example)210 print("to stdout")211 print("to stderr", file=sys.stderr)212 self.assertion = lambda: None213 self.assertion.__name__ = "assertion"214 def because_the_assertion_fails(self):215 self.reporter.assertion_failed(self.assertion, Exception())216 def it_should_not_print_anything_to_stdout(self):217 assert self.fake_stdout.getvalue() == ''218 def it_should_not_print_anything_to_stderr(self):219 assert self.fake_stderr.getvalue() == ''220 def it_should_report_what_went_to_stdout(self):221 assert self.parse_line(0) == ("testStdOut", {'name': 'context -> assertion', 'out': 'to stdout|n'})222 def it_should_report_what_went_to_stderr(self):223 assert self.parse_line(1) == ("testStdErr", {'name': 'context -> assertion', 'out': 'to stderr|n'})224 def cleanup_stdout_and_stderr(self):225 sys.stdout, sys.stderr = self.real_stdout, self.real_stderr226###########################################################227# assertion_errored tests228###########################################################229class WhenAnAssertionErrorsInTeamCity(TeamCitySharedContext):230 def establish_that_a_context_is_running(self):231 context = tools.create_context('FakeContext')232 tb = [('made_up_file.py', 3, 'made_up_function', 'frame1'),233 ('another_made_up_file.py', 2, 'another_made_up_function', 'frame2')]234 self.exception = tools.build_fake_assertion_error(tb, "Gotcha")235 self.formatted_tb = (236 'Traceback (most recent call last):|n'237 ' File "made_up_file.py", line 3, in made_up_function|n'238 ' frame1|n'239 ' File "another_made_up_file.py", line 2, in another_made_up_function|n'240 ' frame2|n'241 'plugin_tests.tools.FakeAssertionError: Gotcha')242 self.reporter.context_started(context.cls, context.example)243 self.assertion = lambda: None244 self.assertion.__name__ = 'Fake_assertion'245 def because_the_assertion_errors(self):246 self.reporter.assertion_errored(self.assertion, self.exception)247 def it_should_tell_team_city_it_failed(self):248 assert self.parse_line(0) == (249 "testFailed",250 {251 'name': 'Fake context -> Fake assertion',252 'message': 'plugin_tests.tools.FakeAssertionError: Gotcha',253 'details': self.formatted_tb254 })255 def it_should_tell_team_city_it_finished(self):256 assert self.parse_line(1) == ("testFinished", {'name': 'Fake context -> Fake assertion'})257class WhenAnAssertionInAContextWithExamplesErrorsInTeamCity(TeamCitySharedContext):258 @setup259 def establish_that_a_context_with_an_example_is_running(self):260 context = tools.create_context('ContextWithExamples', 12.3)261 self.reporter.context_started(context.cls, context.example)262 self.assertion = lambda: None263 self.assertion.__name__ = 'aLovelyAssertion'264 def because_the_assertion_errors(self):265 self.reporter.assertion_errored(self.assertion, Exception())266 @assertion267 def it_should_report_the_example(self):268 assert self.parse_line(0)[1]['name'] == 'Context with examples -> 12.3 -> a lovely assertion'269class WhenSomethingGetsPrintedDuringAnErroringAssertionInTeamCity(TeamCitySharedContext):270 def establish_that_something_has_been_printed(self):271 self.real_stdout, self.real_stderr = sys.stdout, sys.stderr272 sys.stdout, sys.stderr = self.fake_stdout, self.fake_stderr = StringIO(), StringIO()273 self.context = tools.create_context("FakeContext")274 self.reporter.context_started(self.context.cls, self.context.example)275 print("to stdout")276 print("to stderr", file=sys.stderr)277 self.assertion = lambda: None278 self.assertion.__name__ = "FakeAssertion4"279 def because_we_run_an_assertion(self):280 self.reporter.assertion_errored(self.assertion, Exception())281 def it_should_not_print_anything_to_the_real_stdout(self):282 assert self.fake_stdout.getvalue() == ''283 def it_should_not_print_anything_to_the_real_stderr(self):284 assert self.fake_stdout.getvalue() == ''285 def it_should_tell_team_city_what_went_to_stdout(self):286 assert self.parse_line(0) == ("testStdOut", {'name': 'Fake context -> Fake assertion 4', 'out': 'to stdout|n'})287 def it_should_tell_team_city_what_went_to_stderr(self):288 assert self.parse_line(1) == ("testStdErr", {'name': 'Fake context -> Fake assertion 4', 'out': 'to stderr|n'})289 def cleanup_stdout_and_stderr(self):290 sys.stdout, sys.stderr = self.real_stdout, self.real_stderr291###########################################################292# context_errored tests293###########################################################294class WhenAContextErrorsInTeamCity(TeamCitySharedContext):295 def establish_that_a_context_is_running(self):296 self.context = tools.create_context('FakeContext')297 tb = [('made_up_file.py', 3, 'made_up_function', 'frame1'),298 ('another_made_up_file.py', 2, 'another_made_up_function', 'frame2')]299 self.exception = tools.build_fake_exception(tb, "Gotcha")300 self.formatted_tb = (301 'Traceback (most recent call last):|n'302 ' File "made_up_file.py", line 3, in made_up_function|n'303 ' frame1|n'304 ' File "another_made_up_file.py", line 2, in another_made_up_function|n'305 ' frame2|n'306 'plugin_tests.tools.FakeException: Gotcha')307 self.reporter.context_started(self.context.cls, self.context.example)308 def because_we_run_an_assertion(self):309 self.reporter.context_errored(self.context.cls, self.context.example, self.exception)310 def it_should_tell_team_city_a_test_started(self):311 assert self.parse_line(0) == ("testStarted", {'name': 'Fake context'})312 def it_should_tell_team_city_the_test_failed(self):313 assert self.parse_line(1) == (314 "testFailed",315 {316 'name': 'Fake context',317 'message': 'plugin_tests.tools.FakeException: Gotcha',318 'details': self.formatted_tb319 })320 def it_should_tell_team_city_the_test_finished(self):321 assert self.parse_line(2) == ("testFinished", {'name': 'Fake context'})322class WhenAContextWithExamplesErrorsInTeamCity(TeamCitySharedContext):323 @setup324 def establish_that_a_context_with_an_example_is_running(self):325 self.context = tools.create_context('ContextWithExamples', 12.3)326 self.reporter.context_started(self.context.cls, self.context.example)327 @action328 def because_the_context_errors(self):329 self.reporter.context_errored(self.context.cls, self.context.example, Exception())330 @assertion331 def it_should_report_the_example(self):332 assert self.parse_line(0)[1]['name'] == 'Context with examples -> 12.3'333class WhenSomethingGetsPrintedDuringAnErroringContextInTeamCity(TeamCitySharedContext):334 def establish_that_something_has_been_printed(self):335 self.real_stdout, self.real_stderr = sys.stdout, sys.stderr336 sys.stdout, sys.stderr = self.fake_stdout, self.fake_stderr = StringIO(), StringIO()337 self.context = tools.create_context("FakeContext")338 self.reporter.context_started(self.context.cls, self.context.example)339 print("to stdout")340 print("to stderr", file=sys.stderr)341 def because_we_run_an_assertion(self):342 self.reporter.context_errored(self.context.cls, self.context.example, Exception())343 def it_should_not_print_anything_to_the_real_stdout(self):344 assert self.fake_stdout.getvalue() == ''345 def it_should_not_print_anything_to_the_real_stderr(self):346 assert self.fake_stdout.getvalue() == ''347 def it_should_tell_team_city_what_went_to_stdout(self):348 assert self.parse_line(1) == ("testStdOut", {'name': 'Fake context', 'out': 'to stdout|n'})349 def it_should_tell_team_city_what_went_to_stderr(self):350 assert self.parse_line(2) == ("testStdErr", {'name': 'Fake context', 'out': 'to stderr|n'})351 def cleanup_stdout_and_stderr(self):352 sys.stdout, sys.stderr = self.real_stdout, self.real_stderr353###########################################################354# unexpected_error tests355###########################################################356class WhenAnUnexpectedErrorOccursInTeamCity(TeamCitySharedContext):357 def establish_the_exception(self):358 tb = [('made_up_file_7.py', 1, 'made_up_function_7', 'frame7'),359 ('made_up_file_8.py', 2, 'made_up_function_8', 'frame8')]360 self.exception = tools.build_fake_exception(tb, "another exception")361 self.formatted_tb = (362 'Traceback (most recent call last):|n'363 ' File "made_up_file_7.py", line 1, in made_up_function_7|n'364 ' frame7|n'365 ' File "made_up_file_8.py", line 2, in made_up_function_8|n'366 ' frame8|n'367 'plugin_tests.tools.FakeException: another exception')368 def because_an_unexpected_error_occurs(self):369 self.reporter.unexpected_error(self.exception)370 def it_should_tell_team_city_a_test_started(self):371 assert self.parse_line(0) == ("testStarted", {'name': 'Test error'})372 def it_should_tell_team_city_the_test_failed(self):373 assert self.parse_line(1) == (374 "testFailed",375 {376 'name': 'Test error',377 'message': 'plugin_tests.tools.FakeException: another exception',378 'details': self.formatted_tb379 })380 def it_should_tell_team_city_the_test_finished(self):381 assert self.parse_line(2) == ("testFinished", {'name': 'Test error'})382###########################################################383# tests for sequential calls384###########################################################385# these are really testing context_ended and context_errored,386# but sadly we can only observe it through assertion_started387class WhenASecondContextRuns(TeamCitySharedContext):388 def establish_that_a_context_has_run_and_ended(self):389 context1 = tools.create_context('the_first_context')390 self.reporter.context_started(context1.cls, context1.example)391 self.reporter.context_ended(context1.cls, context1.example)392 self.context2 = tools.create_context('the_second_context')393 self.reporter.context_started(self.context2.cls, self.context2.example)394 self.assertion = lambda: None395 self.assertion.__name__ = 'aLovelyAssertion'396 def when_something_gets_sent_to_team_city(self):397 self.reporter.assertion_started(self.assertion)398 @assertion399 def it_should_report_the_name_of_the_current_context(self):400 assert self.parse_line(0)[1]['name'] == 'the second context -> a lovely assertion'401class WhenASecondContextRunsAfterAnError(TeamCitySharedContext):402 def establish_that_a_context_has_run_and_errored(self):403 context1 = tools.create_context('the_first_context')404 self.reporter.context_started(context1.cls, context1.example)405 self.reporter.context_errored(context1.cls, context1.example, Exception())406 context2 = tools.create_context('the_second_context')407 self.reporter.context_started(context2.cls, context2.example)408 self.assertion = lambda: None409 self.assertion.__name__ = 'aLovelyAssertion'410 def when_something_gets_sent_to_team_city(self):411 self.reporter.assertion_started(self.assertion)412 @assertion413 def it_should_report_the_name_of_the_current_context(self):414 assert self.parse_line(-1)[1]['name'] == 'the second context -> a lovely assertion'415class WhenParsingATeamCityMessage:416 # tests for the test helper method417 @classmethod418 def examples(self):419 yield "##teamcity[hello]", ('hello', {})420 yield "##teamcity[hello2 ]", ('hello2', {})421 yield "##teamcity[msgName one='value one' two='value two']", ('msgName', {'one': 'value one', 'two': 'value two'})422 yield "##teamcity[escaped1 name='|'']", ('escaped1', {'name': "|'"})423 yield "##teamcity[escaped2 name='|]']", ('escaped2', {'name': "|]"})424 def context(self, example):425 self.msg, (self.expected_name, self.expected_values) = example426 def because_we_parse_a_message(self):427 self.name, self.values = teamcity_parse(self.msg)428 def it_should_return_the_correct_name(self):429 assert self.name == self.expected_name430 def it_should_return_the_correct_values(self):431 assert self.values == self.expected_values432def teamcity_parse(string):433 outer_match = re.match(r"##teamcity\[(\S+)( .*)?(?<!\|)\]", string)434 assignments_string = outer_match.group(2)435 if assignments_string is not None and assignments_string.strip():436 assignment_matches = re.findall(r"(\w+)='(.*?)(?<!\|)'", assignments_string)437 assignments = dict(assignment_matches)438 else:439 assignments = {}...

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