当前位置: 首页 > 工具软件 > TestLink > 使用案例 >

Python 对testlink的操作

裴韬
2023-12-01

摘要: 
TestLink 是基于web的测试用例管理系统,主要功能是测试用例的创建、管理和执行,并且还提供了一些简单的统计功能。

主要内容: 
testlink官网:http://www.testlink.org/

1. python 2.7 安装testlink命令:pip install TestLink-API-Python-client==0.4.7
2. Python连接上testlink可进行以下操作

#encoding=utf-8
import testlink, time
manual = 1  # manual
automation = 2  # automation
import ssl
# ssl._create_default_https_context = ssl._create_unverified_context
# connect to test link
# The API address of the test link server, only requires modification the IP section
url = "https://testlink部署的服务器IP/lib/api/xmlrpc/v1/xmlrpc.php"
key = "adjl8292uujsjiiwj1911gljdflsjgksljg1"  # test link personal key
my_testlink = testlink.TestLinkHelper(url, key).connect(testlink.TestlinkAPIClient)
# print("tlc information is: %s " % tlc)


class TestlinkProcess():

    #  get information of test link上, all of the following function are validated(except the last)
    def get_information_test_project(self):
            print("Number of Projects      in TestLink: %s " % my_testlink.countProjects())
            print("Number of Platforms  (in TestPlans): %s " % my_testlink.countPlatforms())
            print("Number of Builds                   : %s " % my_testlink.countBuilds())
            print("Number of TestPlans                : %s " % my_testlink.countTestPlans())
            # print("Number of TestSuites               : %s " % tlc.countTestSuites())
            # print("Number of TestCases (in TestSuites): %s " % tlc.countTestCasesTS())
            # print("Number of TestCases (in TestPlans) : %s " % tlc.countTestCasesTP()) # have something wrong
            # tlc.listProjects()

    # get test project, validated
    def get_test_project(self):
        projects = my_testlink.getProjects()
        for project in projects:
            print("testProject", project["id"], project["name"])

    # get test plan, validated
    def get_test_plan(self, project_id):
        test_plans = my_testlink.getProjectTestPlans(project_id)
        for test_plan in test_plans:
            print("testPlan", test_plan["id"], test_plan["name"])


    # get test suite, validated
    def get_test_suites_by_test_plan(self, test_plan_id):
        test_suites = my_testlink.getTestSuitesForTestPlan(test_plan_id)
        test_suites_type = type(test_suites)
        print(test_suites_type)
        for test_suite in test_suites:
            print("testSuite", test_suite["id"], test_suite["name"])

    # get test case, validated
    def get_test_cases_by_test_suite(self, test_suite_id):
        test_cases = my_testlink.getTestCasesForTestSuite(test_suite_id, True, None)
        for test_case in test_cases:
            print("testCase", test_case["id"], test_case["name"])

    # get test suite, validated
    def get_test_suite(self):
        projects = my_testlink.getProjects()
        top_suites = my_testlink.getFirstLevelTestSuitesForTestProject(projects[0]["id"])
        for suite in top_suites:
            print("test suite", suite["id"], suite["name"])

    # create test suite, validated
    def create_test_suite(self, project_id="1", test_suite_name="test02", test_suite_describe="test01", father_id= ""):
        if father_id == "":
            my_testlink.createTestSuite(project_id, test_suite_name, test_suite_describe)
        else:
            my_testlink.createTestSuite(project_id, test_suite_name, test_suite_describe, parentid=father_id)

    # create test case, Unverified
    def create_test_case(self, father_id, data):
        my_testlink.initStep(data[0][2], data[0][3], automation)
        for i in range(1, len(data)):
            my_testlink.appendStep(data[i][2], data[i][3], automation)
        my_testlink.createTestCase(data[0][0], father_id, "1", "timen.xu", "", preconditions=data[0][1])

    # get test case, Unverified
    def get_test_case(self, test_case_id):
        test_case = my_testlink.getTestCase(testcaseid=test_case_id)
        for i in test_case:
            print ("序列", "执行步骤", "预期结果")
            for m in i.get("steps"):
                print (m.get("step_number"), m.get("actions"), m.get("expected_results"))
        return test_case

    # report test result, validated
    def report_test_result(self, test_plan_id, test_case_id, final_test_result, test_notes, execduration, platform_name, build_name):
        localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        my_testlink.reportTCResult(testcaseid=test_case_id, testplanid=test_plan_id, status=final_test_result,
                                  guess=True, testcaseexternalid=None, platformname=platform_name, buildname=build_name, execduration=execduration,
                                   timestamp=localtime, steps=[], notes=test_notes)
    def get_result(self, test_plan_id, test_case_id):
        response = my_testlink.getLastExecutionResult(testplanid=test_plan_id, testcaseid=test_case_id)
        print("getAllExecutionsResults", response)
        return response

    def get_total_for_test_plan(self, test_plan_id):
        response = my_testlink.getTotalsForTestPlan(testplanid=test_plan_id)
        print("getTotalsForTestPlan", response)
        return response

    def get_builds_for_test_plan(self, test_plan_id):
        response = my_testlink.getBuildsForTestPlan(testplanid=test_plan_id)
        print("getBuildsForTestPlan", response)
        return response

    def get_exec_counter_by_build(self, test_plan_id):
        response = my_testlink.getExecCountersByBuild(testplanid = test_plan_id)
        print("getExecCountersByBuild", response)
        return response

    def get_test_suites_for_test_plan(self, test_plan_id):
        response = my_testlink.getTestSuitesForTestPlan(test_plan_id)
        print("getTestSuitesForTestPlan", response)
        return response

    def get_test_cases_for_test_plan(self, test_plan_id, build_id, platform_id, execstatus):
        if execstatus =='p' or execstatus =='f' or execstatus =='b':
            response = my_testlink.getTestCasesForTestPlan(testplanid=test_plan_id, buildid=build_id, platformid=platform_id, executestatus=execstatus)
            print("getTestCasesForTestPlan A  ", response)
            return response
        elif execstatus =='n':
            response = my_testlink.getTestCasesForTestPlan(testplanid=test_plan_id, buildid=build_id,
                                                           platformid=platform_id)
            print("getTestCasesForTestPlan A  ", response)
            return response

    def get_test_case_assigned_tester(self, test_plan_id, build_id, platform_id, testcase_id):
        response = my_testlink.getTestCaseAssignedTester(testplanid=test_plan_id, buildid=build_id,
                                                       platformid=platform_id, testcaseexternalid=testcase_id)
        print("getTestCasesForTestPlan A  ", response)
        return response




# test_link_process = TestlinkProcess()
# project_id = "13417"
# test_case_id = "13629"
# test_plan_dict = {"20687": "SLAsajksd"}
# platform_dict = {"7": "Automation"}
# build_name = "Srtm.20191008"
# test_link_process.get_test_suites_by_test_plan("20687")
# test_link_process.get_test_cases_by_test_suite("13666")
# test_link_process.get_test_case("21045")


 

 类似资料: