Robotframework 入门教程(七)

== Custom Library==

终于到了重点了,自定义Library。自定义Library可以用实Python模块和python或java的类来实现。简单写一个python模块实现的
新建mylib.py内含一个md5签名算法函数:
import hashlib

def gen_sign(*args):
    m = hashlib.md5()
    m.update(''.join(args))
    return m.hexdigest()
修改demo.robot如下:
*** Settings ***
Documentation     Example test cases using the keyword-driven testing approach.
Resource          pub.txt
Library           Collections
Library           mylib.py

*** Test Cases ***
First Case
    [Documentation]    variable type
    Log to console    ${var1}
    Log to console    ${list1}[0]
    Log to console    ${list1[0]}
    Log to console    ${dict1}['key1']
    Log to console    ${dict1['key1']}
    Log to console    ${dict1.key1}

Second Case
    [Documentation]    Default values with user-keywords
    Calculate and Check Equals
    Calculate and Check Equals    1+5
    Calculate and Check Equals    7-2    ${5}
    Calculate and Check Equals    expression=6+3    expected=${9}

Third Case
    [Documentation]    Varargs with user-keywords
    Varargs keyword    a    f    ${var1}
    Varargs keyword    @{list1}    ${var1}
    Kwargs keyword    key1=value1    key2=789
    Kwargs keyword    &{dict1}
    Kwargs keyword    &{dict1}    mykey=tututu    &{dict1}

Fourth Case
    [Documentation]    data-driven case
    [Template]    Calculate "${expression}" and Check Equals "${expected}"
    2+4    ${6}
    9-2    ${7}
    6*4    ${24}

Fifth Case
    [Documentation]    builtIn library
    [Template]    Calculate "${expression}" and Check Equals "${expected}"
    2+4    ${6}
    9-2    ${7}
    6*4    ${24}

Sixth Case
    [Documentation]    custome keyword
    ${sign}    gen_sign    @{list1}
    Log to console    ${sign}
传入计算1234的md5并返回打印到console,结果如下:


多个关键字有隐式联系的时候要用到基于类的关键字。新建yourLib.py:
class yourLib(object):
    def __init__(self, member=None):
        self.members = [member] if member else []

    def add_member(self, name):
        self.members.append(name)
        return self.members
修改demo.robot如下:
*** Settings ***
Documentation     Example test cases using the keyword-driven testing approach.
Resource          pub.txt
Library           Collections
Library           mylib.py
Library           yourLib.py
       ......
       ......

Sixth Case
    [Documentation]    custome keyword
    ${sign}    gen_sign    @{list1}
    Log to console    ${sign}
    ${members}    add member    ${var1}
    Log to console    ${members}
    ${members}    add member    LLisisi
    Log to console    ${members}
执行结果如下:


基于类的自定义Library,引入的时候创建了一个该类的实例,调用关键字可以改变实例的属性和状态。




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值