Commit 9f1384a1 authored by Luis Araujo's avatar Luis Araujo

Add support to fetch test case attachments

Now the --attachments option of the test command will fetch all
test run and test case attachments files.

These files will be saved in a directory hierarchy of the form
<attachments-dir>/<test-run-dir>/<test-case-dir>, and each of the files
will be saved in the specific category for each of the test runs and
test cases.

Also the output message is much simpler now, and it will only print
a logger info message after all the files have been saved to the
parent 390b3c43
......@@ -70,31 +70,60 @@ class TestCmd(Command):
for pkg in test.packages:
print "P: {} {}".format(pkg['name'], pkg['version'])
# Show test results if -r/--results passed
if self.args.results:
# Save test run attachments if --attachments is true
# and there are attachments available.
if self.args.attachments and test.attachments:
tr_attachments_path = _make_attachments_dir(
"{}-{}".format(, test.uuid))
for attachment in test.attachments:
_save_attachment(attachment, tr_attachments_path)
# Show test results if -r/--results passed and/or save test case
# attachments if --attachments option passed.
if self.args.results or self.args.attachments:
for result in test.results:
# Print results if --results is true
if self.args.results:
ms = ("{} {}".format(result.get('measurement', ''),
result.get('units', ''))).strip()
print "- {}: {} {}".format(result['test_case_id'],
(ms and '/ '+ ms) or '')
# Fetch and save test attachments inside directory
# Save test case attachments if --attachments is true
# and there are attachments available.
tc_attachments = \
self.args.attachments and result.get('attachments', [])
if tc_attachments:
# Test case attachments path is the
# test_run_atttachments path + test_case_id.
tc_attachments_path = _make_attachments_dir(
for attachment in tc_attachments:
_save_attachment(attachment, tc_attachments_path)
if self.args.attachments:
# Create target directory if it doesn't exist
if not os.path.exists(self.args.attachments):
os.mkdir(self.args.attachments)"Attachments saved to: {}".format(\
for attachment in test.attachments:
def _make_attachments_dir(attachments_dir, test_dir):
dir_pathname = os.path.join(attachments_dir, test_dir)
if not os.path.exists(dir_pathname):
except EnvironmentError as e:
return dir_pathname
def _save_attachment(attachment, directory):
# Replace any '/' in the attachment name by '_'
name = attachment['pathname'].replace('/', '_')
# Object path is: test-name+test-uuid+attachment-name
filename = "{}-{}_{}".format(, test.uuid, name)"Saving attachment {} into directory {}"
.format(filename, self.args.attachments))
pathname = os.path.join(self.args.attachments, filename)
filename = attachment['pathname'].replace('/', '_')
attachment_pathname = os.path.join(directory, filename)
with open(pathname, 'w') as f:
with open(attachment_pathname, 'w') as f:
except EnvironmentError as e:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment