用Python从质量中心读取特定测试步骤

0 投票
2 回答
1954 浏览
提问于 2025-04-17 23:38

我正在通过OTA COM库与质量中心(Quality Center)进行工作。我已经弄明白了如何连接到服务器,但在OTA文档中找不到如何使用它的具体方法。我需要做的是创建一个函数,这个函数接收一个测试名称作为输入,然后从QC中返回这个测试的步骤数量。目前我在这个问题上进展到这个程度。

import win32com
from win32com.client import Dispatch
# import codecs #to store info in additional codacs
import re
import json
import getpass #for password
qcServer = "***"
qcUser = "***"
qcPassword = getpass.getpass('Password: ')
qcDomain = "***"
qcProject = "***"
td = win32com.client.Dispatch("TDApiOle80.TDConnection.1")
#Starting to connect
td.InitConnectionEx(qcServer)
td.Login(qcUser,qcPassword)
td.Connect(qcDomain, qcProject)
if td.Connected == True:
    print "Connected to " + qcProject

else:
    print "Connection failed"
#Path = "Subject\Regression\C.001_Band_tones"
mg=td.TreeManager
npath="Subject\Regression"
tsFolder = td.TestSetTreeManager.NodeByPath(npath)
print tsFolder    

td.Disconnect
td.Logout
print "Disconnected from " + qcProject

如果能提供一些好的Python示例或教程,我会非常感激。目前我找到的这个这个,但它们对我没有帮助。

2 个回答

1

使用OTA API从质量中心获取数据,通常意味着你需要通过路径获取某个元素,创建一个工厂,然后用这个工厂来搜索对象。在你的情况下,你需要用TreeManager来获取测试计划中的一个文件夹,然后需要一个TestFactory来获取测试,最后还需要DesignStepFactory来获取步骤。我不是Python程序员,但我希望你能从中得到一些帮助:

mg=td.TreeManager
npath="Subject\Test"
tsFolder = mg.NodeByPath(npath)
testFactory = tsFolder.TestFactory
testFilter = testFactory.Filter
testFilter["TS_NAME"] = "Some Test"
testList = testFactory.NewList(testFilter.Text)
test = testList.Item(1) # There should be only 1 item
print test.Name
stepFactory = test.DesignStepFactory
stepList = stepFactory.NewList("")
for step in stepList:
    print step.StepName

习惯QC OTA API的文档需要一些时间,但我觉得它非常有用。我几乎所有的知识都是来自API文档中的示例——对于你的问题,有一些示例,比如“查找唯一测试”或“通过名称和路径获取测试对象”。这两个示例都是关于测试对象的。即使这些示例是用VB写的,把它们改成Python也应该不难。

1

我找到了这个问题的解决办法,如果你有更好的方法,欢迎分享出来。

import win32com
from win32com.client import Dispatch
import getpass

def number_of_steps(name):
    qcServer = "***"
    qcUser = "***"
    qcPassword = getpass.getpass('Password: ')
    qcDomain = "***"
    qcProject = "***"
    td = win32com.client.Dispatch("TDApiOle80.TDConnection.1")

    #Starting to connect
    td.InitConnectionEx(qcServer)
    td.Login(qcUser, qcPassword)
    td.Connect(qcDomain, qcProject)
    if td.Connected is True:
        print "Connected to " + qcProject

    else:
        print "Connection failed"

    mg = td.TreeManager  # Tree manager
    folder = mg.NodeByPath("Subject\Regression")
    testList = folder.FindTests(name)  # Make a list of tests matching name (partial match is accepted)
    if testList is not None:
        if len(testList) > 1:
            print "There are multiple tests matching this name, please check input parameter\nTests matching"
            for test in testList:
                print test.name
                td.Disconnect
                td.Logout
                return False
        if len(testList) == 1:
            print "In test %s there is %d steps" % (testList[0].Name, testList[0].DesStepsNum)
    else:
        print "There are no test with this test name in Quality Center"
        td.Disconnect
        td.Logout
        return False
    td.Disconnect
    td.Logout
    print "Disconnected from " + qcProject
    return testList[0].DesStepsNum  # Return number of steps for given test

撰写回答