日积硅步,致以千里!

小沙粒不渺小

醉后不知天在水,满船清梦压星河。

python_testlink

实例:

import testlink
import logging


url = 'http://10.12.29.8/testlink/lib/api/xmlrpc/v1/xmlrpc.php'
key = '38914038f41232f775b21b7a3ae3a9f0'


class TestlinkClient():
    def __init__(self, url=url, key=key):
        self.url = url
        self.key = key
        self.tlc = testlink.TestlinkAPIClient(self.url, self.key)

    def get_information_test_project(self):
        # print("Number of Projects      in TestLink: %s " % tlc.countProjects())
        # print("Number of Platforms  (in TestPlans): %s " % tlc.countPlatforms())
        # print("Number of Builds                   : %s " % tlc.countBuilds())
        # print("Number of TestPlans                : %s " % tlc.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())
        self.tlc.listProjects()
    
    def get_test_plan_by_project(self, testprojectname):
        """
        # 通过项目名称获取项目ID,返回数据为项目ID
        # 通过项目ID获取项目下的所有测试计划,返回的数据格式为包含字典的列表
        # 字典为每项测试计划的基本数据,如测试计划ID、测试计划名称、是否公共、是否活动等
        """
        try:
            #Project = tlc.getTestProjectByName(testprojectname)
            #ProjectID = Project['id']
            project_id = self.tlc.getProjectIDByName(testprojectname)
        except Exception as e:
            print(e)
        
        try:
            project_test_plans = self.tlc.getProjectTestPlans(project_id)
            return project_test_plans
        except Exception as e:
            print(e)
        
    
    def create_test_plan(self, testplanname, testprojectname='智能监控平台', active=1, public=1):
        """
        必选参数
            testprojectname:测试项目名称
            testplanname:测试计划名称
        可选参数
            prefix:描述
            note:note
            active:活动 可选0或1 0为非活动 1为活动 默认为1
            public:公共 可选0或1 0为非公共 1为公共 默认为1
        返回信息为创建状态信息和测试计划ID等信息
        """
        project_test_plans = self.get_test_plan_by_project(projectname)
        exist_plan_list = [x.get("name") for x in project_test_plans]
        if testplanname not in exist_plan_list:
            testPlan = self.tlc.createTestPlan(testprojectname, testplanname, active, public)
            print("testPlan")
        else:
            print("The testplan: {} is exist!".format(testplanname))
    
    def create_test_build_for_plan(self, test_plan_name, testprojectname="智能监控平台"):
        """
        必选参数
            testplanid:测试计划ID
            buildname:版本名称
        可选参数
            buildnotes:版本说明
            active:活动   0为关闭 1为打开 默认为1
            open:打开 0为关闭 1为打开 默认为1
            releasedate:发布日期
            copytestersfrombuild:复制测试用例分配
        返回为状态类信息 包含测试版本id等
        """
        project_test_plans = self.get_test_plan_by_project(testprojectname)
        for plan in project_test_plans:
            if plan.get("name") == test_plan_name:
                test_plan_id = plan.get("id")
                try:
                    build_ft00 = tlc.createBuild(testplanid=test_plan_id, buildname='FT-00', active=1, open=1)
                    build_ft01 = tlc.createBuild(testplanid=test_plan_id, buildname='FT-01', active=1, open=1)
                    break
                except Exception as e:
                    print(e)
    
    def get_build_id_by_plan(self, test_plan_name, build_name, testprojectname="智能监控平台"):
        """    
        # 通过测试计划ID获取计划下的所有版本,返回的数据格式为包含字典的列表
        # 字典为每个版本的基本信息,如测试计划ID、版本ID、版本名称、是否打开、是否活动等
        """
        project_test_plans = self.get_test_plan_by_project(testprojectname)
    
        try:
            for plan in project_test_plans:
                if plan.get("name") == test_plan_name:
                    test_plan_id = plan.get("id")
                    break
        except Exception as e:
            print(e)
    
        try:
            builds = self.tlc.getBuildsForTestPlan(test_plan_id)
            for build in builds:
                if build.get("name") == build_name:
                    build_id = build.get("id")
                    break
            return build_id
        except Exception as e:
            print(e)
    
    
    def add_case_to_plan(self, test_plan_name, test_case_id):
        """
        必选参数
            testprojectid:测试项目id
            testplanid:测试计划id
            testcaseexternalid:测试用例外部id,这个也可以使用testcaseid替代
            version:测试用例版本
        可选参数:
            platformid:platformid
            executionorder:执行顺序
            urgency:重要程度
            overwrite:添加日期
        返回为字典,字典中为添加状态类信息
        """
        project_test_plans = self.get_test_plan_by_project()
        for plan in project_test_plans:
            if plan.get("name") == test_plan_name:
                test_plan_id = plan.get("id")
                print(test_plan_id)
                break
        addCase = self.tlc.addTestCaseToTestPlan(testprojectid='1', testplanid=test_plan_id, testcaseexternalid=test_case_id, version=1)
        print(addCase)
    
    
    def report_test_result(self, test_case_id, test_result, testprojectname="智能监控平台", test_plan_name="AMPOnlineSmokeTest", build_name="FT-02"):
        """
        # 发送测试执行结果
        # 其中需要四个必须参数,其他可选参数可参考源码
        # 根据实际情况进行选择,测试计划ID、执行测试状态 p为通过,f为失败,
        # 第三个参数可以选择测试用例ID或测试用例外部ID
        # 第四个参数可以选择测试用例版本名称或版本ID
        # 返回的数据为包含字典的列表,字典中数据为执行状态等信息
        """
        build_id = self.get_build_id_by_plan(test_plan_name, build_name, testprojectname)
        project_test_plans = self.get_test_plan_by_project(testprojectname)
        try:
            for plan in project_test_plans:
                if plan.get("name") == test_plan_name:
                    test_plan_id = plan.get("id")
                    break
        except Exception as e:
            print(e)
    
        try:
            result = self.tlc.reportTCResult(None, test_plan_id, None, test_result, "", guess=True,
                               testcaseexternalid=test_case_id, platformname="0", buildid=build_id)
            print(result)
        except Exception as e:
            print(e)


if __name__ == "__main__":

    deamo = TestlinkClient()
    #get_test_plan_by_project("智能监控平台")
    #query_test_plan()
    #create_test_plan('AMPOnlineSmokeTest')
    #create_test_build_for_plan('AMPOnlineSmokeTest')
    #get_test_suite()
    #add_case_to_plan('AMPOnlineSmokeTest')
    #get_build_id_by_plan('AMPOnlineSmokeTest', 'FT-00')

    #deamo.report_test_result("AMP-62281", "p")
    deamo.get_test_plan_by_project("智能监控平台")
View Code

 

posted @ 2022-01-21 14:31  小沙粒不渺小  阅读(22)  评论(0编辑  收藏  举报