RobotFramework测试框架(13)--扩展RF

本文详细介绍了在RobotFramework中如何自定义测试库,包括静态库、动态库和混合库的区别,Python模块与类的关联,关键字方法的命名与参数处理,以及使用装饰器定义和定制关键字的功能。
摘要由CSDN通过智能技术生成

扩展RF

我们可以自己写Library

从测试库类型上来说分为Static静态、Dynamic动态和Hybrid混合这3种

在Python里来说,每一个测试库的目录名或文件名都是它的module,通常在module里会有一个class类,一般我们都要求class的名字和module的名字是相同的。比如ExampleLibrary.py的文件名是ExampleLibrary

带有“_”开头的方法都算做私有方法,在RobotFramework里会自动过滤掉“_”开头的方法

测试库有一些基础配置项,放到class里

ROBOT_LIBRARY_SCOPE='GLOBAL'

scope可以理解为范围或者作用域,可设置的值有TEST CASE,TESTSUITE,GLOBAL。如果设置为TEST CASE,那么每个Case都会创建一个新的该测试库的实例。

对于测试库里的关键字方法的命名是用来给测试案例使用的,除了前面说到的“_”开头的关键字方法不会看到,其他的关键字都可以直接被使用,并且在使用时忽略大小写、空格或下划线的。比如count,你可以在案例里写Count、count,甚至于“c o u n t”都可以调用到底层的count方法

测试库关键字的参数

 在class里的关键字方法的参数,第一个参数总是self,之后才是函数真正需要的参数

把必填参数放在前面,有默认值的参数放在后面,List参数放在最后。

参数的种类也可以按照之前讲的来理解,不带“*”的就是Scala型的参数,带“*”的就是List型参数

**参数的,Dictionary型的参数,这种参数传值的时候是要传key=value这样的值的。

def arg_demo(self,arg1,agg2=2,*args):
def freearg_demo(self,**freearg)

Static Library

静态库中RF的关键字被定义为python的方法。

Static Library With a Class

将Python类导入为Library,则类中的方法可以是关键字。

class DemoLibrary:
    def __init__(self, *args, **kwargs):
        print(f"Sample Library initialized with args: {args} and kwargs: {kwargs}")
 
    def my_keyword(self, *args, **kwargs):
        print(f"Keyword got args: {args} and kwargs: {kwargs}")
        return "Hello World"
*** Settings ***
Library    DemoLibrary.py

*** Test Cases ***
Use a Keyword with multiple arguments
    My Keyword    Argument 1    Argument 2    Named Argument=One Value

Static Library withouth a Class

将关键字定义在python方法中

将py文件导入为Library,则文件中的方法可以是关键字

import base64

def encode_as_base64(string):
    """
    Encode string as base64.
    """
    return base64.b64encode(string.encode())

def decode_from_base64(string):
    """
    Decode string from base64.
    """
    return base64.b64decode(string).decode()

 

*** Settings ***
Library    LibraryWithoutClass.py

*** Test Cases ***
Use Custom Keywords
    ${base64}    Encode As Base64    This is a Test String
    Log    ${base64}
    ${decoded}    Decode From Base64    ${base64}
    Log    ${decoded}

Decorators 

可以使用装饰@@keyword和@not_keyword将方法装饰为关键字。

from robot.api.deco import keyword, not_keyword


@keyword('Login via user panel')
def login(username, password):
      # ...

@not_keyword
def this_is_not_keyword():
    pass
from robot.api.deco import keyword


@keyword(tags=['tag1', 'tag2'])
def login(username, password):
    # ...

@keyword('Custom name', ['tags', 'here'])
def another_example():
    # ...

@keyword(types={'count': int, 'case_insensitive': bool})
def example_keyword(count, case_insensitive=True):
    if case_insensitive:
        # ...

@keyword(types=[int, bool])
def example_keyword(count, case_insensitive=True):
    if case_insensitive:
        # ...
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值