Skip to content

Commit f4ea3e9

Browse files
author
Luiko Czub
committed
TestLinkAPIClient.method(self, args) with self.method(args) replaced
also getProjectIDByName() fixed - break statement added orenault#2
1 parent 10ff326 commit f4ea3e9

File tree

2 files changed

+191
-33
lines changed

2 files changed

+191
-33
lines changed

src/testlink/testlinkapi.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -426,64 +426,58 @@ def countProjects(self):
426426
""" countProjects :
427427
Count all the test project
428428
"""
429-
projects=TestLinkAPIClient.getProjects(self)
429+
projects=self.getProjects()
430430
return len(projects)
431431

432432
def countTestPlans(self):
433433
""" countProjects :
434434
Count all the test plans
435435
"""
436-
projects=TestLinkAPIClient.getProjects(self)
436+
projects=self.getProjects()
437437
nbTP = 0
438438
for project in projects:
439-
ret = TestLinkAPIClient.getProjectTestPlans(self,project['id'])
439+
ret = self.getProjectTestPlans(project['id'])
440440
nbTP += len(ret)
441441
return nbTP
442442

443443
def countTestSuites(self):
444444
""" countProjects :
445445
Count all the test suites
446446
"""
447-
projects=TestLinkAPIClient.getProjects(self)
447+
projects=self.getProjects()
448448
nbTS = 0
449449
for project in projects:
450-
TestPlans = TestLinkAPIClient.getProjectTestPlans(self,
451-
project['id'])
450+
TestPlans = self.getProjectTestPlans(project['id'])
452451
for TestPlan in TestPlans:
453-
TestSuites = TestLinkAPIClient.getTestSuitesForTestPlan(self,
454-
TestPlan['id'])
452+
TestSuites = self.getTestSuitesForTestPlan(TestPlan['id'])
455453
nbTS += len(TestSuites)
456454
return nbTS
457455

458456
def countTestCasesTP(self):
459457
""" countProjects :
460458
Count all the test cases linked to a Test Plan
461459
"""
462-
projects=TestLinkAPIClient.getProjects(self)
460+
projects=self.getProjects()
463461
nbTC = 0
464462
for project in projects:
465-
TestPlans = TestLinkAPIClient.getProjectTestPlans(self,
466-
project['id'])
463+
TestPlans = self.getProjectTestPlans(project['id'])
467464
for TestPlan in TestPlans:
468-
TestCases = TestLinkAPIClient.getTestCasesForTestPlan(self,
469-
TestPlan['id'])
465+
TestCases = self.getTestCasesForTestPlan(TestPlan['id'])
470466
nbTC += len(TestCases)
471467
return nbTC
472468

473469
def countTestCasesTS(self):
474470
""" countProjects :
475471
Count all the test cases linked to a Test Suite
476472
"""
477-
projects=TestLinkAPIClient.getProjects(self)
473+
projects=self.getProjects()
478474
nbTC = 0
479475
for project in projects:
480-
TestPlans = TestLinkAPIClient.getProjectTestPlans(self,
481-
project['id'])
476+
TestPlans = self.getProjectTestPlans(project['id'])
482477
for TestPlan in TestPlans:
483-
TestSuites = TestLinkAPIClient.getTestSuitesForTestPlan(self,
484-
TestPlan['id'])
478+
TestSuites = self.getTestSuitesForTestPlan(TestPlan['id'])
485479
for TestSuite in TestSuites:
486-
TestCases = TestLinkAPIClient.getTestCasesForTestSuite(self,
480+
TestCases = self.getTestCasesForTestSuite(
487481
TestSuite['id'],'true','full')
488482
for TestCase in TestCases:
489483
nbTC += len(TestCases)
@@ -493,37 +487,33 @@ def countPlatforms(self):
493487
""" countPlatforms :
494488
Count all the Platforms
495489
"""
496-
projects=TestLinkAPIClient.getProjects(self)
490+
projects=self.getProjects()
497491
nbPlatforms = 0
498492
for project in projects:
499-
TestPlans = TestLinkAPIClient.getProjectTestPlans(self,
500-
project['id'])
493+
TestPlans = self.getProjectTestPlans(project['id'])
501494
for TestPlan in TestPlans:
502-
Platforms = TestLinkAPIClient.getTestPlanPlatforms(self,
503-
TestPlan['id'])
495+
Platforms = self.getTestPlanPlatforms(TestPlan['id'])
504496
nbPlatforms += len(Platforms)
505497
return nbPlatforms
506498

507499
def countBuilds(self):
508500
""" countBuilds :
509501
Count all the Builds
510502
"""
511-
projects=TestLinkAPIClient.getProjects(self)
503+
projects=self.getProjects()
512504
nbBuilds = 0
513505
for project in projects:
514-
TestPlans = TestLinkAPIClient.getProjectTestPlans(self,
515-
project['id'])
506+
TestPlans = self.getProjectTestPlans(project['id'])
516507
for TestPlan in TestPlans:
517-
Builds = TestLinkAPIClient.getBuildsForTestPlan(self,
518-
TestPlan['id'])
508+
Builds = self.getBuildsForTestPlan(TestPlan['id'])
519509
nbBuilds += len(Builds)
520510
return nbBuilds
521511

522512
def listProjects(self):
523513
""" listProjects :
524514
Lists the Projects (display Name & ID)
525515
"""
526-
projects=TestLinkAPIClient.getProjects(self)
516+
projects=self.getProjects()
527517
for project in projects:
528518
print "Name: %s ID: %s " % (project['name'], project['id'])
529519

@@ -554,12 +544,12 @@ def appendStep(self, actions, expected_results, execution_type):
554544
return True
555545

556546
def getProjectIDByName(self, projectName):
557-
projects=self._callServer('getProjects', {'devKey' : self.devKey})
547+
projects=self.getProjects()
548+
result=-1
558549
for project in projects:
559550
if (project['name'] == projectName):
560551
result = project['id']
561-
else:
562-
result = -1
552+
break
563553
return result
564554

565555
def __str__(self):
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#! /usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
# Copyright 2012 Luiko Czub, TestLink-API-Python-client developers
5+
#
6+
# Licensed under ???
7+
# see https://github.com/orenault/TestLink-API-Python-client/issues/4
8+
9+
# this test works WITHOUT an online TestLink Server
10+
# no calls are send to a TestLink Server
11+
12+
import unittest
13+
from testlink import TestLinkAPIClient, TestLinkHelper
14+
15+
# scenario_a includes response from a testlink 1.9.3 server
16+
SCENARIO_A = {'getProjects' : [
17+
{'opt': {'requirementsEnabled': 0, 'testPriorityEnabled': 1,
18+
'automationEnabled': 1, 'inventoryEnabled': 0},
19+
'prefix': 'NPROAPI', 'name': 'NEW_PROJECT_API', 'color': '',
20+
'notes': 'This is a Project created with the API',
21+
'option_priority': '0',
22+
'options': 'O:8:"stdClass":4:{s:19:"requirementsEnabled";i:0;s:19:"testPriorityEnabled";i:1;s:17:"automationEnabled";i:1;s:16:"inventoryEnabled";i:0;}',
23+
'tc_counter': '2', 'option_reqs': '0', 'active': '1',
24+
'is_public': '1', 'id': '21', 'option_automation': '0'},
25+
{'opt': {'requirementsEnabled': 1, 'testPriorityEnabled': 1,
26+
'automationEnabled': 1, 'inventoryEnabled': 1},
27+
'prefix': 'TP', 'name': 'TestProject', 'color': '',
28+
'notes': '<p>Initiales TestProject, um &nbsp;TestLink kennen zu lernen</p>',
29+
'option_priority': '0',
30+
'options': 'O:8:"stdClass":4:{s:19:"requirementsEnabled";i:1;s:19:"testPriorityEnabled";i:1;s:17:"automationEnabled";i:1;s:16:"inventoryEnabled";i:1;}',
31+
'tc_counter': '0', 'option_reqs': '0', 'active': '1',
32+
'is_public': '1', 'id': '1', 'option_automation': '0'}],
33+
'getProjectTestPlans' : {
34+
'21' : [{'name': 'TestPlan_API',
35+
'notes': 'New TestPlan created with the API',
36+
'active': '1', 'is_public': '1',
37+
'testproject_id': '21', 'id': '22'}] ,
38+
'1' : '' },
39+
'getFirstLevelTestSuitesForTestProject' : {
40+
'21' : [{'node_type_id': '2', 'name': 'A - First Level',
41+
'parent_id': '21', 'node_order': '0',
42+
'node_table': 'testsuites', 'id': '23'},
43+
{'node_type_id': '2', 'name': 'B - First Level',
44+
'parent_id': '21', 'node_order': '0',
45+
'node_table': 'testsuites', 'id': '24'}],
46+
'1' : [{'message': '(getFirstLevelTestSuitesForTestProject) - Test Project (TestProject) is empty.',
47+
'code': 7008}] },
48+
'getTestSuitesForTestPlan' : {'22' : ''},
49+
'getTestCasesForTestPlan' : {'22' : ''},
50+
# TL(1.9.3)->getTestSuitesForTestSuite really returns {...} and not [{....}] !!!
51+
'getTestSuitesForTestSuite' : {
52+
'23' : {'node_type_id': '2', 'name': 'AA - Second Level',
53+
'parent_id': '23', 'node_order': '0',
54+
'details': 'Details of the Test Suite AA', 'id': '25'},
55+
'24' : ''},
56+
'getTestCasesForTestSuite' : {
57+
'23' : [{'node_type_id': '3', 'tcversion_id': '25',
58+
'name': 'TESTCASE_AA', 'parent_id': '25',
59+
'node_order': '0', 'node_table': 'testcases',
60+
'external_id': 'NPROAPI-1', 'id': '26'}],
61+
'24' : [{'node_type_id': '3', 'tcversion_id': '24',
62+
'name': 'TESTCASE_B', 'parent_id': '24',
63+
'node_order': '0', 'node_table': 'testcases',
64+
'external_id': 'NPROAPI-2', 'id': '33'}],
65+
'25' : [{'node_type_id': '3', 'tcversion_id': '25',
66+
'name': 'TESTCASE_AA', 'parent_id': '25',
67+
'node_order': '0', 'node_table': 'testcases',
68+
'external_id': 'NPROAPI-1', 'id': '26'}]
69+
},
70+
'getTestPlanPlatforms' : {
71+
'22' : [{'message': 'Test plan (name:TestPlan_API) has no platforms linked',
72+
'code': 3041}]},
73+
'getBuildsForTestPlan' : {'22' : ''}
74+
}
75+
76+
class DummyAPIClient(TestLinkAPIClient):
77+
""" Dummy for Simulation TestLinkAPICLient.
78+
Overrides _callServer() Method to return test scenarios
79+
"""
80+
81+
def __init__(self, server_url, devKey):
82+
super(DummyAPIClient, self).__init__(server_url, devKey)
83+
self.scenario_data = {}
84+
85+
def loadScenario(self, a_scenario):
86+
self.scenario_data = a_scenario
87+
88+
def _callServer(self, methodAPI, argsAPI=None):
89+
data = self.scenario_data[methodAPI]
90+
response = None
91+
if methodAPI in ['getProjectTestPlans',
92+
'getFirstLevelTestSuitesForTestProject']:
93+
response = data[argsAPI['testprojectid']]
94+
elif methodAPI in ['getTestSuitesForTestPlan',
95+
'getTestCasesForTestPlan', 'getTestPlanPlatforms',
96+
'getBuildsForTestPlan']:
97+
response = data[argsAPI['testplanid']]
98+
elif methodAPI in ['getTestCasesForTestSuite',
99+
'getTestSuitesForTestSuite']:
100+
response = data[argsAPI['testsuiteid']]
101+
else:
102+
response = data
103+
return response
104+
105+
106+
class TestLinkAPIOfflineTestCase(unittest.TestCase):
107+
""" TestCases for TestLinkAPIClient - does not interacts with a TestLink Server.
108+
works with DummyAPIClientm which returns special test data
109+
"""
110+
111+
def setUp(self):
112+
self.api = TestLinkHelper().connect(DummyAPIClient)
113+
114+
# def tearDown(self):
115+
# pass
116+
117+
118+
def test_countProjects(self):
119+
self.api.loadScenario(SCENARIO_A)
120+
response = self.api.countProjects()
121+
self.assertEqual(2, response)
122+
123+
def test_countTestPlans(self):
124+
self.api.loadScenario(SCENARIO_A)
125+
response = self.api.countTestPlans()
126+
self.assertEqual(1, response)
127+
128+
def test_countTestSuites(self):
129+
self.api.loadScenario(SCENARIO_A)
130+
response = self.api.countTestSuites()
131+
self.assertEqual(0, response)
132+
133+
def test_countTestCasesTP(self):
134+
self.api.loadScenario(SCENARIO_A)
135+
response = self.api.countTestCasesTP()
136+
self.assertEqual(0, response)
137+
138+
def test_countTestCasesTS(self):
139+
self.api.loadScenario(SCENARIO_A)
140+
response = self.api.countTestCasesTS()
141+
self.assertEqual(0, response)
142+
143+
def test_countPlatforms(self):
144+
self.api.loadScenario(SCENARIO_A)
145+
response = self.api.countPlatforms()
146+
self.assertEqual(0, response)
147+
148+
def test_countBuilds(self):
149+
self.api.loadScenario(SCENARIO_A)
150+
response = self.api.countBuilds()
151+
self.assertEqual(0, response)
152+
153+
def test_listProjects(self):
154+
self.api.loadScenario(SCENARIO_A)
155+
self.api.listProjects()
156+
# no assert check cause method returns nothing
157+
# 'just' prints to stdout
158+
159+
def test_getProjectIDByName(self):
160+
self.api.loadScenario(SCENARIO_A)
161+
response = self.api.getProjectIDByName('NEW_PROJECT_API')
162+
self.assertEqual('21', response)
163+
response = self.api.getProjectIDByName('UNKNOWN_PROJECT')
164+
self.assertEqual(-1, response)
165+
166+
if __name__ == "__main__":
167+
#import sys;sys.argv = ['', 'Test.testName']
168+
unittest.main()

0 commit comments

Comments
 (0)