diff --git a/launchable/test_runners/behave.py b/launchable/test_runners/behave.py new file mode 100644 index 000000000..335562950 --- /dev/null +++ b/launchable/test_runners/behave.py @@ -0,0 +1,52 @@ +import click +from . import launchable +from xml.etree import ElementTree as ET +import os + + +@click.argument('reports', required=True, nargs=-1) +@launchable.record.tests +def record_tests(client, reports): + for r in reports: + client.report(r) + + def parse_func(p: str) -> ET.ElementTree: + tree = ET.parse(p) + for suite in tree.iter("testsuite"): + if len(suite) == 0: + continue + + name = suite.get('name') + if name is None: + continue + + suite_name = name.split('.') + if len(suite_name) < 2: + continue + + file_name = suite_name[0] + ".feature" + class_name = suite_name[1] + suite.attrib.update({"filepath": file_name}) + + for case in suite: + case.attrib.update({"classname": class_name}) + + return tree + + client._junitxml_parse_func = parse_func + client.run() + + +@launchable.subset +def subset(client): + for t in client.stdin(): + if 0 < t.find(".feature"): + paths = os.path.split(t) + if len(paths) < 2: + continue + + file = paths[1] + client.test_path(file.rstrip('\n')) + + client.separator = "|" + client.run() diff --git a/tests/data/behave/record_test_result.json b/tests/data/behave/record_test_result.json new file mode 100644 index 000000000..a9adff40e --- /dev/null +++ b/tests/data/behave/record_test_result.json @@ -0,0 +1,4 @@ +{"events": [ + {"type": "case", "testPath": [{"type": "file", "name": "tutorial.feature"}, {"type": "class", "name": "showing off behave"}, {"type": "testcase", "name": "run a simple test", "_lineno": null}], "duration": 0.000202, "status": 1, "stdout": "", "stderr": "", "data": null} +] +} diff --git a/tests/data/behave/reports/report.xml b/tests/data/behave/reports/report.xml new file mode 100644 index 000000000..fb94d247d --- /dev/null +++ b/tests/data/behave/reports/report.xml @@ -0,0 +1,12 @@ + + + \ No newline at end of file diff --git a/tests/data/behave/subset_result.json b/tests/data/behave/subset_result.json new file mode 100644 index 000000000..2f6813586 --- /dev/null +++ b/tests/data/behave/subset_result.json @@ -0,0 +1,9 @@ +{ + "testPaths": [ + [{"type": "file", "name": "tutorial.feature"}] + ], + "target": 0.1, + "session": { + "id": "16" + } +} diff --git a/tests/test_runners/test_behave.py b/tests/test_runners/test_behave.py new file mode 100644 index 000000000..4218bada2 --- /dev/null +++ b/tests/test_runners/test_behave.py @@ -0,0 +1,43 @@ +from pathlib import Path +from unittest import mock +import responses # type: ignore +import json +import gzip +from tests.cli_test_case import CliTestCase + + +class BehaveTest(CliTestCase): + test_files_dir = Path(__file__).parent.joinpath( + '../data/behave/').resolve() + + @responses.activate + def test_subset(self): + pipe = "tutorial.feature" + result = self.cli('subset', '--target', '10%', '--session', + self.session, 'behave', input=pipe) + self.assertEqual(result.exit_code, 0) + + payload = json.loads(gzip.decompress( + responses.calls[0].request.body).decode()) + + expected = self.load_json_from_file( + self.test_files_dir.joinpath('subset_result.json')) + + self.assert_json_orderless_equal(expected, payload) + + @ responses.activate + def test_record_test_maven(self): + result = self.cli('record', 'tests', '--session', self.session, + 'behave', str(self.test_files_dir) + "/reports/report.xml") + self.assertEqual(result.exit_code, 0) + + payload = json.loads(gzip.decompress( + b''.join(responses.calls[0].request.body)).decode()) + + for e in payload["events"]: + del e["created_at"] + + expected = self.load_json_from_file( + self.test_files_dir.joinpath("record_test_result.json")) + + self.assert_json_orderless_equal(expected, payload)