用Python或Java自动化HP质量中心

7 投票
6 回答
33319 浏览
提问于 2025-04-15 21:32

我们有一个项目在使用HP质量中心,而我们经常遇到的问题就是有人不更新缺陷的评论。

所以我在想,能不能做一个小脚本或者工具,定期提醒用户更新评论,甚至强制他们去更新。

我发现了开放测试架构API,不知道有没有好的Python或Java的例子可以参考一下。

谢谢,Hari

6 个回答

6

给其他可能查看这个讨论的人提供一些信息。

首先,你需要安装pywin32,可以从这里下载:http://sourceforge.net/projects/pywin32/files/pywin32/Build216/

首先,你需要导入pywin32。

'''@author: www.qcintegration.com  @mailto:contact@qcintegration.com'''
import pywintypes
import win32com.client as w32c
from win32com.client import gencache, DispatchWithEvents, constants

接下来,我在这里添加了登录服务器的操作。

def connect_server(qc, server):
        '''Connect to QC server
        input = str(http adress)
        output = bool(connected) TRUE/FALSE  '''
        try:
            qc.InitConnectionEx(server); 
        except:
            text = "Unable connect to Quality Center database: '%s'"%(server); 
        return qc.Connected;

def connect_login(qc, username, password):
    '''Login to QC server
    input = str(UserName), str(Password)
    output = bool(Logged) TRUE/FALSE  '''
    try:
        qc.Login(username, password);
    except pywintypes.com_error, err:
        text = unicode(err[2][2]);
    return qc.LoggedIn;

def connect_project(qc, domainname, projectname):
    '''Connect to Project in QC server
    input = str(DomainName), str(ProjectName)
    output = bool(ProjectConnected) TRUE/FALSE  '''

    try:
        qc.Connect(domainname, projectname)
    except pywintypes.com_error, err:
        text = "Repository of project '%s' in domain '%s' doesn't exist or is not accessible. Please contact your Site Administrator"%(projectname, domainname); 
    return qc.ProjectConnected;

然后是一个方法,用来包含OTAapi的dll文件。

def qc_instance():
        '''Create QualityServer instance under variable qc
        input = None
        output = bool(True/False)'''
        qc= None;
        try:
            qc = w32c.Dispatch("TDApiole80.TDConnection");
            text = "DLL QualityCenter file correctly Dispatched"
            return True, qc;
        except:
            return False, qc;

接下来是连接到QCserver的主要方法。

def qcConnect(server, username, password, domainname, projectname):
    print("Getting QC running files");

    status, qc = qc_instance();
    if status:
        print("Connecting to QC server");
        if connect_server(qc, server):
            ##connected to server
            print("Checking username and password");
            if connect_login(qc, username, password):
                print("Connecting to QC domain and project");
                if connect_project(qc, domainname, projectname):
                    text = "Connected"
                    connected = True;
                    return connected, text;
                else:
                    text = "Not connected to Project in QC server.\nPlease, correct DomainName and/or ProjectName";
                    connected = False;
                    return connected, text;
            else:
                text = "Not logged to QC server.\nPlease, correct UserName and/or Password";
                connected = False;
                return connected, text;
        else:
            text = "Not connected to QC server.\nPlease, correct server http address"; 
            connected = False;
            return connected, text;
    else:
        connected = False;
        text = "Unable to find QualityCenter installation files.\nPlease connect first to QualityCenter by web page to install needed files" 
        return connected, text;

最后,我会展示如何在一个地方执行所有这些方法,并给出使用的例子。

if __name__ == "__main__":
    server= r"http://qualitycenterServer:8080/qcbin"
    username= "alex_qc"
    password= ""
    domainname= "DEFAULT"
    projectname= "QualityCenter_Demo" 

    connection_status, text  = qcConnect(server, username, password, domainname, projectname);
    print "connection_status:", connection_status

如果还有其他问题,可以发邮件到:contact@qcintegration.com,或者直接访问网站:http://www.qcintegration.com

13

使用Python(win32com)连接HP质量中心的OTA示例

HP质量中心提供了一个基于com的API,叫做OTA。

关于这个API的文档可以从QC服务器上下载(文件名是OTA_API_Reference.chm),不过奇怪的是,网上很难找到这个文档。

文档里使用的是VBScript(这是QC官方支持的内部语言),你需要把它心里转化成Python。这通常很简单,但有几个地方需要注意。

你需要在你的电脑上安装质量中心的本地代码,如果你能通过网页界面访问QC,那么这个代码应该已经在你的Windows电脑上了。

你还需要知道服务器的URL、你的用户名和密码,以及你正在工作的QC项目的域名。

from win32com.client import Dispatch

conn = get_QCConnection()

for bug in get_bugs(qcConn):
    print bug.Title

put_QCConnection(conn)

#below code needs to be in seperate module or at least above the fold but here 
# for clarity

def get_QCConnection():
    '''Get the hardcoded connection to the server and domain.
    Can be made a "real" engine if you try hard.
    Use makepy utility to determine if the version number has changed (TDApiOle80)
    but this works to current version'''

    QCConnection = Dispatch("TDApiOle80.TDConnection")
    url = "http://qc.example.com/qcbin"
    QCConnection.InitConnectionEx(url)
    QCConnection.login("USER", "PASS")
    QCConnection.Connect("google_projects", "Google_Chrome")    
    return QCConnection

def put_QCConnection(qcConn):
    #If one person logged in to QC changes *anything* on a bug,
    # they hold a global lock on writing to that bug till 
    # thier session times out, so really really remember to logout
    # its painful to wait for your own session to time out

    qcConn.Logout()

def get_bugs(qcConn):
    '''just following boiler plate from vbscript
    PS the SetFilter is not in QTA API, it uses Filter.  
    But due to the workarounds in 
    the very brilliant pythoncom code it supplies a virtual wrapper class
    called SetFilter - this is one of those gotchas '''

    BugFactory = qcConn.BugFactory
    BugFilter = BugFactory.Filter

    BugFilter.SetFilter(u"Status", "New") 
    #NB - a lot of fields in QC are malleable - and vary from site to site. 
    #COntact your admins for a real list of fields you can adjust
    buglist = BugFilter.NewList()
    return buglist       

这为后续的工作打下了不错的基础,不过我创建了一个用于缺陷的虚拟类,并运行类似这样的代码:

dfcts = [defect(b) for b in buglist]

然后我可以把工作代码放进缺陷类里,这样可以让事情更整洁。你要做的一件事是把对QC内部缺陷的访问限制在Python包装类内部。

5

我不太确定有没有好的Java示例,因为OTA不能直接被Java使用,它需要一个Java和COM之间的桥接工具,比如JIntegra

至于Python,你可以使用Python的COM接口。然后任何OTA的示例都可以用,你在QC的OTA文档中可以找到很多。

不过我觉得真正的问题是,为什么你想用Python或Java来做这件事。为什么不直接在QC里用它的工作流功能来写你需要的东西呢?这样你可以用VBScript编写逻辑,并在用户操作时在QC界面中调用它。例如,你可以绑定到缺陷/错误的后置事件,检查是否有评论,如果没有,就直接给用户提示信息。

撰写回答