Created
September 9, 2013 18:16
-
-
Save dnozay/6499423 to your computer and use it in GitHub Desktop.
small testlink CLI using TestLink-API-Python-client
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
# -*- coding: UTF-8 -*- | |
# small testlink CLI using TestLink-API-Python-client | |
# this is useful if you want to insert testcase data into testlink. | |
# | |
# +------------------+-----------+---------+--------------------+-------------+ | |
# | Test Case Name | summary | steps | Expected Results | Execution | | |
# +------------------+-----------+---------+--------------------+-------------+ | |
# | ... | ... | ... | ... | ... | | |
# | |
# | |
# See also the very nice google spreadsheet script | |
# exportJSON (https://gist.github.com/daaain/3932602) | |
# | |
# This takes a google spreadsheet with frozen rows as an input and gives you the json. | |
# A frozen row is a row that stays "frozen" on top when you scroll. The caveat is that | |
# it won't produce anything if there isn't at least one frozen row. | |
# | |
# If you use the columns describe above you should be good. | |
# | |
import sys | |
import os | |
import json | |
import getpass | |
from argparse import ArgumentParser | |
from testlink import TestlinkAPIClient | |
ENVNAME_SERVER_URL = 'TESTLINK_API_PYTHON_SERVER_URL' | |
ENVNAME_APIKEY = 'TESTLINK_API_PYTHON_APIKEY' | |
DEFAULT_PROJECT = 'Default test project' | |
DEFAULT_TESTPLAN = 'Default test plan' | |
class _: | |
# simple class to keep globals in. | |
MANUAL = 1 | |
AUTOMATED = 2 | |
def setup_connection(args): | |
_.server_url = args.server_url | |
_.api_key = args.api_key | |
_.client = TestlinkAPIClient(args.server_url, args.api_key) | |
if _.client.checkDevKey() != True: | |
print "Error with the devKey." | |
sys.exit(-1) | |
def list_testplans(project): | |
'''list all the testplans''' | |
testplans = _.client.getProjectTestPlans(project['id']) | |
for t in testplans: | |
print '{:>4} * Testplan {id:>4}: {name!r}'.format('', **t) | |
def list_projects(args): | |
'''list all the projects and testplans''' | |
setup_connection(args) | |
projects = _.client.getProjects() | |
if projects: | |
print 'Projects:' | |
for p in projects: | |
print ' * Project {id:>4}: {name!r}'.format(**p) | |
list_testplans(p) | |
else: | |
print 'No projects.' | |
def ping(args): | |
'''simple ping test to check proper XMLRPC''' | |
setup_connection(args) | |
print _.client.ping() | |
def insert(args): | |
setup_connection(args) | |
proj = _.client.getTestProjectByName(args.project) | |
proj_id = proj['id'] | |
print proj_id | |
# plan = _.client.getTestPlanByName(args.project, args.testplan) | |
testsuite = _.client.createTestSuite(proj_id, args.testsuite, | |
"** generated from command-line, please fill in **") | |
assert testsuite[0]['message'] == 'ok' | |
testsuite_id = testsuite[0]['id'] | |
with open(args.json) as json_file: | |
testcases = json.load(json_file) | |
def any_key(tup): | |
same = tkeys & set(tup) | |
key = same.pop() | |
return key | |
for tcase in testcases: | |
tkeys = set(tcase.keys()) | |
# not everybody uses the correct template... give them some breathing room. | |
name = tcase.get(any_key(('Test Case Name', 'Test case name', 'ID'))) | |
summary = tcase.get(any_key(('summary', 'Objective'))) | |
steps = tcase.get(any_key(('steps', 'Procedure'))) | |
results = tcase.get(any_key(('Expected Results', 'Expected Result'))) | |
execution = tcase.get(any_key(('Execution', 'Automated'))) | |
automated = execution.strip().lower() in ('yes','automated') | |
if automated: | |
execution = _.AUTOMATED | |
else: | |
execution = _.MANUAL | |
_.client.initStep(steps, results, execution) | |
# tune this script here if you know how to handle more steps. | |
# _.client.appendStep("Step action 2", "Step result 2", _.AUTOMATED) | |
# _.client.appendStep("Step action 3", "Step result 3", _.MANUAL) | |
new_tcase = _.client.createTestCase(name, testsuite_id, | |
proj_id, args.user, summary, 'executiontype={0}'.format(execution)) | |
print '{0} - {1}'.format(name, new_tcase[0]['message']) | |
def parse_args(): | |
# parent parser accepts server_url / api_key | |
parent_parser = ArgumentParser(add_help=False) | |
parent_parser.add_argument('--server_url', default=os.getenv(ENVNAME_SERVER_URL, None), | |
help='TestLink Server URL of XMLRPC (default: %(default)s)') | |
parent_parser.add_argument('--api_key', default=os.getenv(ENVNAME_APIKEY, None), | |
help='API access key generated by TestLink under "My Settings" (default: %(default)s)') | |
parent_parser.add_argument('--user', default=os.getenv('USER'), | |
help='User to use for item creation / update (default: %(default)s)') | |
# create main parser | |
parser = ArgumentParser() | |
subparsers = parser.add_subparsers(help='sub-command help') | |
# list | |
parser_listp = subparsers.add_parser('list', parents=[parent_parser]) | |
parser_listp.set_defaults(func=list_projects) | |
# ping | |
parser_ping = subparsers.add_parser('ping', parents=[parent_parser]) | |
parser_ping.set_defaults(func=ping) | |
# insert | |
parser_insert = subparsers.add_parser('insert', parents=[parent_parser]) | |
parser_insert.add_argument('--project', default=DEFAULT_PROJECT, | |
help='Testlink project (default: %(default)s).') | |
parser_insert.add_argument('testsuite', | |
help='Testlink testsuite name to create.') | |
parser_insert.add_argument('json', | |
help='JSON file created from macro on google drive.') | |
parser_insert.set_defaults(func=insert) | |
args = parser.parse_args() | |
args.func(args) | |
def main(): | |
parse_args() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment