def __call__(self, result: TestReport) -> TestReport: """ :param result: testplan result to export """ exporter = JSONExporter(json_path=self.output) exporter.export(result) return result
def test_split_and_merge(): """ Test static methods used for splitting and merging JSON report. """ meta, structure, assertions = JSONExporter.split_json_report( copy.deepcopy(full_report)) assert meta == meta_part assert structure == structure_part assert assertions == assertions_part assert (JSONExporter.merge_json_report(meta, structure, assertions) == full_report)
def test_json_exporter(runpath): """ JSON Exporter should generate a json report at the given `json_path`. """ json_path = os.path.join(runpath, "report.json") plan = TestplanMock("plan", exporters=JSONExporter(json_path=json_path), runpath=runpath) multitest_1 = multitest.MultiTest(name="Primary", suites=[Alpha()]) multitest_2 = multitest.MultiTest(name="Secondary", suites=[Beta()]) plan.add(multitest_1) plan.add(multitest_2) plan.run() assert os.path.exists(json_path) assert os.stat(json_path).st_size > 0 # Load the JSON file to validate it contains valid JSON. with open(json_path) as json_file: report = json.load(json_file) # Check that the expected text file is attached correctly. attachments_dir = os.path.join(os.path.dirname(json_path), "_attachments") assert os.path.isdir(attachments_dir) assert len(report["attachments"]) == 1 dst_path = list(report["attachments"].keys())[0] attachment_filepath = os.path.join(attachments_dir, dst_path) assert os.path.isfile(attachment_filepath) with open(attachment_filepath) as f: attachment_file_contents = f.read() assert attachment_file_contents == "testplan\n" * 100
def __call__(self, result: TestReport) -> TestReport: # TPR handles commands with a pipeline, resulting in calls like # - tpr convert fromjson $JSON_FILE display # - tpr convert fromdb $DOC_ID display # reading data from input and transform it into `TestReport`, then # the output of actual command becomes input of the next (display). # A `WebUIServer` is used to save the data into a temporary single JSON # which will be displayed it in browser. If the original input file is # a single JSON, this process might be optimized. But here, we stick to # the pipeline concept to make implementation consistent. with tempfile.TemporaryDirectory() as tmpdir: json_path = os.path.join(tmpdir, "report.json") exporter = JSONExporter(json_path=json_path, split_json_report=False) exporter.export(result) ui_server = WebUIServer(json_path=json_path, ui_port=self.port) ui_server.display() ui_server.wait_for_kb_interrupt() return result
def test_json_exporter(tmpdir): """ JSON Exporter should generate a json report at the given `json_path`. """ json_path = tmpdir.mkdir('reports').join('report.json').strpath with log_propagation_disabled(TESTPLAN_LOGGER): plan = Testplan(name='plan', parse_cmdline=False, exporters=JSONExporter(json_path=json_path)) multitest_1 = MultiTest(name='Primary', suites=[Alpha()]) multitest_2 = MultiTest(name='Secondary', suites=[Beta()]) plan.add(multitest_1) plan.add(multitest_2) plan.run() assert os.path.exists(json_path) assert os.stat(json_path).st_size > 0
def __call__(self, result: TestReport) -> TestReport: exporter = JSONExporter(json_path=self.output) exporter.export(result) return result
def test_json_exporter_split_report(runpath): """ JSON Exporter should generate a json report at the given `json_path`. """ json_path = os.path.join(runpath, "report.json") plan = TestplanMock( "plan", exporters=[JSONExporter(json_path=json_path, split_json_report=True)], runpath=runpath, ) multitest_1 = multitest.MultiTest(name="Primary", suites=[Alpha()]) multitest_2 = multitest.MultiTest(name="Secondary", suites=[Beta()]) plan.add(multitest_1) plan.add(multitest_2) plan.run() assert os.path.exists(json_path) assert os.stat(json_path).st_size > 0 # Load the JSON file to validate it contains valid JSON. with open(json_path) as json_file: report = json.load(json_file) assert report["version"] == 2 # Check that the expected text file is attached correctly. attachments_dir = os.path.join(os.path.dirname(json_path), "_attachments") assert os.path.isdir(attachments_dir) assert len(report["entries"]) == 0 assert len(report["attachments"]) == 3 structure_filename, assertions_filename = gen_attached_report_names( json_path) assert structure_filename in report["attachments"] assert assertions_filename in report["attachments"] assert report["structure_file"] == structure_filename assert report["assertions_file"] == assertions_filename structure_filepath = os.path.join(attachments_dir, structure_filename) assertions_filepath = os.path.join(attachments_dir, assertions_filename) assert os.path.isfile(structure_filepath) assert os.path.isfile(assertions_filepath) with open(structure_filepath) as f1, open(assertions_filepath) as f2: structure = json.loads(f1.read()) assertions = json.loads(f2.read()) assert len(structure) == 2 # 2 multitests assert structure[0]["name"] == "Primary" # 1st multitest name assert len(structure[0]["entries"]) == 1 # one suite in 1st multitest assert structure[0]["entries"][0]["name"] == "Alpha" # 1st suite name assert len(structure[0]["entries"][0]["entries"]) == 3 # 3 testcases assert structure[1]["name"] == "Secondary" # 2nd multitest name assert len(structure[1]["entries"]) == 1 # one suite in 2nd multitest assert structure[1]["entries"][0]["name"] == "Beta" # 1st suite name assert len(structure[1]["entries"][0]["entries"]) == 2 # 2 testcases assert len(assertions["plan"]) == 2 assert len(assertions["plan"]["Primary"]) == 1 assert len(assertions["plan"]["Primary"]["Alpha"]) == 3 assert len(assertions["plan"]["Secondary"]) == 1 assert len(assertions["plan"]["Secondary"]["Beta"]) == 2 # only one assertion in each testcase in suite `Alpha` assert (assertions["plan"]["Primary"]["Alpha"]["test_comparison"][0] ["type"] == "Equal") assert (assertions["plan"]["Primary"]["Alpha"]["test_membership"][0] ["type"] == "Contain") assert (assertions["plan"]["Primary"]["Alpha"]["test_attach"][0]["type"] == "Attachment") # 2 assertions in testcase `test_failure` assert (assertions["plan"]["Secondary"]["Beta"]["test_failure"][0]["type"] == "Equal") assert (assertions["plan"]["Secondary"]["Beta"]["test_failure"][1]["type"] == "NotEqual") # no assertion in testcase `test_error` assert len(assertions["plan"]["Secondary"]["Beta"]["test_error"]) == 0
def to_json(result): exporter = JSONExporter(json_path=output) exporter.export(result) return result