def test_api_exception(self, single_passing_xml, simple_json_config, mocker): """Verify that the function fails gracefully if the API endpoint reports an API exception""" # Mock mock_field_resp = mocker.Mock(spec=swagger_client.FieldResource) mock_field_resp.id = 12345 mock_field_resp.label = 'Failure Output' response = { 'items': [{ 'name': 'insert name here', 'id': 12345 }], 'total': 1 } mock_link_response = mocker.Mock( spec=swagger_client.LinkedArtifactContainer) mock_post_response = mocker.Mock(spec=requests.Response) mock_post_response.text = json.dumps(response) mocker.patch('requests.post', return_value=mock_post_response) mocker.patch('swagger_client.FieldApi.get_fields', return_value=[mock_field_resp]) mocker.patch('swagger_client.TestlogApi.submit_automation_test_logs_0', side_effect=ApiException('Super duper failure!')) mocker.patch('swagger_client.ObjectlinkApi.link_artifacts', return_value=[mock_link_response]) # Setup zz = ZigZag(single_passing_xml, simple_json_config, TOKEN) zz.parse() # Test with pytest.raises(RuntimeError): zz.upload_test_results()
def test_job_queue_failure(self, single_passing_xml, mocker): """Verify that the function fails gracefully if the job queue reports a failure""" # Mock mock_field_resp = mocker.Mock(spec=swagger_client.FieldResource) mock_field_resp.id = 12345 mock_field_resp.label = 'Failure Output' response = { 'items': [{ 'name': 'insert name here', 'id': 12345 }], 'total': 1 } mock_link_response = mocker.Mock( spec=swagger_client.LinkedArtifactContainer) mock_post_response = mocker.Mock(spec=requests.Response) mock_post_response.text = json.dumps(response) mocker.patch('requests.post', return_value=mock_post_response) mocker.patch('swagger_client.FieldApi.get_fields', return_value=[mock_field_resp]) mocker.patch('swagger_client.ObjectlinkApi.link_artifacts', return_value=[mock_link_response]) # Setup zz = ZigZag(single_passing_xml, TOKEN, PROJECT_ID, TEST_CYCLE) zz.parse() # Test with pytest.raises(RuntimeError): zz.upload_test_results()
def main(junit_input_file, qtest_project_id, qtest_test_cycle, pprint_on_fail, test_runner): """Upload JUnitXML results to qTest manager. \b Required Arguments: JUNIT_INPUT_FILE A valid JUnit XML results file. QTEST_PROJECT_ID The the target qTest Project ID for results \b Required Environment Variables: QTEST_API_TOKEN The qTest API token to use for authorization """ api_token_env_var = 'QTEST_API_TOKEN' try: if not os.environ.get(api_token_env_var): raise RuntimeError( 'The "{}" environment variable is not defined! ' 'See help for more details.'.format(api_token_env_var)) zz = ZigZag(junit_input_file, os.environ[api_token_env_var], qtest_project_id, qtest_test_cycle, pprint_on_fail) zz.test_runner = test_runner zz.parse() job_id = zz.upload_test_results() click.echo(click.style("\nQueue Job ID: {}".format(str(job_id)))) click.echo(click.style("\nSuccess!", fg='green')) except RuntimeError as e: click.echo(click.style(str(e), fg='red')) click.echo(click.style("\nFailed!", fg='red')) sys.exit(1)
def pytest_sessionfinish(session): """This hook is used by pytest to build the junit XML Using ZigZag as a library we upload in the pytest runtime Args: session (_pytest.main.Session): The pytest session object """ SESSION_MESSAGES.drain( ) # need to reset this on every pass through this hook if session.config.pluginmanager.hasplugin('junitxml'): zz_option = _get_option_of_highest_precedence(session.config, 'zigzag') pytest_zigzag_config = _get_option_of_highest_precedence( session.config, 'pytest-zigzag-config') if zz_option and pytest_zigzag_config: try: junit_file_path = getattr(session.config, '_xml', None).logfile # noinspection PyTypeChecker # validate token token = _validate_qtest_token(os.environ['QTEST_API_TOKEN']) zz = ZigZag(junit_file_path, pytest_zigzag_config, token) job_id = zz.upload_test_results() SESSION_MESSAGES.append("ZigZag upload was successful!") SESSION_MESSAGES.append("Queue Job ID: {}".format(job_id)) except Exception as e: # we want this super broad so we dont break test execution SESSION_MESSAGES.append('The ZigZag upload was not successful') SESSION_MESSAGES.append("Original error message:\n\n{}".format( str(e)))
def main(zigzag_config_file, junit_input_file, pprint_on_fail): """Upload JUnitXML results to qTest manager. \b Required Arguments: ZIGZAG_CONFIG_FILE A valid json config file for zigzag. JUNIT_INPUT_FILE A valid JUnit XML results file. \b Required Environment Variables: QTEST_API_TOKEN The qTest API token to use for authorization """ api_token_env_var = 'QTEST_API_TOKEN' try: if not os.environ.get(api_token_env_var): raise RuntimeError( 'The "{}" environment variable is not defined! ' 'See help for more details.'.format(api_token_env_var)) zz = ZigZag(junit_input_file, zigzag_config_file, os.environ[api_token_env_var], pprint_on_fail) zz.parse() job_id = zz.upload_test_results() click.echo(click.style("\nQueue Job ID: {}".format(str(job_id)))) click.echo(click.style("\nSuccess!", fg='green')) except (RuntimeError, ZigZagError) as e: click.echo(click.style(str(e), fg='red')) click.echo(click.style("\nFailed!", fg='red')) sys.exit(1)
def invoke_zigzag(self, force_clean_up=True): """Execute the ZigZag CLI. Args: force_clean_up (bool): Flag indicating whether previous test data should be cleaned up first before execution. (Default: True) """ if force_clean_up: self.clean_up() self.tests.reset( ) # This is for super safety in case a developer was being tricky with execution with open(self._junit_xml_file_path, 'w') as f: f.write( self._junit_template.render(tests=self._tests, global_props=self._global_props)) zz = ZigZag(self._junit_xml_file_path, self._qtest_api_token, self._qtest_project_id, self._qtest_root_test_cycle.name, False) zz.parse() self._last_invocation_queue_job_id = zz.upload_test_results()
def test_happy_path(self, single_passing_xml, mocker): """Verify that the function can upload results from a JUnitXML file that contains a single passing test""" # Expectation job_id = '54321' # Mock test_cycle_name = 'queens' test_cycle_pid = 'CL-1' mock_get_tc_resp = mocker.Mock(spec=swagger_client.TestCycleResource) mock_create_tc_resp = mocker.Mock( spec=swagger_client.TestCycleResource) mock_get_tc_resp.to_dict.return_value = { 'name': 'queens', 'pid': 'CL-2' } mock_create_tc_resp.to_dict.return_value = { 'name': test_cycle_name, 'pid': test_cycle_pid } mock_queue_resp = mocker.Mock(state='IN_WAITING', id=job_id) mock_field_resp = mocker.Mock(spec=swagger_client.FieldResource) mock_field_resp.id = 12345 mock_field_resp.label = 'Failure Output' response = { 'items': [{ 'name': 'insert name here', 'id': 12345 }], 'total': 1 } mock_link_response = mocker.Mock( spec=swagger_client.LinkedArtifactContainer) mock_post_response = mocker.Mock(spec=requests.Response) mock_post_response.text = json.dumps(response) mocker.patch('requests.post', return_value=mock_post_response) mocker.patch('swagger_client.TestlogApi.submit_automation_test_logs_0', return_value=mock_queue_resp) mocker.patch('swagger_client.FieldApi.get_fields', return_value=[mock_field_resp]) mocker.patch('swagger_client.ObjectlinkApi.link_artifacts', return_value=[mock_link_response]) mocker.patch('swagger_client.TestcycleApi.get_test_cycles', return_value=[mock_get_tc_resp]) mocker.patch('swagger_client.TestcycleApi.create_cycle', return_value=mock_create_tc_resp) # Setup zz = ZigZag(single_passing_xml, TOKEN, PROJECT_ID, TEST_CYCLE) zz.parse() # Test response = zz.upload_test_results() assert int(job_id) == response