Last active
September 14, 2018 19:48
-
-
Save m1yag1/c377379ac13aa894997a20a2de90b316 to your computer and use it in GitHub Desktop.
Import a csv export of TestRail test cases into GitHub as user stories
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
import csv | |
import os | |
from datetime import datetime | |
# Requires pip install github3.py | |
from github3 import login | |
# Requires pip install jinja2 | |
from jinja2 import Template | |
gh_issue_template = '''# CASE NUM: {{ case_id }} | |
## User story | |
{{ story }} | |
## Preconditions | |
{{ preconditions }} | |
## Steps | |
{{ steps }} | |
## Expected Result | |
{{ expected_result }} | |
''' | |
def get_rows(filename): | |
"""A generator that returns rows of a csv as a dictionary """ | |
with open(filename, 'r', encoding='ISO-8859-1') as csvfile: | |
datareader = csv.DictReader(csvfile) | |
for row in datareader: | |
yield {key: value for key, value in row.items()} | |
def parse_title(title): | |
"""Exports from test rail seem to have numbers at the beginning. | |
Parse out numbers if they exist or return the title | |
""" | |
if title[0].isdigit(): | |
title = title.split(' - ')[1] | |
return title | |
def import_issue(repo, issue, created_at=None, milestone=None): | |
if milestone: | |
issue['milestone'] = milestone | |
if created_at: | |
issue['created_at'] = created_at | |
else: | |
issue['created_at'] = datetime.utcnow() | |
repo.import_issue(**issue) | |
return | |
def parse_test_case(row): | |
"""Take in a row from a test case export and return the necessary info as an issue""" | |
case_id = row['ID'] | |
title = '{} - {}'.format(row['Section Hierarchy'].split(' > ')[1], row['Title']) | |
section = row['Section Hierarchy'] | |
section_description = row['Section Description'] | |
story = 'AAT, I\'d like an automated test that asserts {}'.format(parse_title(row['Title'])) | |
preconditions = row['Preconditions'] | |
steps = row['Steps'] | |
expected_result = row['Expected Result'] | |
template = Template(gh_issue_template) | |
body = template.render(case_id=case_id, | |
section=section, | |
section_description=section_description, | |
story=story, | |
preconditions=preconditions, | |
steps=steps, | |
expected_result=expected_result) | |
return {'title': title, 'body': body} | |
if __name__ == '__main__': | |
user = os.environ['GITHUB_USER'] | |
password = os.environ['GITHUB_PASSWORD'] | |
repo = 'cnx-automation' | |
milestone = 1 | |
gh = login(user, password=password) | |
repo = gh.repository('openstax', repo) | |
for row in get_rows('input/webview_test_cases.csv'): | |
issue = parse_test_case(row) | |
import_issue(repo, issue, milestone=milestone) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment