【问题标题】:HP QC ALM REST API - Defect creation issueHP QC ALM REST API - 缺陷创建问题
【发布时间】:2017-11-10 09:59:02
【问题描述】:

我正在尝试创建一个新缺陷。我必须在我的 C# ASP.Net 项目中使用 REST API 来完成它。以下是使用的网址:

qcbin/rest/domains/TESTDOMAIN/projects/TESTPROJECT/defects

以下是我的请求正文:

<?xml version="1.0" encoding="UTF-8"?>
<Entity Type="defect">
  <Fields>  
    <Field Name="priority">
      <Value>3-High</Value>
    </Field>
    <Field Name="description">
      <Value>test description</Value>
    </Field>
    <Field Name="name">
      <Value>test with rest API</Value>
    </Field>    
    <Field Name="creation-time">
      <Value>2011-08-16 11:34:08</Value>
    </Field>
  </Fields>
</Entity>

只有在 XML 中不使用 creation-time 时,我才能成功创建缺陷。我可以知道如何使用包含连字符的列吗?

我已经在 C# 上编写了代码。下面是我的 C# 代码

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HPALMUrl + "/rest/domains/" + HPALMDomain + "/projects/" + HPALMProject + "/defects/");
request.Method = "POST";
request.Accept = "application/xml";
request.ContentType = "application/xml";
authRequest.KeepAlive = true;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
request.CookieContainer = createSessionRequest.CookieContainer; //Authenticated cookie
using (var requestStream = request.GetRequestStream())
{
    requestStream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();

string responseText = string.Empty;
if (response.StatusCode == HttpStatusCode.OK)
    responseText = "Update completed";
else
    responseText = "Error in update";

我在更新测试表列时也面临这个连字符列问题。所以,请让我知道如何使用连字符包含列

提前致谢!

【问题讨论】:

  • 这应该不是问题,您面临的问题可能是,您正在尝试为插入新记录时 ALM 自动生成的字段设置一些值。具体说明测试计划中的哪些字段导致问题。
  • @Barney,我尝试了与 To-Mail 相同的方法,它不是自动生成的。
  • @Barney,这是缺陷而不是测试计划。
  • I am facing this hyphen column problem when updating Test table column 好的也贴一下错误信息
  • 抱歉,Test 表也出现了这种情况。但目前,缺陷创建是重中之重。谢谢!

标签: c# asp.net rest alm hp-quality-center


【解决方案1】:

如何接近,

  1. 发出 get 请求以提取它接受的所有字段名称和数据格式。

    例如:https://almserver/qcbin/rest/domains/WWT/projects/content/defects/1

  2. 确定必填字段。

  3. 根据您的有效负载将请求标头设置为 XML/JSON

在 Python 中创建缺陷

ALM_USER_NAME = "username"
ALM_PASSWORD = "password"
ALM_DOMAIN = "domain"

ALM_URL = "https://almserver/qcbin/"
AUTH_END_POINT = ALM_URL + "authentication-point/authenticate"
QC_SESSION_END_POINT = ALM_URL + "rest/site-session"
QC_LOGOUT_END_POINT = ALM_URL + "authentication-point/logout"
ALM_MIDPOINT = ALM_URL + "rest/domains/" + ALM_DOMAIN + "/projects/"

def generate_xml_data(inputdata):
    '''
        Function    :   generateXMLData
        Description :   Generate an xml string
        Parameters  :   Dictionary of variable
    '''
    root = Element('Entity')
    root.set('Type', inputdata['Type'])
    inputdata.pop('Type')
    childs = SubElement(root, 'Fields')

    for key, value in inputdata.iteritems():
        child1 = SubElement(childs, 'Field')
        child1.set('Name', key)
        child2 = SubElement(child1, 'Value')
        child2.text = value
    return tostring(root)

def createdefect():
    '''
        alm defect
    '''
    alm_session = requests.Session()
    # Login
    try:
        res = alm_session.post(AUTH_END_POINT, auth=HTTPBasicAuth(
            ALM_USER_NAME, ALM_PASSWORD))
        if res.status_code == 200:
            print "ALM: Logged in"
        alm_session.post(QC_SESSION_END_POINT)
        alm_session.headers.update({'Accept': 'application/json',
                                    'Content-Type': 'application/xml'})
        # Get all the projects
        res = alm_session.get(ALM_MIDPOINT)

    # Post a New Defect
    defect = dict()
    defect['Type'] = 'defect'
    defect['name'] = 'StackOverflow'
    defect['user-10'] = 'Content'  # User defined field
    defect['severity'] = '1-Low'
    defect['priority'] = '1-Low'
    defect['detected-by'] = 'userid'
    defect['creation-time'] = '2017-11-13'

    # Call the generic method to build the xml data
    defect_payload = generate_xml_data(defect)

    response = alm_session.post(ALM_MIDPOINT + "content/defects", data=defect_payload)
    print response.url

except (KeyboardInterrupt, SystemExit, Exception) as err:
    print "keyboard interrupt"
    print err.message
finally:
    if alm_session:
        res = alm_session.post(QC_LOGOUT_END_POINT)
        if res.status_code == 200:
            print "ALM: Logged out"

if __name__ == "__main__":
    try:
        createdefect()
    except (KeyboardInterrupt, SystemExit):
        print "done with errors"
    

【讨论】:

  • 现在可以使用了。感谢您的代码! +50 为您提供帮助 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
相关资源
最近更新 更多