RobotFramework 入门

在机器重启后遇到RobotFramework的RIDE启动故障,通过将python2.exe改回python.exe,然后重新安装pip、robotframework及robotframework-ride模块,成功解决了问题。接着,了解并熟悉了Robot的参数配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最大的收获是终于实践了站在巨人的肩膀上,可以让自己走的更远。

step 1 : 安装可以很简单。 
python安装2.7版本,robotframework 仅支持2.7版本

wxpython安装2.8版本,ride基于wxpython,仅支持2.8版本 

安装robot framework: 进入Python27\Scripts目录,应该会有一个pip.exe文件(pip若未安装请参考下面段落描述的安装方法),在此目录下shift+右健点击,打开的菜单中选择“在此处打开命令窗口”, 然后依次执行pip install robotframework, pip install robotframework-ride。这两个命令分别执行安装robot framework以及robotframework-ride的操作。
在cmd命令行输入ride如果可以正常打开ride的界面则说明安装成功。
从Pipit可以下载到robot framework的source文件,如何安装参考 http://www.robotframework.net/article/5, 相较之下推荐pip的安装方式。

pip安装方法参考 https://pip.pypa.io/en/stable/installing/,表示再次体会到官方文档有多靠谱。

遇到的问题:由于机器上同时装了两个版本的python, 2.7跟3.4, 按照百度经验给的方法分别对两个版本 python.exe文件做了修改  http://jingyan.baidu.com/article/b87fe19e94ca95521935686e.html 

机器重启之后ride 无法正常启动了,将2.7版本下的python2.exe 修改回python.exe, 重新执行python easy_install.py pip, pip install robotframework, pip install robotframework-ride 才把问题解决, ride正常启动。



setp 2: 熟悉robot的参数。

RobotFramework 除了可以使用ride 运行以外,本身也提供了一些命令行参数进行拓展,我们可以在使用过程中添加这些参数,以达到方便我们测试和调试的目的。
在cmd下输入robot --help就能看到所有支持的参数以及示例。
比如: 
-G --settag tag *        Sets given tag(s) to all executed test cases.
运行时给所有testcases设置给定的标签 , 只在运行时生效, 不会改变测试用例中的标签

 -v --variable name:value *  Set variables in the test data. Only scalar
                          variables with string value are supported and name is
                          given without `${}`. See --escape for how to use
                          special characters and --variablefile for a more
                          powerful variable setting mechanism.
                          Examples:
                          --variable str:Hello       =>  ${str} = `Hello`
                          -v hi:Hi_World -E space:_  =>  ${hi} = `Hi World`
                          -v x: -v y:42              =>  ${x} = ``, ${y} = `42`
例子:robot --outputdir "D:\Python27\works\robot" --variable scal:4 --argumentfile "D:\Python27\works\robot\argfile.txt"
脚本中定义了一个变量${scal}=2, 调上面的语句运行脚本后,从输出的log看到${scal}=4 

-b --debugfile file      Debug file written during execution. Not created
                          unless this option is specified.
输出完整的debug信息。 可以自己试试效果:> 


setp 3: 简化执行流程
创建一个目录保存测试用例文件, 一般为txt文件, 目录为D:\Python27\works\testcases , 文件为test1.txt

新建一个名为argfile的txt文件,文件中指示待运行的测试用例的路径。
-C
off
-W
107
D:\Python27\works\testcases

新建一个批处理文件run.bat, outputdir 指定output.xml,report.html, log.html生成的三个文件的保存路径。 argumentfile 指示argfile.txt文件的所在路径。
echo off
robot --outputdir "D:\Python27\works\robot" --argumentfile "D:\Python27\works\ride\argfile.txt"

执行run.bat就会在outputdir 指定的路径下生成三个运行结果的文件。 


step 4  : 定制输出结果--处理xml文件的榜样。
RF执行完毕后,默认生成 xml 格式的输出文件、html格式的report和log文件。xml 格式的输出是RF的详细执行信息,report.html和log.html基于该文件生成。
目标:输出的output.xml文件中包括了所有运行用例的结果等信息, 想要过滤掉出所有成功的用例, 只打印出失败的用例。
参考 :robot framework result module  https://robot-framework.readthedocs.org/en/3.0/autodoc/robot.result.html

#!/usr/bin/env python
#encoding "utf-8"

"""Usage: check_test_times.py seconds inpath [outpath]

Reads test execution result from an output XML file and checks that no test
took longer than given amount of seconds to execute.

Optional `outpath` specifies where to write processed results. If not given,
results are written over the original file.
"""

import sys, re
import os, time

from robot.api import ExecutionResult, ResultVisitor, SuiteVisitor

class GetFailedCases(ResultVisitor):

    def __init__(self):
        pass
        #self.max_milliseconds = max_seconds * 1000

    def visit_test(self, test):
        if test.status == 'FAIL' :# and test.elapsedtime > self.max_milliseconds:
            print(test.name)
            print(test.status)# = 'FAIL'
            print(test.parent.doc)# = 'Test execution took too long.'
            print('|'.join(test._setter__tags._tags))    

def check_tests(inpath):
    result = ExecutionResult(inpath)
    result.visit(GetFailedCases())
    #result.save(outpath)

if __name__ == '__main__':
    outfile = ''

    rootdir = os.getcwd()  
    #print 'rootdir = ' + rootdir

    for (dirpath, dirnames, filenames) in os.walk(rootdir):  
        #print('dirpath = ' + dirpath)  
        for filename in filenames:  
            if re.search('.xml', filename):
                #outfile = os.path.join(dirpath,filename) + 
                try:
                    check_tests(os.path.join(dirpath,filename))
                except TypeError:
                    pass #print (__doc__ )
                #print os.path.join(dirpath,filename)


debug可以查看到visit_test()的test参数是一个树形结构,test的属性中包括用例名,执行时间,执行状态等等关键信息。
直接提取出来即可。



如此,可以把用例相关的信息都获取到。如果需要进一步的操作比如获取指定的信息并保存到另外的文件中,再在代码中定义相关的处理函数即可。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值