惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
I
InfoQ
博客园_首页
G
Google Developers Blog
爱范儿
爱范儿
Last Week in AI
Last Week in AI
量子位
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
月光博客
月光博客
The GitHub Blog
The GitHub Blog
V
Visual Studio Blog
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19)
Robot Framework 进阶 (1)
微信公众号 · 2018-08-15 · via 陈少文的网站

Please enable Javascript to view the contents

pybot 命令

  • 执行所有测试用例
  • 执行某个测试套件
  • 执行某个测试套件中的测试用例
1
pybot --test case_name testsuit.txt
  • 将测试结果输出到固定路径
1
pybot --ouputdir your_ouput_dir testsuit.txt
  • 执行包含某个 tag 的测试用例
1
pybot --include tag_name testsuit.txt

关于日志

默认情况下,Robot Framework 中低于 INFO 级别的日志消息不会写日志。这个阈值可以通过命令行选项 --loglevel 修改。

在 Robot Framework 中有五种日志级别:

  • FAIL

当关键字失败时使用,只能由Robot Framework自己使用

  • WARN

用来展示警告, 警告消息同样会出现在 控制台以及日志文件的测试执行错误区,,不过它们不会影响到测试用例的状态

  • INFO

默认的消息级别,默认情况下日志文件中不会显示低于此级别的消息

  • DEBUG

用于调试目的.,当需要记录测试库内部执行过程时很有用。当关键字失败时,代码失败的地方会自动使用该级别打印 traceback 信息

  • TRACE

更详细的调试级别,使用该级别时,关键字的参数和返回值会自动写入日志

在测试用例中,可以调用内置关键字 log 输出相应的日志:

例如:

1
2
log    "this is my log"    warn
log    ${num}    warn

输出:

1
2
[ WARN ] "this is my log"
[ WARN ] 100

第一个参数是日志内容,第二个参数是日志级别,默认是 info。

声明变量

  • 变量

在测试套件中:

1
2
${num}    100
${string}   abcdef

在测试用例中:

1
2
${num}    Set Variable    100
${string}    Set Variable    abcdef
  • 列表

在测试套件中:

1
2
@{num_list}       1    2    3
@{string_list}    a    b    c

在测试用例中:

1
2
@{num_list}    Create  List    1        2         3
@{string_list}    Create  List    a       b          c

列表的访问方法有两种:

  1. @{变量名}[index]
  2. ${变量名[index]}
  • 字典

在测试套件中:

1
&{user}  name=admin    email=admin@admin.com

在测试用例中:

1
&{user}    Create Dictionary    name=admin    email=admin@admin.com

也可以使用标量符声明字典变量 ${user}

访问方法:

1
2
log    ${user.name}
log    ${user['name']}

条件表达式

语法结构:

1
2
3
4
5
Run Keyword If  判断条件  其他关键字  参数

...     ELSE IF   判断条件  其他关键字  参数

...     ELSE  判断条件  其他关键字  参数

例如:

1
2
3
4
5
testCase
    ${n}    Set Variable    3
    Run Keyword If    ${n}==100    log    num is 100
    ...    ELSE IF    ${n}>100    log    num is more than 100
    ...    ELSE    log    num is less than 100

需要注意的是,关键字不区别大小写,但是这里的 ELSE ,ELSE IF 严格区分大小。

循环表达式

语法结构:

1
2
3
4
5
6
7
:For 变量  IN  序列(or 列表)

     关键字 参数值

:For 变量  IN RANGE 循环限量

     关键字  参数值

例如:

1
2
3
4
testCase
    ${t}    create list    123    qwe    dfg
    : FOR    ${x}    IN    @{t}
    \    log    ${x}    warn

参数与返回值

在 Robot Framework 中给关键字传递参数,或者从关键字中获取返回值是很常见的操作。

在关键字定义中,通过 [Arguments] 定义参数,也可以设置默认值 ${version}=0 。具有默认值的参数,使用时可以不传,否则必须传递。传递参数时,可以使用默认位置传递,也可以使用 key/value 的形式。

在关键字定义中,通过 [Return] 返回值。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
*** Test Cases ***
testCase
    ${tem} =    获取完整版本    18
    log    ${tem}    warn

*** Keywords ***
获取完整版本
    [Arguments]    ${version}=0
    ${full_version}    set variable    Test-${version}
    [Return]    ${full_version}

Variables 方式扩展 Python 变量

在 Robot Framework 中,有时需要使用 Python 处理一些逻辑,获取运算值。这时,可以利用 Variable 来扩展 Python 变量。

test.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# -*- coding: utf-8 -*-
import random

class Test(object):
    def __init__(self):
        pass

    def random(self):
        return random.randint(1, 100)

myrand = Test()
*** Settings ***
Variables  test.py

*** Test Cases ***
testCase
    log    ${myrand.random()}    warn

Library 方式扩展 Python 函数作为关键字

在 Robot Framework 中,有时需要使用 Python 封装一些操作,比如发送邮件等。这时,可以利用 Library 的方式,引入 Python 函数作为 Robot Framework 中的关键字。

mylib.py

1
2
3
# -*- coding: utf-8 -*-
def myprint(name):
    return "Your name is :" + name
1
2
3
4
5
6
*** Settings ***
Library           ../lib/mylib.py
*** Test Cases ***
testCase
    ${name}=    my_print    haha
    log    ${name}    warn

微信公众号