Robot Framework 进阶 (1)

2023年 1月 4日 34.7k 0

pybot 命令

  • 执行所有测试用例
1
pybot .
  • 执行某个测试套件
1
pybot testsuite.txt
  • 执行某个测试套件中的测试用例
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

列表的访问方法有两种:

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

    在测试套件中:

    1
    
    &{user}  name=admin    email=[email protected]
    

    在测试用例中:

    1
    
    &{user}    Create Dictionary    name=admin    email=[email protected]
    

    也可以使用标量符声明字典变量 ${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
    

    相关文章

    KubeSphere 部署向量数据库 Milvus 实战指南
    探索 Kubernetes 持久化存储之 Longhorn 初窥门径
    征服 Docker 镜像访问限制!KubeSphere v3.4.1 成功部署全攻略
    那些年在 Terraform 上吃到的糖和踩过的坑
    无需 Kubernetes 测试 Kubernetes 网络实现
    Kubernetes v1.31 中的移除和主要变更

    发布评论