-
Notifications
You must be signed in to change notification settings - Fork 216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add junit output callback #853
Open
Minipada
wants to merge
5
commits into
google:master
Choose a base branch
from
synapticon:junit_output
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"""Module for outputting test record to JUNIT-formatted files.""" | ||
|
||
import base64 | ||
import six | ||
from junit_xml import TestSuite, TestCase | ||
|
||
from openhtf.output import callbacks | ||
from openhtf.util import data | ||
|
||
|
||
class OutputToJUNIT(callbacks.OutputToFile): | ||
"""Return an output callback that writes JUNIT Test Records. | ||
Example filename_patterns might be: | ||
'/data/test_records/{dut_id}.{metadata[test_name]}.xml', indent=4)) or | ||
'/data/test_records/%(dut_id)s.%(start_time_millis)s' | ||
To use this output mechanism: | ||
test = openhtf.Test(PhaseOne, PhaseTwo) | ||
test.add_output_callback(openhtf.output.callbacks.OutputToJUNIT( | ||
'/data/test_records/{dut_id}.{metadata[test_name]}.xml')) | ||
|
||
Args: | ||
filename_pattern: A format string specifying the filename to write to, | ||
will be formatted with the Test Record as a dictionary. May also be a | ||
file-like object to write to directly. | ||
inline_attachments: Whether attachments should be included inline in the | ||
output. Set to False if you expect to have large binary attachments. If | ||
True (the default), then attachments are base64 encoded to allow for | ||
binary data that's not supported by JUNIT directly. | ||
""" | ||
|
||
def __init__(self, filename_pattern=None, **kwargs): | ||
super(OutputToJUNIT, self).__init__(filename_pattern) | ||
self.test_cases = [] | ||
|
||
def serialize_test_record(self, test_record): | ||
dict_test_record = self.convert_to_dict(test_record) | ||
test_case = None | ||
|
||
for phase in dict_test_record["phases"]: | ||
output = [] | ||
for _, phase_data in phase["measurements"].items(): | ||
log_output = [] | ||
|
||
for log_data in dict_test_record["log_records"]: | ||
if phase["name"] in log_data["logger_name"]: | ||
log_output.append(log_data["message"]) | ||
|
||
output.extend(["name: " + phase_data["name"], | ||
"validators: " + str(phase_data["validators"]), | ||
"measured_value: " + str(phase_data["measured_value"]), | ||
"outcome: " + phase_data["outcome"], | ||
"log: " + "\n".join(log_output), "\n"]) | ||
|
||
if phase["outcome"] == "PASS": | ||
test_case = TestCase(phase["name"], | ||
dict_test_record["dut_id"] + "." + | ||
dict_test_record["metadata"]["test_name"], | ||
(phase["end_time_millis"] - | ||
phase["start_time_millis"]) / 1000, | ||
"\n".join(output), | ||
'') | ||
else: | ||
test_case = TestCase(phase["name"], | ||
dict_test_record["dut_id"] + "." + | ||
dict_test_record["metadata"]["test_name"], | ||
(phase["end_time_millis"] - | ||
phase["start_time_millis"]) / 1000, | ||
'', | ||
"\n".join(output)) | ||
|
||
test_case.add_failure_info("failure message") | ||
|
||
self.test_cases.append(test_case) | ||
|
||
return TestSuite.to_xml_string([TestSuite("test", self.test_cases)]) | ||
|
||
def convert_to_dict(self, test_record): | ||
as_dict = data.convert_to_base_types(test_record) | ||
|
||
for phase, original_phase in zip(as_dict['phases'], test_record.phases): | ||
for name, attachment in six.iteritems(phase['attachments']): | ||
original_data = original_phase.attachments[name].data | ||
attachment['data'] = base64.standard_b64encode( | ||
original_data).decode('utf-8') | ||
return as_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was having an issue with this throwing an exception because there was no "validators" entry in the phase_data dictionary for a measurement generated by a monitor. The change below resolved that issue with me.