Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions smart_tests/test_runners/smart_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,20 @@ def record_tests(client: RecordTests,
source_roots: Annotated[list[str], typer.Argument(
multiple=True,
help="Source directories containing test report files"
)]):
)],
file_path_attribute: Annotated[list[str], typer.Option(
"--file-path-attribute",
multiple=True,
help="Attributes to look for file paths in test reports (e.g., name, classname, file, filepath)"
)] = ["file", "filepath"]):
def path_builder(
case: TestCase, suite: TestSuite, report_file: str
) -> TestPath:
def find_filename():
"""look for what looks like file names from test reports"""
for e in [case, suite]:
for a in ["file", "filepath"]:
filepath = e._elem.attrib.get(a)
if filepath:
for a in file_path_attribute:
if filepath := e._elem.attrib.get(a):
return filepath
return None # failing to find a test name

Expand All @@ -188,9 +192,9 @@ def find_filename():

# default test path in `subset` expects to have this file name
test_path = [client.make_file_path_component(filepath)]
if suite.name:
if suite.name and suite.name != filepath:
test_path.append({"type": "testsuite", "name": suite.name})
if case.name:
if case.name and case.name != filepath:
test_path.append({"type": "testcase", "name": case.name})
return test_path

Expand Down
53 changes: 53 additions & 0 deletions tests/data/file/record_test_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"events": [
{
"type": "case",
"testPath": [
{
"type": "file",
"name": "tests/test_example.py"
},
{
"type": "testsuite",
"name": "Suite 1"
},
{
"type": "testcase",
"name": "test_case_1"
}
],
"duration": 0.5,
"status": 1,
"stdout": "",
"stderr": "",
"data": null
},
{
"type": "case",
"testPath": [
{
"type": "file",
"name": "tests/test_example.py"
},
{
"type": "testsuite",
"name": "Suite 1"
},
{
"type": "testcase",
"name": "test_case_2"
}
],
"duration": 0.734,
"status": 1,
"stdout": "",
"stderr": "",
"data": null
}
],
"testRunner": "file",
"group": "",
"noBuild": false,
"flavors": [],
"testSuite": ""
}
45 changes: 45 additions & 0 deletions tests/data/file/record_test_result_custom_attribute.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"events": [
{
"type": "case",
"testPath": [
{
"type": "file",
"name": "tests/test_custom.py"
},
{
"type": "testsuite",
"name": "Suite 1"
}
],
"duration": 0.5,
"status": 1,
"stdout": "",
"stderr": "",
"data": null
},
{
"type": "case",
"testPath": [
{
"type": "file",
"name": "tests/test_custom2.py"
},
{
"type": "testsuite",
"name": "Suite 1"
}
],
"duration": 0.734,
"status": 1,
"stdout": "",
"stderr": "",
"data": null
}
],
"testRunner": "file",
"group": "",
"noBuild": false,
"flavors": [],
"testSuite": ""
}
9 changes: 9 additions & 0 deletions tests/data/file/result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Test Suite" time="1.234" tests="2" failures="0">
<testsuite name="Suite 1" timestamp="2025-10-27T01:20:06" tests="2" file="tests/test_example.py" time="1.234" failures="0">
<testcase name="test_case_1" time="0.500" classname="TestExample">
</testcase>
<testcase name="test_case_2" time="0.734" classname="TestExample">
</testcase>
</testsuite>
</testsuites>
9 changes: 9 additions & 0 deletions tests/data/file/result_custom_attribute.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Test Suite" time="1.234" tests="2" failures="0">
<testsuite name="Suite 1" timestamp="2025-10-27T01:20:06" tests="2" time="1.234" failures="0">
<testcase time="0.500" name="tests/test_custom.py">
</testcase>
<testcase time="0.734" name="tests/test_custom2.py">
</testcase>
</testsuite>
</testsuites>
41 changes: 41 additions & 0 deletions tests/test_runners/test_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
from unittest import mock

import responses # type: ignore

from tests.cli_test_case import CliTestCase


class FileTest(CliTestCase):
@responses.activate
@mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.smart_tests_token})
def test_record_test_file(self):
result = self.cli(
"record",
"tests",
"--session",
self.session,
"file",
str(self.test_files_dir.joinpath("result.xml")),
)

self.assert_success(result)
self.assert_record_tests_payload("record_test_result.json")

@responses.activate
@mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.smart_tests_token})
def test_record_test_file_with_custom_file_path_attribute(self):
"""Test that --file-path-attribute option allows specifying custom attributes"""
result = self.cli(
"record",
"tests",
"--session",
self.session,
"file",
"--file-path-attribute",
"name",
str(self.test_files_dir.joinpath("result_custom_attribute.xml")),
)

self.assert_success(result)
self.assert_record_tests_payload("record_test_result_custom_attribute.json")
Loading