Robot Framework_Ride(Run标签)

发表于:2018-3-16 10:12

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Silence&QH    来源:博客园

  前言
  我一直在想 Robot Framework 不要 RIDE 可不可以。对于编写测试用例来说,只要掌握 RobotFramework 的语法规则,随便在一个你顺手的编辑器下编写也没问题,甚至效率更高。为什么要填写那个该死的“表格”。
  直到运行案例的时候我才意识到 RIDE 的好处。在 RIDE 中运行测试用例,就是勾选想要运行的用例,然后点击按钮即可。想想我们在做单元测试的时候可不会这么方便,调用 addTest()方法将一个个想要运行的测试方法添加到测试套件中,或者一行行的注释掉不添加到测试套件的测试用例的addTest()方法,这是个极其痛苦的过程
  Run 标签
  下面是 Run 标签的截图:
  第一眼看上去,Run 标签提供了丰富的操作和日志。按照截图我们依次来说明 Run 标签上的按钮和输入框的作用:
  1) Execution Profile:选择运行方式,里面有 pybot、jybot 和 custom script。其中我们默认是用 pybot来运行案例,pybot 的运行 Python 编译器完成。jybot 需要安装 Jython 的支持。custom script 是选择自定义的脚本来运行。
  2) Start 和 Stop:用例的运行和停止。
  3) Report 和 Log:报告和日志,要运行之后才能点击。他们之间的区别:报告更多是结果上的展示,日志更多是过程的记录,在测试用例调试的过程中更多使用日志来查看执行错误。当只想知道测试用例的最终执行情况时用报告。
  4) Autosave:自动保存,如果不勾选,在修改了用例之后如果没有保存的话,运行案例时会提示是否保存。勾选则在运行时自动保存了。
  5) Arguments:pybot 的参数(或者 jybot 等),可以在这里输入 pybot 的命令完成相应的操作。
  6) Only Run Tests with these Tags:只运行这些标记的测试案例。
  7) Skip Tests with these Tags: 跳过这些标记的测试案例。
  下面的两个区域,中间区域记录用例的执行过程,底部的区域输出用例的执行结果。
  运行与停止
  在 Run 标签页提供了运行与停止的按钮,使用很简单。可是你知道到点击“运行”按钮的时候,RobotFramework 是怎么执行“测试套件.txt”文件的么?点击“停止”按钮的时候,Robot Framework 又做了什么操作来终止用例的执行的?带着这样的疑问,我们来简单的读一下 RIDE 的 run 代码。
  首先打开 C:\Python27\Lib\site-packages\robotide\run 目录下的 process.py 文件
import os
import time
import tempfile
import subprocess
class Process(object):
……
def start(self):
self._out_fd, self._out_path = \
tempfile.mkstemp(prefix='rfproc_', suffix='.txt',
text=True)
self._out_file = open(self._out_path)
if not self._command:
self._error = 'The command is missing from this run configuration.'
return
try:
self._process = subprocess.Popen(self._command, stdout=self._out_fd,
stderr=subprocess.STDOUT)
except OSError, err:
self._error = str(err)
……
def stop(self):
try:
self._process.kill()
except AttributeError:
raise AttributeError('Stopping process is possible only with '
'Python 2.6 or newer')
……
def get_output(self, wait_until_finished=False):
"""Returns the output produced by the process.
If ``wait_until_finished`` is True, blocks until the process is
finished and returns all output. Otherwise the currently available
output is returned immediately.
Currently available output depends on buffering and might not include
everything that has been written by the process.
"""
if self._error:
self._close_outputs()
return self._error
if wait_until_finished:
self._process.wait()
output = self._out_file.read()
if self.is_finished():
self._close_outputs()
return output
def _close_outputs(self):
self._out_file.close()
os.close(self._out_fd)
self._remove_tempfile()
  首先看 start()方法,通过 tempfile 模块的 mkstemp()方法找到“txt”文件,也就是“测试套件.txt”文这类件。接着通过 open()方法打开。
  在 get_output()方法中通过 read()方法来读取“txt”文件。最后把读取的文件的赋值给变量 output 并返回。
  在_close_outputs()方法中通过 close()关闭打开的“txt”文件。
  停止测试用例的执行非常单间,由 stop()方法实现,通过调用 kill()将用例的执行进程杀死。
  代码读到这里只是开始,get_output()方法读到的文件返回给谁了呢?或者谁会调用 get_output()方法呢?继续打开 C:\Python27\Lib\site-packages\robotide\run 目录下的 ui.py 文件
……
fromrobotide.run.processimportProcess
……
classRunner(wx.EvtHandler):
def__init__(self,config,notebook):
wx.EvtHandler.__init__(self)
self.Bind(wx.EVT_TIMER,self.OnTimer)
self.name=config.name
self._timer=wx.Timer(self)
self._config=config
self._window=self._get_output_window(notebook)
def_get_output_window(self,notebook):
return_OutputWindow(notebook,self)
defrun(self):
self._process=Process(self._config.command)
self._process.start()
self._timer.Start(500)
defOnTimer(self,event=None):
finished=self._process.is_finished()
self._window.update_output(self._process.get_output(),finished)
iffinished:
self._timer.Stop()
defstop(self):
try:
self._process.stop()
exceptException,err:
wx.MessageBox(str(err),style=wx.ICON_ERROR)
……
  ui.py 文件调用 process.py 文件的方法来运行测试用例。如果读者精通于 Python 语言的话可以顺着这条线继续读下去,看看哪个方法会调用 Runer 类。因为本文档的重点的不是分析 Robot Framework 代码,所以不在再继续。但这里想传达的思路是知其然,一定要知其所以然;用工具而不要受制于工具
  3、报告与日志
  当用例运行结束,Robot Framework 生成三个文件:output.xml、log.html 和 report.html。output.xml 记录的测试结果是 xml 文件,这个文件不够直观。根据特定的需要可以编写脚本读取 xml 文件并生成特定的测试报告。
  相比较而言 log.html 和 report.html 报告要直观得多,因为是 html 格式的嘛。
  查看 log.html 文件,点击 Run 标签而上的“Log”按钮,通过默认浏览器打开

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号