Robot Framework(十六) 扩展RobotFramework框架——使用监听器接口
4.3使用监听器接口
Robot Framework有一个侦听器接口,可用于接收有关测试执行的通知。监听器是具有某些特殊方法的类或模块,它们可以用Python和Java实现。监听器接口的示例用法包括外部测试监视器,在测试失败时发送邮件消息以及与其他系统通信。
4.3.1使用听众
使用--listener 选项从命令行使用监听器,以便将监听器的名称作为参数提供给它。侦听器名称来自实现侦听器接口的类或模块的名称,类似于从实现它们的类获取测试库名称。指定的侦听器必须位于导入导入时搜索测试库的同一模块搜索路径中。其他选项是提供侦听器文件的绝对路径或相对路径, 与测试库类似。通过多次使用此选项,可以使用多个侦听器。
也可以从命令行为监听器类提供参数。使用冒号作为分隔符在侦听器名称(或路径)之后指定参数。此方法仅提供字符串类型参数,并且参数显然不能包含冒号。但是,听众应该很容易绕过这些限制。
例子:
pybot --listener MyListener tests.html
jybot --listener com.company.package.Listener tests.html
pybot --listener path/to/MyListener.py tests.html
pybot --listener module.Listener --listener AnotherListener tests.html
pybot --listener ListenerWithArgs:arg1:arg2
pybot --listener path/to/MyListener.java:argument tests.html
4.3.2可用的侦听器接口方法
在测试执行开始时,Robot Framework使用给定的参数创建一个侦听器类的实例。在测试执行期间,当测试套件,测试用例和关键字开始和结束时,Robot Framework会调用侦听器的方法。它还在输出文件准备好时调用适当的方法,最后在调用close方法时调用。监听器不需要实现任何官方接口,它只需要具有它实际需要的方法。
监听器接口版本
在Robot Framework 2.1中更改了与测试执行进度相关的方法的签名。进行此更改以便可以在不破坏现有侦听器的情况下将新信息添加到侦听器接口。旧签名将继续有效,但在将来的某个版本中将被弃用,因此所有新的侦听器都应使用下表中描述的签名来实现。有关旧侦听器接口的最新详细说明,请参阅Robot Framework 2.0.4的用户指南。
注意
侦听器必须 定义属性ROBOT_LISTENER_API_VERSION才能被识别为新样式侦听器。ROBOT_LISTENER_API_VERSION属性的值 必须为2,可以是字符串,也可以是整数。以下示例实现为新样式侦听器。
监听器接口方法签名
与测试执行进度相关的所有侦听器方法都具有相同的签名方法(名称,属性),其中属性 是包含事件详细信息的字典。下表列出了侦听器界面中的所有可用方法以及属性字典的内容(如果适用)。字典的键是字符串。所有这些方法都有 camelCase别名。因此,例如,startSuite是同义词start_suite。
| 方法 | 参数 | 属性/解释 |
|---|---|---|
| start_suite |
名称,属性 name, attributes |
属性字典中的键:
|
| end_suite |
名称,属性 name, attributes |
属性字典中的键:
|
| start_test |
名称,属性 name, attributes |
属性字典中的键:
|
| end_test |
名称,属性 name, attributes |
属性字典中的键:
|
| start_keyword |
名称,属性 name, attributes |
属性字典中的键:
|
| end_keyword |
名称,属性 name, attributes |
属性字典中的键:
|
| log_message |
信息 message |
在执行的关键字写入日志消息时调用。message是一个包含以下键的字典:
|
|
信息 message |
信息 message |
在框架本身写入系统日志 消息时调用。message是一个与log_message方法具有相同键的字典。 |
| 输出文件 output_file | 路径 path | 完成写入输出文件时调用。路径是文件的绝对路径。 |
| log_file | 路径 path | 完成写入日志文件时调用。路径是文件的绝对路径。 |
| 报告文件 report_file | 路径 path | 写入报告文件时调用已完成。路径是文件的绝对路径。 |
| debug_file | 路径 path | 写入调试文件时调用完成。路径是文件的绝对路径。 |
| close | 在所有测试套件及其中的测试用例之后调用已经执行。 |
可用的方法及其参数也显示在下面的正式Java接口规范中。所述的内容java.util.Map属性是如在上面的表格。应该记住,监听器不需要实现任何显式接口或具有所有这些方法。
public interface RobotListenerInterface {
public static final int ROBOT_LISTENER_API_VERSION = 2;
void startSuite(String name, java.util.Map attributes);
void endSuite(String name, java.util.Map attributes);
void startTest(String name, java.util.Map attributes);
void endTest(String name, java.util.Map attributes);
void startKeyword(String name, java.util.Map attributes);
void endKeyword(String name, java.util.Map attributes);
void logMessage(java.util.Map message);
void message(java.util.Map message);
void outputFile(String path);
void logFile(String path);
void reportFile(String path);
void debugFile(String path);
void close();
}
4.3.3监听器记录
Robot Framework 2.6引入了新的编程日志API,听众也可以使用它们。但是,存在一些限制,以及下表中解释了不同的侦听器方法如何记录消息。
| 方法 | 说明 |
|---|---|
| start_keyword,end_keyword,log_message | 消息将记录到 execution关键字下的普通日志文件中。 |
| start_suite,end_suite,start_test,end_test | 消息将记录到syslog中。警告也显示在普通日志文件的执行错误部分中。 |
| 信息 | 消息通常记录在syslog中。如果在执行关键字时使用此方法,则会将消息记录到普通日志文件中。 |
| 其他方法 | 消息仅记录到syslog中。 |
| Methods | Explanation |
|---|---|
| start_keyword, end_keyword, log_message | Messages are logged to the normal log file under the executed keyword. |
| start_suite, end_suite, start_test, end_test | Messages are logged to the syslog. Warnings are shown also in the execution errors section of the normal log file. |
| message | Messages are normally logged to the syslog. If this method is used while a keyword is executing, messages are logged to the normal log file. |
| Other methods | Messages are only logged to the syslog. |
注意
为避免递归,侦听器记录的消息不会发送到侦听器方法log_message和message。
警告
在Robot Framework 2.6.2之前,侦听器的日志记录存在严重问题。因此不建议在早期版本中使用此功能。
4.3.4监听器示例
第一个简单示例在Python模块中实现。它主要说明使用监听器接口并不是很复杂。
ROBOT_LISTENER_API_VERSION = 2 def start_test(name, attrs):
print 'Executing test %s' % name def start_keyword(name, attrs):
print 'Executing keyword %s with arguments %s' % (name, attrs['args']) def log_file(path):
print 'Test log available at %s' % path def close():
print 'All tests executed'
第二个仍然使用Python的例子稍微复杂一些。它将获取的所有信息写入临时目录中的文本文件,而不需要太多格式化。文件名可以从命令行给出,但也有默认值。请注意,在实际使用中, 通过命令行选项--debugfile提供的调试文件功能可能比此示例更有用。
import os.path
import tempfile class PythonListener: ROBOT_LISTENER_API_VERSION = 2 def __init__(self, filename='listen.txt'):
outpath = os.path.join(tempfile.gettempdir(), filename)
self.outfile = open(outpath, 'w') def start_suite(self, name, attrs):
self.outfile.write("%s '%s'\n" % (name, attrs['doc'])) def start_test(self, name, attrs):
tags = ' '.join(attrs['tags'])
self.outfile.write("- %s '%s' [ %s ] :: " % (name, attrs['doc'], tags)) def end_test(self, name, attrs):
if attrs['status'] == 'PASS':
self.outfile.write('PASS\n')
else:
self.outfile.write('FAIL: %s\n' % attrs['message']) def end_suite(self, name, attrs):
self.outfile.write('%s\n%s\n' % (attrs['status'], attrs['message'])) def close(self):
self.outfile.close()
第三个示例实现与前一个示例相同的功能,但使用Java而不是Python。
import java.io.*;
import java.util.Map;
import java.util.List; public class JavaListener { public static final int ROBOT_LISTENER_API_VERSION = 2;
public static final String DEFAULT_FILENAME = "listen_java.txt";
private BufferedWriter outfile = null; public JavaListener() throws IOException {
this(DEFAULT_FILENAME);
} public JavaListener(String filename) throws IOException {
String tmpdir = System.getProperty("java.io.tmpdir");
String sep = System.getProperty("file.separator");
String outpath = tmpdir + sep + filename;
outfile = new BufferedWriter(new FileWriter(outpath));
} public void startSuite(String name, Map attrs) throws IOException {
outfile.write(name + " '" + attrs.get("doc") + "'\n");
} public void startTest(String name, Map attrs) throws IOException {
outfile.write("- " + name + " '" + attrs.get("doc") + "' [ ");
List tags = (List)attrs.get("tags");
for (int i=0; i < tags.size(); i++) {
outfile.write(tags.get(i) + " ");
}
outfile.write(" ] :: ");
} public void endTest(String name, Map attrs) throws IOException {
String status = attrs.get("status").toString();
if (status.equals("PASS")) {
outfile.write("PASS\n");
}
else {
outfile.write("FAIL: " + attrs.get("message") + "\n");
}
} public void endSuite(String name, Map attrs) throws IOException {
outfile.write(attrs.get("status") + "\n" + attrs.get("message") + "\n");
} public void close() throws IOException {
outfile.close();
} }
Robot Framework(十六) 扩展RobotFramework框架——使用监听器接口的更多相关文章
- Robot Framework(十五) 扩展RobotFramework框架——远程库接口
4.2远程库接口 远程库接口提供了在运行Robot Framework本身的机器上运行测试库的方法,以及使用除本机支持的Python和Java之外的其他语言实现库的方法.对于测试库,用户远程库看起来与 ...
- Robot Framework(十四) 扩展RobotFramework框架——创建测试库
4.1创建测试库 Robot Framework的实际测试功能由测试库提供.有许多现有的库,其中一些甚至与核心框架捆绑在一起,但仍然经常需要创建新的库.这个任务并不复杂,因为正如本章所示,Robot ...
- Robot Framework(十七) 扩展RobotFramework框架——扩展Robot Framework Jar
4.4扩展Robot Framework Jar 使用标准JDK安装中包含的jar命令,可以非常简单地向Robot Framework jar添加其他测试库或支持代码.Python代码必须放在jar里 ...
- robotframework的学习笔记(十六)----robotframework标准库String
官方文档:http://robotframework.org/robotframework/latest/libraries/String.html Introduction A test libra ...
- 【Python之路】第十六篇--Web框架之Tornado
概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...
- Robot Framework(六)变量
变量 2.5.1简介 变量是Robot Framework的一个不可或缺的特性,它们可以在测试数据的大多数地方使用.最常见的是,它们用于测试用例表和关键字表中关键字的参数,但所有设置都允许在其值中使用 ...
- robot framework用python扩展编写自定义library
我的utils.py文件 #!/usr/bin/env python #-*- coding:utf8 -*- __version__ = '0.1' import sys reload(sys) s ...
- Spring(十六)之MVC框架
MVC 框架教程 Spring web MVC 框架提供了模型-视图-控制的体系结构和可以用来开发灵活.松散耦合的 web 应用程序的组件.MVC 模式导致了应用程序的不同方面(输入逻辑.业 ...
- Python3爬虫(十六) pyspider框架
Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.pyspider介绍1.基本功能 提供WebUI可视化功能,方便编写和调试爬虫 提供爬取进度监控.爬取结果查看 ...
随机推荐
- opencv 加载pb
1.错误1 Tensor's data type is not supported the type of Mul is DF_Float 2. 错误2 type == " ...
- js大数计算之展示
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 脱壳系列—— *加密脱壳(Android使用手册破解)
作者:i春秋作家HAI_ZHU 0×00 前言 好久没有写文了,要好好开动一下了.很多事情要忙.这篇文章本来很早之前就要写的,但是因为很多事情就拖了很久. 前置内容 HAI_使用手册 知识总结 0×0 ...
- sql计算两个时间之间的差,并用时分秒表示
这是自己写的方法,总觉得会有更好的办法实现这个效果呢? SELECT then ))))+'秒' then )))+'秒' then ))+'秒' else CONVERT(nvarchar,DATE ...
- 在nuxt中引入Font Awesome字体图标库
介绍 在element-ui框架中提供了一些图标样式,但是种类比较少,所以在这里提供一套更完善的字体图标库Font Awesome(官方文档),下面就开始介绍如何在一个nuxt项目中使用这套字体库. ...
- 理解JVM之JAVA运行时内存区域
java运行时内存区域划分为方法区,堆区,虚拟机栈区,本地方法栈,程序计数器.其中方法区跟堆区是线程共享的数据区,其他的是线程私有的数据区. 1.程序计数器 程序计数器(PC)是一块较小的内存,他是存 ...
- PowerBulider获取计算机mac地址
PowerBulider获取计算机mac地址 1.下载GETNET.DLL获取网络资源的API 2.PB的全局函数中的引入需要API,常用API列表如下 //得到计算机名字 function bool ...
- 10.Vue请求远端数据库
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- RT-Thread--简介
RT-Thread 概述 RT-Thread,全称是 Real Time-Thread,它是一个嵌入式实时多线程操作系统,基本属性之一是支持多任务,允许多个任务同时运行,但并不是真正的同时运行,而是宏 ...
- idou老师教你学Istio11 : 如何用Istio实现流量熔断
在之前的最佳实践中,已经带大家通过一系列的实践任务领略了Istio的无穷魅力.今天,将向大家介绍如何用Istio实现流量熔断. 熔断机制是创建弹性微服务应用程序的重要模式.熔断可以帮助您自由控制故障影 ...