def test_html_report(self): runner = HttpRunner(failfast=True) summary = runner.run(self.testcase_cli_path) self.assertEqual(summary["stat"]["testcases"]["total"], 1) self.assertEqual(summary["stat"]["teststeps"]["total"], 10) self.assertEqual(summary["stat"]["teststeps"]["skipped"], 4) report_save_dir = os.path.join(os.getcwd(), 'reports', "demo") report.gen_html_report(summary, report_dir=report_save_dir) self.assertGreater(len(os.listdir(report_save_dir)), 0) shutil.rmtree(report_save_dir)
def test_html_report_with_fixed_report_file(self): runner = HttpRunner(failfast=True) summary = runner.run(self.testcase_cli_path) self.assertEqual(summary["stat"]["testcases"]["total"], 1) self.assertEqual(summary["stat"]["teststeps"]["total"], 10) self.assertEqual(summary["stat"]["teststeps"]["skipped"], 4) report_file = os.path.join(os.getcwd(), 'reports', "demo", "test.html") report.gen_html_report(summary, report_file=report_file) report_save_dir = os.path.dirname(report_file) self.assertEqual(len(os.listdir(report_save_dir)), 1) self.assertTrue(os.path.isfile(report_file)) shutil.rmtree(report_save_dir)
def test_html_report_repsonse_image(self): runner = HttpRunner(failfast=True) summary = runner.run("tests/httpbin/load_image.yml") report_save_dir = os.path.join(os.getcwd(), 'reports', "demo") report_path = report.gen_html_report(summary, report_dir=report_save_dir) self.assertTrue(os.path.isfile(report_path)) shutil.rmtree(report_save_dir)
def test_html_report_xss(self): testcases = [ { "config": { 'name': "post data" }, "teststeps": [ { "name": "post data", "request": { "url": "{}/anything".format(HTTPBIN_SERVER), "method": "POST", "headers": { "Content-Type": "application/json" }, "json": { 'success': False, "person": "<img src=x onerror=alert(1)>" } }, "validate": [ {"eq": ["status_code", 200]} ] } ] } ] tests_mapping = { "testcases": testcases } summary = self.runner.run(tests_mapping) report_path = report.gen_html_report(summary) with open(report_path) as f: content = f.read() m = re.findall( re.escape(""person": "<img src=x onerror=alert(1)>""), content ) self.assertEqual(len(m), 2)
def main(): """ API test: parse command line options and run commands. """ if is_py2: color_print(get_python2_retire_msg(), "YELLOW") parser = argparse.ArgumentParser(description=__description__) parser.add_argument( '-V', '--version', dest='version', action='store_true', help="show version") parser.add_argument( 'testfile_paths', nargs='*', help="Specify api/testcase/testsuite file paths to run.") parser.add_argument( '--log-level', default='INFO', help="Specify logging level, default is INFO.") parser.add_argument( '--log-file', help="Write logs to specified file path.") parser.add_argument( '--dot-env-path', help="Specify .env file path, which is useful for keeping sensitive data.") parser.add_argument( '--report-template', help="Specify report template path.") parser.add_argument( '--report-dir', help="Specify report save directory.") parser.add_argument( '--report-file', help="Specify report file path, this has higher priority than specifying report dir.") parser.add_argument( '--save-tests', action='store_true', default=False, help="Save loaded/parsed/summary json data to JSON files.") parser.add_argument( '--failfast', action='store_true', default=False, help="Stop the test run on the first error or failure.") parser.add_argument( '--startproject', help="Specify new project name.") parser.add_argument( '--validate', nargs='*', help="Validate YAML/JSON api/testcase/testsuite format.") parser.add_argument( '--prettify', nargs='*', help="Prettify JSON testcase format.") args = parser.parse_args() if len(sys.argv) == 1: # no argument passed parser.print_help() sys.exit(0) if args.version: color_print("{}".format(__version__), "GREEN") sys.exit(0) if args.validate: for validate_path in args.validate: try: color_print("validate test file: {}".format(validate_path), "GREEN") load_cases(validate_path, args.dot_env_path) except exceptions.MyBaseError as ex: log_error(str(ex)) continue color_print("done!", "BLUE") sys.exit(0) if args.prettify: prettify_json_file(args.prettify) sys.exit(0) project_name = args.startproject if project_name: create_scaffold(project_name) sys.exit(0) runner = HttpRunner( failfast=args.failfast, save_tests=args.save_tests, log_level=args.log_level, log_file=args.log_file ) err_code = 0 try: for path in args.testfile_paths: summary = runner.run(path, dot_env_path=args.dot_env_path) report_dir = args.report_dir or os.path.join(runner.project_working_directory, "reports") gen_html_report( summary, report_template=args.report_template, report_dir=report_dir, report_file=args.report_file ) err_code |= (0 if summary and summary["success"] else 1) except Exception as ex: color_print("!!!!!!!!!! exception stage: {} !!!!!!!!!!".format(runner.exception_stage), "YELLOW") color_print(str(ex), "RED") sentry_sdk.capture_exception(ex) err_code = 1 sys.exit(err_code)