def test_write_success(self, mocked__console, mocked__session): test_success = { "name": "should_be_success", "status": "passed", "failure": None, "error": None, } fake_results_passed = [ { "response": "foo", "tests_results": [test_success], "no_failure": True, }, ] mocked__session.failures = 0 mocked__session.successes = 1 write_results(fake_results_passed) mocked__console.print.assert_called_once_with( "[bright_green] [PASSED] [white]should_be_success") mocked__console.rule.assert_called_once_with( "[bright_green]1 passed in 3.0s", characters="=", )
def scan(): """Caller function that tries to scans the file and write the report.""" spec_path = settings["spec_path"] no_report = settings["no_report"] open_browser = settings["open_browser"] try: api_spec = load_config_file(spec_path) except FileNotFoundError as e: error_message = f"Could not find API spec file: {spec_path}. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except EmptyConfigFileError as e: error_message = f"API spec file is empty. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except yaml.YAMLError as e: error_message = "Error loading specification file." error_message = "{}\nPyYAML: {}".format(error_message, str(e)) logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) try: root_node = EndpointNode(api_spec) results = root_node.run() except (InvalidKeyError, KeyError, InvalidPythonCodeError,) as e: error_message = "Error loading API spec." error_message = "{} {}".format(error_message, str(e)) logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) write_results(results) if no_report: session.exit() try: write_report(results, open_browser) except (BadConfigurationError, InvalidPythonCodeError) as e: logger.error(e) raise SystemExit(ExitCode.USAGE_ERROR) session.exit()
def test_write_failures(self, mocker, mocked__console, mocked__session): tests = [ { "name": "failed_test", "status": "failed", "failure": "response.status_code == 200", "error": None, }, { "name": "should_be_success", "status": "passed", "failure": None, "error": None, }, ] fake_results_failed = [ { "response": "foo", "tests_results": tests, "no_failure": True, }, ] mocked__session.failures = 1 mocked__session.successes = 1 write_results(fake_results_failed) calls = [ mocker.call( "[bright_red] [FAILED] [white]failed_test\n\t [bright_red]response.status_code == 200 is false" ), mocker.call("[bright_green] [PASSED] [white]should_be_success"), ] mocked__console.print.assert_has_calls(calls) mocked__console.rule.assert_called_once_with( "[bright_green]1 passed, [bright_red]1 failed, [bright_red]0 errors in 3.0s", characters="=", style="bright_red", )
def _write(results): """When the user passed the `--no-report` flag: prints the test results to the console output. When the user did not pass the `--no_report flag`: writes the results on a report file and opens it using a browser, if the --browser flag is present. Returns: None """ no_report = settings["no_report"] open_browser = settings["open_browser"] if no_report: write_results(results) return try: _write_report(results, open_browser) except (BadConfigurationError, InvalidPythonCodeError) as e: logger.error(e) raise SystemExit(ExitCode.USAGE_ERROR)
def test_write_without_tests(self, mocker, mocked__console, mocked__session): fake_results_failed = [ { "response": "foo", "tests_results": [], "no_failure": True, }, ] mocked__session.failures = 0 mocked__session.successes = 0 write_results(fake_results_failed) assert not mocked__console.print.called mocked__console.rule.assert_called_once_with( "[bright_green]0 passed in 3.0s", characters="=", )
def test_should_call_3_times(self, mocked__write_result): write_results([1, 2, 3]) assert mocked__write_result.call_count == 3
def test_should_not_call(self, mocked__write_result): write_results([]) assert not mocked__write_result.called