如何通过Rally API获取测试用例名称

1 投票
2 回答
616 浏览
提问于 2025-04-18 05:10

我正在尝试写一个循环,想要返回一个包含测试集和测试用例的表格。不过,我无法获取测试用例的名称,而是得到了一个“对象ID(OID)”。下面是我的代码:

_M_writer(u'</p>\r\n<table  class="objects" style="width: 60%">\r\n\t\t<tr>\r\n\t\t\t<th>TestSets</td>\r\n\t\t\t<th>TestCases</th>\r\n\t\t</tr>\r\n')

        for ts in testSets:

                __M_writer(u'\t\t\t\t')
                tc = ts.TestCases


                if tc:

                    __M_writer(u'<tr>')
                    __M_writer(u'<td>')
                    __M_writer(unicode(ts.FormattedID))
                    __M_writer(u'\t\t\t\t</td>')

                    __M_writer(u'<td>')

                    __M_writer(filters.html_escape(unicode(tc.FormattedID)))
                    __M_writer(u'</td>')

                __M_writer(u'</tr>\r\n')
                pass

        __M_writer(u'</table>\n')

使用 tc.FormattedID 时出现了属性错误。请看下面的内容:

 print >> fh, template.render(**workContext)

  File "C:\Python27\lib\site-packages\mako\template.py", line 397, in render
    return runtime._render(self, self.callable_, args, data)

  File "C:\Python27\lib\site-packages\mako\runtime.py", line 764, in _render
    **_kwargs_for_callable(callable_, data))

  File "C:\Python27\lib\site-packages\mako\runtime.py", line 796, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)

  File "C:\Python27\lib\site-packages\mako\runtime.py", line 822, in _exec_template
    callable_(context, *args, **kwargs)

  File "C:\Users\xxx\Documents\xx\tools\xxx\tmp\audit.base.html.py", line 130,               in render_body

    context['self'].content(**pageargs)
  File "C:\Users\xxx\Documents\TOOLS_WS\tools\xxxx\tmp\Validation Test Plan.html.py",               line 171, in render_content
    __M_writer(filters.html_escape(unicode(tc.FormattedID)))
AttributeError: 'list' object has no attribute 'FormattedID'

希望能得到帮助。谢谢!

2 个回答

0

这不是Python特有的问题,但要获取Name,必须明确地去取。你可以试试用 tc._refObjectName。其实没有必要单独去获取 _refObjectName,它和Name是一样的。

2

恰好我在代码里缺少了一个'for循环:ts.TestCases`返回的是一系列测试案例,而不是单个测试案例。

修正后的代码如下:

__M_writer(u'</p>\r\n<table  class="objects" style="width: 60%">\r\n\t\t<tr>\r\n\t\t\t<th>TestSets</td>\r\n\t\t\t<th>TestCases</th>\r\n\t\t</tr>\r\n')

    for ts in testSets:

            __M_writer(u'\t\t\t\t')
            testCaseObjectList = ts.TestCases
            for tc in testCaseObjectList:
                if tc:
                     __M_writer(u'<tr>')
                     __M_writer(u'<td>')
                     __M_writer(unicode(ts.FormattedID))
                     __M_writer(u'\t\t\t\t</td>')
                     __M_writer(u'<td>')
                     __M_writer(filters.html_escape(unicode(tc.FormattedID)))
                     __M_writer(u'</td>')
                     __M_writer(u'</tr>\r\n')
                pass

     __M_writer(u'</table>\n')

谢谢。

撰写回答