1

我正在尝试编写一个循环,该循环将返回一个包含测试集和测试用例的表。但是,我无法获得测试用例的名称,但会返回“对象 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'

请需要帮助。谢谢

4

2 回答 2

2

碰巧我错过了一个'for loop in my code: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')

干杯。

于 2014-05-08T15:13:43.203 回答
0

这不是 Python 特有的,但必须显式获取 Name 才能访问它。尝试使用tc._refObjectName. 无需获取_refObjectName,与Name相同。

于 2014-05-02T21:40:59.127 回答