Skip to content

Commit

Permalink
Merge pull request #161 from launchableinc/ST-821-behave-runner
Browse files Browse the repository at this point in the history
[ST-821] support behave runner
  • Loading branch information
awilkes authored Mar 10, 2021
2 parents a021fd5 + 8ce07cf commit cda0ad7
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
52 changes: 52 additions & 0 deletions launchable/test_runners/behave.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 4 additions & 0 deletions tests/data/behave/record_test_result.json
Original file line number Diff line number Diff line change
@@ -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}
]
}
12 changes: 12 additions & 0 deletions tests/data/behave/reports/report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<testsuite name="tutorial.showing off behave" tests="1" errors="0" failures="0" skipped="0" time="0.000202" timestamp="2021-03-04T16:03:07.132143" hostname="localhost"><testcase classname="tutorial.showing off behave" name="run a simple test" status="passed" time="0.000202"><system-out>
<![CDATA[
@scenario.begin
Scenario: run a simple test
Given we have behave installed ... passed in 0.000s
When we implement a test ... passed in 0.000s
Then behave will test it for us! ... passed in 0.000s
@scenario.end
--------------------------------------------------------------------------------
]]>
</system-out></testcase></testsuite>
9 changes: 9 additions & 0 deletions tests/data/behave/subset_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"testPaths": [
[{"type": "file", "name": "tutorial.feature"}]
],
"target": 0.1,
"session": {
"id": "16"
}
}
43 changes: 43 additions & 0 deletions tests/test_runners/test_behave.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit cda0ad7

Please sign in to comment.