Best Python code snippet using autotest_python
control_file.py
Source:control_file.py
...45 """46 lines = _indent_text(lines, ' ')47 lines = 'def step%d():\n%s' % (item, lines)48 return lines49def _get_tests_stanza(tests, is_server, prepend=None, append=None,50 client_control_file='', test_source_build=None):51 """ Constructs the control file test step code from a list of tests.52 @param tests A sequence of test control files to run.53 @param is_server bool, Is this a server side test?54 @param prepend A list of steps to prepend to each client test.55 Defaults to [].56 @param append A list of steps to append to each client test.57 Defaults to [].58 @param client_control_file If specified, use this text as the body of a59 final client control file to run after tests. is_server must be False.60 @param test_source_build: Build to be used to retrieve test code. Default61 to None.62 @returns The control file test code to be run.63 """64 assert not (client_control_file and is_server)65 if not prepend:66 prepend = []67 if not append:68 append = []69 if test_source_build:70 raw_control_files = _get_test_control_files_by_build(71 tests, test_source_build)72 else:73 raw_control_files = [_read_control_file(test) for test in tests]74 if client_control_file:75 # 'return locals()' is always appended in case the user forgot, it76 # is necessary to allow for nested step engine execution to work.77 raw_control_files.append(client_control_file + '\nreturn locals()')78 raw_steps = prepend + [_add_boilerplate_to_nested_steps(step)79 for step in raw_control_files] + append80 steps = [_format_step(index, step)81 for index, step in enumerate(raw_steps)]82 if is_server:83 step_template = SERVER_STEP_TEMPLATE84 footer = '\n\nstep_init()\n'85 else:86 step_template = CLIENT_STEP_TEMPLATE87 footer = ''88 header = ''.join(step_template % i for i in xrange(len(steps)))89 return header + '\n' + '\n\n'.join(steps) + footer90def _indent_text(text, indent):91 """Indent given lines of python code avoiding indenting multiline92 quoted content (only for triple " and ' quoting for now).93 @param text The string of lines.94 @param indent The indent string.95 @return The indented string.96 """97 regex = re.compile('(\\\\*)("""|\'\'\')')98 res = []99 in_quote = None100 for line in text.splitlines():101 # if not within a multinline quote indent the line contents102 if in_quote:103 res.append(line)104 else:105 res.append(indent + line)106 while line:107 match = regex.search(line)108 if match:109 # for an even number of backslashes before the triple quote110 if len(match.group(1)) % 2 == 0:111 if not in_quote:112 in_quote = match.group(2)[0]113 elif in_quote == match.group(2)[0]:114 # if we found a matching end triple quote115 in_quote = None116 line = line[match.end():]117 else:118 break119 return '\n'.join(res)120def _get_profiler_commands(profilers, is_server, profile_only):121 prepend, append = [], []122 if profile_only is not None:123 prepend.append("job.default_profile_only = %r" % profile_only)124 for profiler in profilers:125 prepend.append("job.profilers.add('%s')" % profiler.name)126 append.append("job.profilers.delete('%s')" % profiler.name)127 return prepend, append128def _sanity_check_generate_control(is_server, client_control_file):129 """130 Sanity check some of the parameters to generate_control().131 This exists as its own function so that site_control_file may call it as132 well from its own generate_control().133 @raises ValidationError if any of the parameters do not make sense.134 """135 if is_server and client_control_file:136 raise model_logic.ValidationError(137 {'tests' : 'You cannot run server tests at the same time '138 'as directly supplying a client-side control file.'})139def generate_control(tests, is_server=False, profilers=(),140 client_control_file='', profile_only=None,141 test_source_build=None):142 """143 Generate a control file for a sequence of tests.144 @param tests A sequence of test control files to run.145 @param is_server bool, Is this a server control file rather than a client?146 @param profilers A list of profiler objects to enable during the tests.147 @param client_control_file Contents of a client control file to run as the148 last test after everything in tests. Requires is_server=False.149 @param profile_only bool, should this control file run all tests in150 profile_only mode by default151 @param test_source_build: Build to be used to retrieve test code. Default152 to None.153 @returns The control file text as a string.154 """155 _sanity_check_generate_control(is_server=is_server,156 client_control_file=client_control_file)157 control_file_text = EMPTY_TEMPLATE158 prepend, append = _get_profiler_commands(profilers, is_server, profile_only)159 control_file_text += _get_tests_stanza(tests, is_server, prepend, append,160 client_control_file,161 test_source_build)162 return control_file_text163def _get_test_control_files_by_build(tests, build, ignore_invalid_tests=False):164 """Get the test control files that are available for the specified build.165 @param tests A sequence of test objects to run.166 @param build: unique name by which to refer to the image.167 @param ignore_invalid_tests: flag on if unparsable tests are ignored.168 @return: A sorted list of all tests that are in the build specified.169 """170 raw_control_files = []171 # shortcut to avoid staging the image.172 if not tests:173 return raw_control_files...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!