def handle_test_begin(self, testcase): '''处理一个测试用例执行的开始 :param testcase: 测试用例 :type testcase: TestCase ''' self._xmldoc.appendChild( self._xmldoc.createProcessingInstruction( "xml-stylesheet", 'type="text/xsl" href="TestResult.xsl"')) owner = getattr(testcase, 'owner', None) priority = getattr(testcase, 'priority', None) timeout = getattr(testcase, 'timeout', None) self._testnode = self._xmldoc.createElement('TEST') self._testnode.setAttribute( "name", _to_utf8(saxutils.escape(testcase.test_name))) self._testnode.setAttribute("owner", _to_utf8(saxutils.escape(str(owner)))) self._testnode.setAttribute("priority", str(priority)) self._testnode.setAttribute("timeout", str(timeout)) self._testnode.setAttribute( 'begintime', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.begin_time))) self._xmldoc.appendChild(self._testnode) self.begin_step('测试用例初始步骤')
def handle_log_record(self, level, msg, record, attachments): '''处理一个日志记录 :param level: 日志级别,参考EnumLogLevel :type level: string :param msg: 日志消息 :type msg: string :param record: 日志记录 :type record: dict :param attachments: 附件 :type attachments: dict ''' self._write("%s: %s\n" % (levelname[level], msg)) if level == EnumLogLevel.ASSERT: if record.has_key("actual"): actual = record["actual"] self._write(" 实际值:%s%s\n" % (actual.__class__, actual)) if record.has_key("expect"): expect = record["expect"] self._write(" 期望值:%s%s\n" % (expect.__class__, expect)) if record.has_key("code_location"): self._write( _to_utf8(' File "%s", line %s, in %s\n' % record["code_location"])) if record.has_key("traceback"): self._write(_to_utf8_by_lines("%s\n" % record["traceback"])) for name in attachments: file_path = attachments[name] if os.path.exists(_to_unicode(file_path)): file_path = os.path.realpath(file_path) self._write(" %s:%s\n" % (name, _to_utf8(file_path)))
def _to_utf8_by_lines(s): '''将任意字符串转换为UTF-8编码 ''' lines = [] for line in s.split('\n'): lines.append(_to_utf8(line)) return '\n'.join(lines)
def assert_equal(self, message, actual, expect=True): '''检查实际值和期望值是否相等,不能则测试用例失败 :param message: 检查信息 :param actual: 实际值 :param expect: 期望值(默认:True) :return: True or False ''' if isinstance(actual, basestring): actual = _to_utf8(actual) if isinstance(expect, basestring): expect = _to_utf8(expect) if expect != actual: self.__record_assert_failed(message, actual, expect) return False else: return True
def handle_step_begin(self, msg): '''处理一个测试步骤的开始 :param msg: 测试步骤名称 :type msg: string ''' if not isinstance(msg, (str, unicode)): raise ValueError("msg必须是str或unicode类型") self._stepnode = self._xmldoc.createElement("STEP") self._stepnode.setAttribute('title', _to_utf8(msg)) self._stepnode.setAttribute( 'time', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))) self._testnode.appendChild(self._stepnode)
def handle_log_record(self, level, msg, record, attachments): '''处理一个日志记录 :param level: 日志级别,参考EnumLogLevel :type level: string :param msg: 日志消息 :type msg: string :param record: 日志记录 :type record: dict :param attachments: 附件 :type attachments: dict ''' if not isinstance(msg, basestring): msg = str(msg) #由于目前的报告系统仅支持部分级别的标签,所以这里先做转换 if level >= EnumLogLevel.ERROR: tagname = levelname[EnumLogLevel.ERROR] elif level == EnumLogLevel.Environment or level == EnumLogLevel.RESOURCE: tagname = levelname[EnumLogLevel.INFO] else: tagname = levelname[level] infonode = self._xmldoc.createElement(tagname) textnode = self._xmldoc.createTextNode(_to_utf8(msg)) infonode.appendChild(textnode) self._stepnode.appendChild(infonode) if level == EnumLogLevel.ASSERT: if record.has_key("actual"): node = self._xmldoc.createElement("ACTUAL") try: actual = record["actual"] if isinstance(actual, basestring): dom.parseString("<a>%s</a>" % actual) acttxt = "%s%s" % (actual.__class__, actual) except xmlexpat.ExpatError: acttxt = "%s%s" % (actual.__class__, repr(actual)) except UnicodeEncodeError: acttxt = "%s%s" % (actual.__class__, repr(actual)) node.appendChild(self._xmldoc.createTextNode(acttxt)) infonode.appendChild(node) if record.has_key("expect"): node = self._xmldoc.createElement("EXPECT") try: expect = record["expect"] if isinstance(expect, basestring): dom.parseString("<a>%s</a>" % expect) exptxt = "%s%s" % (expect.__class__, expect) except xmlexpat.ExpatError: exptxt = "%s%s" % (expect.__class__, repr(expect)) except UnicodeEncodeError: exptxt = "%s%s" % (expect.__class__, repr(expect)) node.appendChild(self._xmldoc.createTextNode(exptxt)) infonode.appendChild(node) if record.has_key("traceback"): excnode = self._xmldoc.createElement('EXCEPT') excnode.appendChild( self._xmldoc.createTextNode(_to_utf8(record["traceback"]))) infonode.appendChild(excnode) for name in attachments: file_path = attachments[name] attnode = self._xmldoc.createElement('ATTACHMENT') attnode.setAttribute('filepath', _to_utf8(file_path)) attnode.appendChild(self._xmldoc.createTextNode(_to_utf8(name))) infonode.appendChild(attnode)
def handle_log_record(self, level, msg, record, attachments): '''处理一个日志记录 :param level: 日志级别,参考EnumLogLevel :type level: string :param msg: 日志消息 :type msg: string :param record: 日志记录 :type record: dict :param attachments: 附件 :type attachments: dict ''' #2011/06/13 pear 增加对crashfiles属性的处理 #2012/02/24 pear 添加对异常类型的处理 #2012/05/16 pear 优化在测试报告显示的异常信息,取完整的最后一行 #2012/05/17 pear callstack如果找不到raise,则默认最后一行 #2012/10/22 pear 获取堆栈信息;去除异常原因信息字符串中左右两边的空格和换行符 #2012/10/23 pear 修改显示的异常信息 #2012/10/23 pear 若脚本不通过,则提示"检查点不通过" #2013/06/21 pear 对用户传入的expect的值进行有效性检查,看能否被xml解释 #2013/08/27 pear 对实际值进行有效性检查,看能否被xml解释 #2013/08/28 pear qtfa(rambutan)需要期望值存在非正常字符时也不抛异常 #2015/01/26 olive 优化用例超时时的报告展示 #2015/01/26 olive 报告展示错误类型增加优先级 #2016/01/26 olive 支持非str类型的消息 if not isinstance(msg, basestring): msg = str(msg) if level == EnumLogLevel.ASSERT and record.has_key("code_location"): msg += ' [File "%s", line %s, in %s]' % record["code_location"] #由于目前的报告系统仅支持部分级别的标签,所以这里先做转换 if level >= EnumLogLevel.ERROR: tagname = levelname[EnumLogLevel.ERROR] elif level == EnumLogLevel.Environment or level == EnumLogLevel.RESOURCE: tagname = levelname[EnumLogLevel.INFO] else: tagname = levelname[level] infonode = self._xmldoc.createElement(tagname) textnode = self._xmldoc.createTextNode(_to_utf8(msg)) infonode.appendChild(textnode) self._stepnode.appendChild(infonode) if level == EnumLogLevel.ASSERT: if record.has_key("actual"): node = self._xmldoc.createElement("ACTUAL") #2013/08/27 pear 这里对self.record.actual进行格式化,加入a标签(其实,可使用能被xml解释接受的任一合法标签), # 是为了使dom.parseString函数在解释self.record.expect时,不会抛异常。 #2014/05/14 pear 若actual是unicode类型,需要catch此异常,并显示出来 try: if isinstance(record["actual"], basestring): dom.parseString("<a>%s</a>" % record["actual"]) acttxt = _to_utf8(record["actual"]) except xmlexpat.ExpatError: acttxt = _to_utf8(repr(record["actual"])) except UnicodeEncodeError: acttxt = _to_utf8(repr(record["actual"])) node.appendChild(self._xmldoc.createTextNode(acttxt)) infonode.appendChild(node) if record.has_key("expect"): node = self._xmldoc.createElement("EXPECT") try: if isinstance(record["expect"], basestring): #2013/06/27 pear 这里对self.record.expect进行格式化,加入a标签(其实,可使用能被xml解释接受的任一合法标签), # 是为了使dom.parseString函数在解释self.record.expect时,不会抛异常。 dom.parseString("<a>%s</a>" % record["expect"]) exptxt = _to_utf8(str(record["expect"])) except xmlexpat.ExpatError: exptxt = _to_utf8(repr(record["expect"])) except UnicodeEncodeError: exptxt = _to_utf8(repr(record["expect"])) node.appendChild(self._xmldoc.createTextNode(exptxt)) infonode.appendChild(node) if record.has_key("traceback"): excnode = self._xmldoc.createElement('EXCEPT') excnode.appendChild( self._xmldoc.createTextNode(_to_utf8(record["traceback"]))) infonode.appendChild(excnode) for name in attachments: file_path = attachments[name] attnode = self._xmldoc.createElement('ATTACHMENT') attnode.setAttribute('filepath', _to_utf8(file_path)) attnode.appendChild(self._xmldoc.createTextNode(_to_utf8(name))) infonode.appendChild(attnode)