From 99c6344849ed1e7fe6ba637f1fe0703c937d60cf Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 9 Mar 2021 11:44:43 +0900 Subject: [PATCH 1/3] add behave plugin --- launchable/test_runners/behave.py | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 launchable/test_runners/behave.py diff --git a/launchable/test_runners/behave.py b/launchable/test_runners/behave.py new file mode 100644 index 000000000..bfb911d00 --- /dev/null +++ b/launchable/test_runners/behave.py @@ -0,0 +1,48 @@ +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 + + suite_name = suite.get('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() From bbc932992250a9895195ae1517d18466ac19b1bc Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 9 Mar 2021 16:41:22 +0900 Subject: [PATCH 2/3] add behave's test --- tests/data/behave/record_test_result.json | 4 +++ tests/data/behave/reports/report.xml | 12 +++++++ tests/data/behave/subset_result.json | 9 +++++ tests/test_runners/test_behave.py | 43 +++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 tests/data/behave/record_test_result.json create mode 100644 tests/data/behave/reports/report.xml create mode 100644 tests/data/behave/subset_result.json create mode 100644 tests/test_runners/test_behave.py 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) From 8ce07cf92d29f3de1bdc5eb6e66c6c98ea13f053 Mon Sep 17 00:00:00 2001 From: Ryosuke Yabuki Date: Tue, 9 Mar 2021 16:54:37 +0900 Subject: [PATCH 3/3] fix type checks --- launchable/test_runners/behave.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/launchable/test_runners/behave.py b/launchable/test_runners/behave.py index bfb911d00..335562950 100644 --- a/launchable/test_runners/behave.py +++ b/launchable/test_runners/behave.py @@ -16,7 +16,11 @@ def parse_func(p: str) -> ET.ElementTree: if len(suite) == 0: continue - suite_name = suite.get('name').split('.') + name = suite.get('name') + if name is None: + continue + + suite_name = name.split('.') if len(suite_name) < 2: continue