|
| 1 | +#! /usr/bin/python |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | + |
| 4 | +# Copyright 2012 pade (Paul Dassier), TestLink-API-Python-client developers |
| 5 | +# |
| 6 | +# Licensed under ??? |
| 7 | +# see https://github.com/orenault/TestLink-API-Python-client/issues/4 |
| 8 | + |
| 9 | +from testlinkapi import TestlinkAPIClient, TestLinkHelper |
| 10 | +from testlinkerrors import TestLinkError |
| 11 | +from datetime import date |
| 12 | + |
| 13 | + |
| 14 | +class TestLink(TestlinkAPIClient): |
| 15 | + """ |
| 16 | + TestLink API library |
| 17 | + provide a user friendly library, with more robustness and error management |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, server_url, key): |
| 21 | + """ |
| 22 | + Class initialisation |
| 23 | + """ |
| 24 | + super(TestLink, self).__init__(server_url, key) |
| 25 | + |
| 26 | + def getTestCaseIDByName(self, testCaseName, testSuiteName, testProjectName): |
| 27 | + """ |
| 28 | + Find a test case by its name, by its suite and its project |
| 29 | + Suite name must not be duplicate, so only one test case must be found |
| 30 | + Return test case id if success |
| 31 | + or raise TestLinkError exception with error message in case of error |
| 32 | + """ |
| 33 | + results = super(TestLink, self).getTestCaseIDByName(testCaseName, testSuiteName, testProjectName) |
| 34 | + if results[0].has_key("message"): |
| 35 | + raise TestLinkError(results[0]["message"]) |
| 36 | + elif len(results) > 1: |
| 37 | + raise TestLinkError("(getTestCaseIDByName) - Several case test found. Suite name must not be duplicate for the same project") |
| 38 | + else: |
| 39 | + if results[0]["name"] == testCaseName: |
| 40 | + return results[0]["id"] |
| 41 | + raise TestLinkError("(getTestCaseIDByName) - Internal server error. Return value is not expected one!") |
| 42 | + |
| 43 | + |
| 44 | + def reportResult(self, testResult, testCaseName, testSuiteName, testNotes="", **kwargs): |
| 45 | + """ |
| 46 | + Report results for test case |
| 47 | + Arguments are: |
| 48 | + - testResult: "p" for passed, "b" for blocked, "f" for failed |
| 49 | + - testCaseName: the test case name to report |
| 50 | + - testSuiteName: the test suite name that support the test case |
| 51 | + - testNotes: optional, if empty will be replace by a default string. To let it blank, just set testNotes to " " characters |
| 52 | + - an anonymous dictionnary with followings keys: |
| 53 | + - testProjectName: the project to fill |
| 54 | + - testPlanName: the active test plan |
| 55 | + - buildName: the active build. |
| 56 | + Raise a TestLinkError error with the error message in case of trouble |
| 57 | + Return the execution id needs to attach files to test execution |
| 58 | + """ |
| 59 | + |
| 60 | + # Check parameters |
| 61 | + for data in ["testProjectName", "testPlanName", "buildName"]: |
| 62 | + if not kwargs.has_key(data): |
| 63 | + raise TestLinkError("(reportResult) - Missing key %s in anonymous dictionnary" % data) |
| 64 | + |
| 65 | + # Get project id |
| 66 | + project = self.getTestProjectByName(kwargs["testProjectName"]) |
| 67 | + |
| 68 | + # Check if project is active |
| 69 | + if project['active'] != '1': |
| 70 | + raise TestLinkError("(reportResult) - Test project %s is not active" % kwargs["testProjectName"]) |
| 71 | + |
| 72 | + # Check test plan name |
| 73 | + plan = self.getTestPlanByName(kwargs["testProjectName"], kwargs["testPlanName"]) |
| 74 | + |
| 75 | + # Check is test plan is open and active |
| 76 | + if plan['is_open'] != '1' or plan['active'] != '1': |
| 77 | + raise TestLinkError("(reportResult) - Test plan %s is not active or not open" % kwargs["testPlanName"]) |
| 78 | + # Memorise test plan id |
| 79 | + planId = plan['id'] |
| 80 | + |
| 81 | + # Check build name |
| 82 | + build = self.getBuildByName(kwargs["testProjectName"], kwargs["testPlanName"], kwargs["buildName"]) |
| 83 | + |
| 84 | + # Check if build is open and active |
| 85 | + if build['is_open'] != '1' or build['active'] != '1': |
| 86 | + raise TestLinkError("(reportResult) - Build %s in not active or not open" % kwargs["buildName"]) |
| 87 | + |
| 88 | + # Get test case id |
| 89 | + caseId = self.getTestCaseIDByName(testCaseName, testSuiteName, kwargs["testProjectName"]) |
| 90 | + |
| 91 | + # Check results parameters |
| 92 | + if testResult not in "pbf": |
| 93 | + raise TestLinkError("(reportResult) - Test result must be 'p', 'f' or 'b'") |
| 94 | + |
| 95 | + if testNotes == "": |
| 96 | + # Builds testNotes if empty |
| 97 | + today = date.today() |
| 98 | + testNotes = "%s - Test performed automatically" % today.strftime("%c") |
| 99 | + elif testNotes == " ": |
| 100 | + #No notes |
| 101 | + testNotes = "" |
| 102 | + |
| 103 | + print "testNotes: %s" % testNotes |
| 104 | + # Now report results |
| 105 | + results = self.reportTCResult(caseId, planId, kwargs["buildName"], testResult, testNotes) |
| 106 | + # Check errors |
| 107 | + if results[0]["message"] != "Success!": |
| 108 | + raise TestLinkError(results[0]["message"]) |
| 109 | + |
| 110 | + return results[0]['id'] |
| 111 | + |
| 112 | + def getTestProjectByName(self, testProjectName): |
| 113 | + """ |
| 114 | + Return project |
| 115 | + A TestLinkError is raised in case of error |
| 116 | + """ |
| 117 | + results = super(TestLink, self).getTestProjectByName(testProjectName) |
| 118 | + if results[0].has_key("message"): |
| 119 | + raise TestLinkError(results[0]["message"]) |
| 120 | + |
| 121 | + return results[0] |
| 122 | + |
| 123 | + def getTestPlanByName(self, testProjectName, testPlanName): |
| 124 | + """ |
| 125 | + Return test plan |
| 126 | + A TestLinkError is raised in case of error |
| 127 | + """ |
| 128 | + results = super(TestLink, self).getTestPlanByName(testProjectName, testPlanName) |
| 129 | + if results[0].has_key("message"): |
| 130 | + raise TestLinkError(results[0]["message"]) |
| 131 | + |
| 132 | + return results[0] |
| 133 | + |
| 134 | + def getBuildByName(self, testProjectName, testPlanName, buildName): |
| 135 | + """ |
| 136 | + Return build corresponding to buildName |
| 137 | + A TestLinkError is raised in case of error |
| 138 | + """ |
| 139 | + plan = self.getTestPlanByName(testProjectName, testPlanName) |
| 140 | + builds = self.getBuildsForTestPlan(plan['id']) |
| 141 | + |
| 142 | + # Check if a builds exists |
| 143 | + if builds == '': |
| 144 | + raise TestLinkError("(getBuildByName) - Builds %s does not exists for test plan %s" % (buildName, testPlanName)) |
| 145 | + |
| 146 | + # Search the correct build name in the return builds list |
| 147 | + for build in builds: |
| 148 | + if build['name'] == buildName: |
| 149 | + return build |
| 150 | + |
| 151 | + # No build found with builName name |
| 152 | + raise TestLinkError("(getBuildByName) - Builds %s does not exists for test plan %s" % (buildName, testPlanName)) |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + tl_helper = TestLinkHelper() |
| 156 | + tl_helper.setParamsFromArgs() |
| 157 | + myTestLink = tl_helper.connect(TestLink) |
| 158 | + print myTestLink |
| 159 | + print myTestLink.about() |
0 commit comments