Robot Framework_Ride(Run標簽)


前言

我一直在想 Robot Framework 不要 RIDE 可不可以。對於編寫測試用例來說,只要掌握 Robot
Framework 的語法規則,隨便在一個你順手的編輯器下編寫也沒問題,甚至效率更高。為什么要填寫
那個該死的“表格”。
直到運行案例的時候我才意識到 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 標簽頁提供了運行與停止的按鈕,使用很簡單。可是你知道到點擊“運行”按鈕的時候,Robot
Framework 是怎么執行“測試套件.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 文件

 

……
from robotide.run.process import Process
……
class Runner(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)
def run(self):
self._process = Process(self._config.command)
self._process.start()
self._timer.Start(500)
def OnTimer(self, event=None):
finished = self._process.is_finished()
self._window.update_output(self._process.get_output(), finished)
if finished:
self._timer.Stop()
def stop(self):
try:
self._process.stop()
except Exception, 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”按鈕,通過默認瀏覽器打開

在 log.html 文件中可以查看用例執行的每一步,適合跟蹤定義問題。

查看 report.html,點擊 Run 標簽而上的“Report”按鈕,通過默認瀏覽器打開

report.html 用於最終結果的展示,適合了解測試用例的執行情況:測試了哪些模塊,用例數、失敗率等。

4、篩選執行用例

這一節來探討一下,幾中方式可以篩選要運行的運用例。
第一種:勾選用例
在要執行的用例前面打勾。

這種方法最簡單和直觀,要運行哪條用例就勾選哪一條。如果全部不勾選,點擊“運行”按鈕會運行所有用例

也可以在“測試套件”上右鍵選擇:

 

Select All Test:選擇當前套件的所有用例。
Select Only Failed Test:選擇當前套件下運行失敗的用例。
Select Only Passed Test:選擇當前套件下運行成功的用例。
對於一個測試套件下有幾十上百個用例來說,這幾選項將非常有用。

第二種:用命令
這就用到 Run 標簽中的 Arguments 功能。

 


在 Arguments 的輸入框內輸入“-t testcase4”。點擊“Start”按鈕,只執行了 test case4 這一條用例。
Arguments 能做的事情可不止於此。想了解更多多命令。可以在 cmd.exe 下執行“pybot.bat --help”

第三種:篩選標記
這種方式就非常有意思的,對於不同的人來說會有一些標記,比如某富二代的標記就是“任性”。對
於用例來說也可以打上標記。比如“重要”、“一般”、“基礎”等。
點擊某個用例,你會看到“Setting>>”的按鈕,點擊按鈕展開:

在最下面將會看到“Tags”的選項,在“<Add New>”的輸入框內輸入“重要”。這條用例就打上了
“重要”的標記。
現在切換到 Run 標簽,我要運行帶“重要”標記的用例了,如何去做了?這就要用到:
Only Run Tests with these Tags:只運行這些標記的測試案例。
Skip Tests with these Tags: 跳過這些標記的測試案例

因為“test case3”被打上了“重要”的標記,所以它被執行了。
對於一個用例來說,我們可以為它添加多個標記。勾選“Skip Tests with these Tags”選項可以跳過某些標記的用例。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM