def test_source_generator(): """Check if ``collect_tests`` 'tests/data'collect tests.""" tests = collector.collect_tests('tests/data/test_sample.py') test_decorated_test = [ test for test in tests['tests/data/test_sample.py'] if test.name == 'test_decorated_test' ].pop() assert test_decorated_test.decorators == [ 'decorator', 'decorator.mark.something', "decorator_with_args([1, b'bytes', ('a', 'b'), None])", 'decorator_with_args(*[True, ((True or False) and True)])', "decorator_with_args((f'{CONSTANT!r:5>} with literal {{ and }}',))", 'decorator_with_args({1, 2, (- 3)})', "decorator_with_args({'a': 1, 'b': 2, **{'c': 3}})", 'decorator_with_args([1, 2][0], [1, 2][:1], [1, 2][0:], ' '[1, 2][0:1:1])', 'decorator_with_args([i for i in range(5) if ((i % 2) == 0)])', 'decorator_with_args((i for i in range(5)))', 'decorator_with_args({i for i in range(5)})', "decorator_with_args({k: v for k in 'abcde' for v in range(5)})", 'decorator_with_args(1, 2, 3, a=1, b=2)', 'decorator_with_args(' 'dict(a=1, b=2), ' "dict(**{'a': 1}), " 'vars(decorator.mark), ' '(lambda a, *args, b=1, **kwargs: (a, args, b, kwargs)), ' '(lambda a, *, b=1: (a, b)), ' '(lambda v: (v if v else None))' ')', ]
def requirement(source_code_path, project): """Create and/or update requirements in Polarion.""" requirements = [] source_testcases = itertools.chain( *collector.collect_tests(source_code_path).values()) for testcase in source_testcases: fields = {k.lower(): v for k, v in testcase.fields.items()} if ('requirement' in fields and fields['requirement'] not in requirements): requirement_title = fields['requirement'] results = Requirement.query(requirement_title, fields=['status', 'title']) requirement = None for result in results: if result.title == requirement_title: requirement = result break if requirement is None: click.echo(u'Creating requirement {0}.'.format( fields['requirement'])) requirement = Requirement.create(project, requirement_title, '', reqtype='functional') if requirement.status != 'approved': click.echo(u'Approving requirement {0}.'.format( requirement.title)) requirement.status = 'approved' requirement.update()
def test_collect_tests(path): """Check if ``collect_tests`` 'tests/data'collect tests.""" tests = collector.collect_tests(path) assert 'tests/data/test_sample.py' in tests assert len(tests['tests/data/test_sample.py']) == 4 # Check if we are not doing a specific python module collection if path.endswith('.py'): return assert 'tests/data/ignore_dir/test_ignore_dir.py' in tests assert len(tests['tests/data/ignore_dir/test_ignore_dir.py']) == 2
def test_case( config, automation_script_format, collect_ignore_path, dry_run, lookup_method, lookup_method_custom_field_id, 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)) if lookup_method == 'custom': properties.append(create_xml_property( 'polarion-custom-lookup-method-field-id', lookup_method_custom_field_id )) testcases.append(properties) source_testcases = itertools.chain(*collector.collect_tests( source_code_path, collect_ignore_path).values()) for testcase in source_testcases: testcases.append( create_xml_testcase(config, testcase, automation_script_format)) et = ElementTree.ElementTree(testcases) et.write(output_path, encoding='utf-8', xml_declaration=True)
def test_run(collect_ignore_path, custom_fields, dry_run, lookup_method, lookup_method_custom_field_id, 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 if lookup_method == 'custom': custom_fields['polarion-custom-lookup-method-field-id'] = ( lookup_method_custom_field_id) 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-custom-lookup-method-field-id', '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 = {} for test in itertools.chain(*collector.collect_tests( source_code_path, collect_ignore_path).values()): junit_test_case_id = generate_test_id(test) test_id = test.fields.get('id') if not test_id: click.echo( 'Was not able to find the ID for {0}, setting it to {0}'. format(junit_test_case_id)) test_id = junit_test_case_id testcases[junit_test_case_id] = test_id 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( 'Found {} on jUnit report but not on source code, 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_collect_tests(path): """Check if ``collect_tests`` 'tests/data'collect tests.""" tests = collector.collect_tests(path) assert 'tests/data/test_sample.py' in tests assert len(tests['tests/data/test_sample.py']) == 3
def test_collect_ignore_path(ignore_path): """Check if ``collect_tests`` don't collect tests on the ignore paths.""" tests = collector.collect_tests('tests/data', [ignore_path]) assert 'tests/data/ignore_dir/test_ignore_dir.py' not in tests assert 'tests/data/test_sample.py' in tests assert len(tests['tests/data/test_sample.py']) == 4
def test_run( config, collect_ignore_path, create_defects, custom_fields, dry_run, lookup_method, lookup_method_custom_field_id, no_include_skipped, response_property, status, test_run_group_id, test_run_id, test_run_template_id, test_run_title, test_run_type_id, junit_path, project_span_ids, 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-create-defects': 'true' if create_defects else 'false', 'polarion-dry-run': 'true' if dry_run else 'false', 'polarion-include-skipped': 'false' if no_include_skipped else 'true', }) if response_property: key = 'polarion-response-' + response_property[0] custom_fields[key] = response_property[1] custom_fields['polarion-lookup-method'] = lookup_method if lookup_method == 'custom': custom_fields['polarion-custom-lookup-method-field-id'] = ( lookup_method_custom_field_id ) custom_fields['polarion-project-id'] = project custom_fields['polarion-testrun-id'] = test_run_id custom_fields['polarion-testrun-status-id'] = status if project_span_ids: custom_fields['polarion-project-span-ids'] = project_span_ids if test_run_group_id: custom_fields['polarion-group-id'] = test_run_group_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-create-defects', 'polarion-custom-lookup-method-field-id', 'polarion-dry-run', 'polarion-group-id', 'polarion-include-skipped', 'polarion-lookup-method', 'polarion-project-id', 'polarion-project-span-ids', 'polarion-testrun-id', 'polarion-testrun-status-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 = {} for test in itertools.chain(*collector.collect_tests( source_code_path, collect_ignore_path).values()): update_testcase_fields(config, test) testcases[test.junit_id] = test testsuite = ElementTree.parse(junit_path).getroot() if testsuite.tag == 'testsuites': testsuite = testsuite.getchildren()[0] for testcase in testsuite.iterfind('testcase'): junit_test_case_id = '{0}.{1}'.format( testcase.get('classname'), testcase.get('name')) pytest_parameters = None if '[' in junit_test_case_id: junit_test_case_id, pytest_parameters = junit_test_case_id.split( '[', 1) pytest_parameters = pytest_parameters[:-1] source_test_case = testcases.get(junit_test_case_id) if not source_test_case: click.echo( 'Found {} on jUnit report but not on source code, 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', source_test_case.fields['id']) test_properties.append(element) if (pytest_parameters and source_test_case.fields.get('parametrized') == 'yes'): element = ElementTree.Element('property') element.set('name', 'polarion-parameter-pytest parameters') element.set('value', pytest_parameters) test_properties.append(element) elif (pytest_parameters and source_test_case.fields.get('parametrized') != 'yes'): click.echo( '{} has a parametrized result of {} but its parametrized ' 'field is not set to yes. Only one result will be recorded.' .format(junit_test_case_id, pytest_parameters) ) testcase.append(test_properties) testsuites.append(testsuite) et = ElementTree.ElementTree(testsuites) et.write(output_path, encoding='utf-8', xml_declaration=True)
def requirement( config, assignee, approver, collect_ignore_path, dry_run, lookup_method, response_property, source_code_path, project, output_path): """Generate an XML suited to be importer by the requirement importer. This will read the source code at SOURCE_CODE_PATH in order to capture the requirements and generate a XML file place at OUTPUT_PATH. The generated XML file will be ready to be imported by the XML Requirement Importer. The requirements will be created on the project ID provided by PROJECT and will be assigned to the Polarion user ID provided by USER. Other requirement importer options can be set by the various options this command accepts. Check their help for more information. """ requirements = ElementTree.Element('requirements') requirements.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) requirements.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)) requirements.append(properties) source_testcases = itertools.chain(*collector.collect_tests( source_code_path, collect_ignore_path).values()) cache = [] for testcase in source_testcases: update_testcase_fields(config, testcase) if ('requirement' in testcase.fields and testcase.fields['requirement'] not in cache): requirement_title = testcase.fields['requirement'] fields = {} if assignee: fields['assignee'] = assignee if approver: fields['approvers'] = approver requirement = collector.Requirement(requirement_title, fields) requirement.fields.update( get_requirement_field_values(config, requirement) ) for field in requirement.fields.keys(): transform_func = getattr( config, 'TRANSFORM_REQUIREMENT_{}_VALUE'.format(field.upper()), None ) if callable(transform_func): fields[field] = transform_func( requirement.fields[field], requirement ) cache.append(requirement_title) requirements.append( create_xml_requirement(config, requirement) ) et = ElementTree.ElementTree(requirements) et.write(output_path, encoding='utf-8', xml_declaration=True)