def test_case(context, path, collect_only, project): """Sync test cases with Polarion.""" testcases = testimony.get_testcases([path]) OBJ_CACHE['collect_only'] = collect_only OBJ_CACHE['project'] = project pool = multiprocessing.Pool(context.obj['jobs']) pool.map(add_test_case, testcases.items()) pool.close() pool.join()
def test_run( context, path, source_code_path, test_run_id, test_run_type, test_template_id, user, custom_fields, project): """Execute a test run based on jUnit XML file.""" custom_fields = load_custom_fields(custom_fields) test_run_id = re.sub(INVALID_CHARS_REGEX, '', test_run_id) testcases = { generate_test_id(test): test.tokens.get('id') for test in itertools.chain( *testimony.get_testcases([source_code_path]).values() ) } results = parse_junit(path) try: test_run = TestRun(test_run_id, project_id=project) click.echo('Test run {0} found.'.format(test_run_id)) except PylarionLibException as err: click.echo(err, err=True) click.echo('Creating test run {0}.'.format(test_run_id)) test_run = TestRun.create( project, test_run_id, test_template_id, type=test_run_type, **custom_fields) update = False if test_run.type != test_run_type: test_run.type = test_run_type update = True for field, value in custom_fields.items(): if getattr(test_run, field) != value: setattr(test_run, field, value) click.echo( 'Test Run {0} updated with {1}={2}.'.format( test_run_id, field, value) ) update = True if update: test_run.update() OBJ_CACHE['test_run'] = test_run OBJ_CACHE['user'] = user OBJ_CACHE['testcases'] = testcases TestRun.session.tx_begin() pool = multiprocessing.Pool(context.obj['jobs']) pool.map(add_test_record, results) pool.close() pool.join() TestRun.session.tx_commit()
def get_bz_data(paths): testcases = testimony.get_testcases(paths) result = {} for path, tests in testcases.items(): path_result = [] for test in tests: test_dict = test.to_dict() test_data = {**test_dict['tokens'], **test_dict['invalid-tokens']} if 'bz' in test_data.keys(): if ( 'customerscenario' not in test_data.keys() or test_data['customerscenario'] == 'false' ): path_result.append([test.name, test_data['bz']]) if path_result: result[path] = path_result return result
def xml_test_case(context, dry_run, lookup_method, response_property, source_code_path, project, output_path): """Generate an XML suited to be importer by the test-case importer. This will read the source code at SOURCE_CODE_PATH in order to capture the test cases and generate a XML file place at OUTPUT_PATH. The generated XML file will be ready to be imported by the XML Test Case Importer. The test cases will be created on the project ID provided by PROJECT and will be assigned to the Polarion user ID provided by USER. Other test case importer options can be set by the various options this command accepts. Check their help for more information. """ testcases = ElementTree.Element('testcases') testcases.set('project-id', project) if response_property: response_properties = ElementTree.Element('response-properties') element = ElementTree.Element('response-property') element.set('name', response_property[0]) element.set('value', response_property[1]) response_properties.append(element) testcases.append(response_properties) properties = ElementTree.Element('properties') properties.append( create_xml_property('dry-run', 'true' if dry_run else 'false')) properties.append(create_xml_property('lookup-method', lookup_method)) testcases.append(properties) source_testcases = itertools.chain( *testimony.get_testcases([source_code_path]).values()) for testcase in source_testcases: testcases.append(create_xml_testcase(testcase)) et = ElementTree.ElementTree(testcases) et.write(output_path, encoding='utf-8', xml_declaration=True) # et.write(output_path) # TODO remove from xml.dom import minidom pretty = minidom.parse(output_path).toprettyxml(indent=' ') with open(output_path, 'w') as f: f.write(pretty)
def xml_test_run( context, custom_fields, dry_run, lookup_method, no_include_skipped, response_property, status, test_run_id, test_run_title, junit_path, source_code_path, user, project, output_path): """Generate an XML suited to be importer by the test-run importer. This will read the jUnit XML at JUNIT_PATH and the source code at SOURCE_CODE_PATH in order to generate a XML file place at OUTPUT_PATH. The generated XML file will be ready to be imported by the XML Test Run Importer. The test run will be created on the project ID provided by PROJECT and will be assigned to the Polarion user ID provided by USER. Other test run options can be set by the various options this command accepts. Check their help for more information. """ test_run_id = re.sub(INVALID_CHARS_REGEX, '', test_run_id) testsuites = ElementTree.Element('testsuites') properties = ElementTree.Element('properties') custom_fields = load_custom_fields(custom_fields) custom_fields.update({ 'polarion-dry-run': 'true' if dry_run else 'false', 'polarion-include-skipped': 'false' if no_include_skipped else 'true', 'polarion-set-testrun-finished': 'false' if status == 'inprogress' else 'true', }) if response_property: key = 'polarion-response-' + response_property[0] custom_fields[key] = response_property[1] custom_fields['polarion-lookup-method'] = lookup_method custom_fields['polarion-project-id'] = project custom_fields['polarion-testrun-id'] = test_run_id if test_run_title: custom_fields['polarion-testrun-title'] = test_run_title custom_fields['polarion-user-id'] = user properties_names = ( 'polarion-dry-run', 'polarion-include-skipped', 'polarion-lookup-method', 'polarion-project-id', 'polarion-set-testrun-finished', 'polarion-testrun-id', 'polarion-testrun-title', 'polarion-user-id', ) for name, value in custom_fields.items(): if (not name.startswith('polarion-custom-') and not name.startswith('polarion-response-') and name not in properties_names): name = 'polarion-custom-{}'.format(name) properties.append(create_xml_property(name, value)) testsuites.append(properties) testcases = { generate_test_id(test): test.tokens.get('id') for test in itertools.chain( *testimony.get_testcases([source_code_path]).values() ) } testsuite = ElementTree.parse(junit_path).getroot() for testcase in testsuite.iterfind('testcase'): junit_test_case_id = '{0}.{1}'.format( testcase.get('classname'), testcase.get('name')) test_case_id = testcases.get(junit_test_case_id) if not test_case_id: click.echo( 'Could not find ID information for {}, skipping...' .format(junit_test_case_id) ) continue test_properties = ElementTree.Element('properties') element = ElementTree.Element('property') element.set('name', 'polarion-testcase-id') element.set('value', test_case_id) test_properties.append(element) testcase.append(test_properties) testsuites.append(testsuite) et = ElementTree.ElementTree(testsuites) et.write(output_path, encoding='utf-8', xml_declaration=True)
def test_case(path, collect_only, project): """Sync test cases with Polarion.""" testcases = testimony.get_testcases([path]) for path, tests in testcases.items(): requirement = None for test in tests: # Expect test_case_id to be path.test_name or # path.ClassName.test_name. test_case_id_parts = [ path.replace('/', '.').replace('.py', ''), test.name ] if test.parent_class is not None: test_case_id_parts.insert(-1, test.parent_class) test_case_id = '.'.join(test_case_id_parts) if requirement is None: requirement_name = parse_requirement_name(test_case_id) results = Requirement.query('{0}'.format(requirement_name), fields=['title', 'work_item_id']) if len(results) > 0: # As currently is not possible to get a single # match for the title, make sure to not use a # not intended Requirement. for result in results: if result.title == requirement_name: requirement = result if requirement is None: click.echo( 'Creating requirement {0}.'.format(requirement_name)) if not collect_only: requirement = Requirement.create(project, requirement_name, '', reqtype='functional') results = TestCase.query(test_case_id, fields=['description', 'work_item_id']) if len(results) == 0: click.echo( 'Creating test case {0} for requirement {1}.'.format( test.name, requirement_name)) if not collect_only: test_case = TestCase.create( project, test.name, test.docstring if test.docstring else '', caseautomation='automated', casecomponent='-', caseimportance='medium', caselevel='component', caseposneg='positive', subtype1='-', test_case_id=test_case_id, testtype='functional', ) click.echo( 'Liking test case {0} to verify requirement {1}.'.format( test.name, requirement_name)) if not collect_only: test_case.add_linked_item(requirement.work_item_id, 'verifies') else: click.echo( 'Updating test case {0} for requirement {1}.'.format( test.name, requirement_name)) # Ensure that a single match for the Test Case is # returned. assert len(results) == 1 test_case = results[0] if (not collect_only and test_case.description != test.docstring): test_case = TestCase(project, test_case.work_item_id) test_case.description = (test.docstring if test.docstring else '') test_case.update()
def test_case(path, collect_only, project): """Sync test cases with Polarion.""" testcases = testimony.get_testcases([path]) for path, tests in testcases.items(): requirement = None for test in tests: # Expect test_case_id to be path.test_name or # path.ClassName.test_name. test_case_id_parts = [ path.replace('/', '.').replace('.py', ''), test.name ] if test.parent_class is not None: test_case_id_parts.insert(-1, test.parent_class) test_case_id = '.'.join(test_case_id_parts) if requirement is None: requirement_name = parse_requirement_name(test_case_id) results = Requirement.query( '{0}'.format(requirement_name), fields=['title', 'work_item_id'] ) if len(results) > 0: # As currently is not possible to get a single # match for the title, make sure to not use a # not intended Requirement. for result in results: if result.title == requirement_name: requirement = result if requirement is None: click.echo( 'Creating requirement {0}.'.format(requirement_name)) if not collect_only: requirement = Requirement.create( project, requirement_name, '', reqtype='functional' ) results = TestCase.query( test_case_id, fields=['description', 'work_item_id']) if len(results) == 0: click.echo( 'Creating test case {0} for requirement {1}.' .format(test.name, requirement_name) ) if not collect_only: test_case = TestCase.create( project, test.name, test.docstring if test.docstring else '', caseautomation='automated', casecomponent='-', caseimportance='medium', caselevel='component', caseposneg='positive', subtype1='-', test_case_id=test_case_id, testtype='functional', ) click.echo( 'Liking test case {0} to verify requirement {1}.' .format(test.name, requirement_name) ) if not collect_only: test_case.add_linked_item( requirement.work_item_id, 'verifies') else: click.echo( 'Updating test case {0} for requirement {1}.' .format(test.name, requirement_name) ) # Ensure that a single match for the Test Case is # returned. assert len(results) == 1 test_case = results[0] if (not collect_only and test_case.description != test.docstring): test_case = TestCase(project, test_case.work_item_id) test_case.description = ( test.docstring if test.docstring else '') test_case.update()
def xml_test_run(context, custom_fields, dry_run, lookup_method, no_include_skipped, response_property, status, test_run_id, test_run_template_id, test_run_title, test_run_type_id, junit_path, source_code_path, user, project, output_path): """Generate an XML suited to be importer by the test-run importer. This will read the jUnit XML at JUNIT_PATH and the source code at SOURCE_CODE_PATH in order to generate a XML file place at OUTPUT_PATH. The generated XML file will be ready to be imported by the XML Test Run Importer. The test run will be created on the project ID provided by PROJECT and will be assigned to the Polarion user ID provided by USER. Other test run options can be set by the various options this command accepts. Check their help for more information. """ test_run_id = re.sub(INVALID_CHARS_REGEX, '', test_run_id) testsuites = ElementTree.Element('testsuites') properties = ElementTree.Element('properties') custom_fields = load_custom_fields(custom_fields) custom_fields.update({ 'polarion-dry-run': 'true' if dry_run else 'false', 'polarion-include-skipped': 'false' if no_include_skipped else 'true', 'polarion-set-testrun-finished': 'false' if status == 'inprogress' else 'true', }) if response_property: key = 'polarion-response-' + response_property[0] custom_fields[key] = response_property[1] custom_fields['polarion-lookup-method'] = lookup_method custom_fields['polarion-project-id'] = project custom_fields['polarion-testrun-id'] = test_run_id if test_run_template_id: custom_fields['polarion-testrun-template-id'] = test_run_template_id if test_run_title: custom_fields['polarion-testrun-title'] = test_run_title if test_run_type_id: custom_fields['polarion-testrun-type-id'] = test_run_type_id custom_fields['polarion-user-id'] = user properties_names = ( 'polarion-dry-run', 'polarion-include-skipped', 'polarion-lookup-method', 'polarion-project-id', 'polarion-set-testrun-finished', 'polarion-testrun-id', 'polarion-testrun-template-id', 'polarion-testrun-title', 'polarion-testrun-type-id', 'polarion-user-id', ) for name, value in custom_fields.items(): if (not name.startswith('polarion-custom-') and not name.startswith('polarion-response-') and name not in properties_names): name = 'polarion-custom-{}'.format(name) properties.append(create_xml_property(name, value)) testsuites.append(properties) testcases = { generate_test_id(test): test.tokens.get('id') for test in itertools.chain( *testimony.get_testcases([source_code_path]).values()) } testsuite = ElementTree.parse(junit_path).getroot() for testcase in testsuite.iterfind('testcase'): junit_test_case_id = '{0}.{1}'.format(testcase.get('classname'), testcase.get('name')) test_case_id = testcases.get(junit_test_case_id) if not test_case_id: click.echo( 'Could not find ID information for {}, skipping...'.format( junit_test_case_id)) continue test_properties = ElementTree.Element('properties') element = ElementTree.Element('property') element.set('name', 'polarion-testcase-id') element.set('value', test_case_id) test_properties.append(element) testcase.append(test_properties) testsuites.append(testsuite) et = ElementTree.ElementTree(testsuites) et.write(output_path, encoding='utf-8', xml_declaration=True)