Robot Framework(15)- 扩展关键字

如果你还想从头学起Robot Framework,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1770899.html

 

前言

  • 什么是扩展关键字?就是你自己写的 Python 文件,里面包含了函数或者类
  • 然后 RF 导入这个 Python 模块,就可以调用函数或者类方法,它们就是扩展关键字

 

Python 模块作为测试库

模块文件名作为测试库的名字

比如:Python 模块名叫 MyLibrary,文件名是 MyLibrary.py,那么测试库的名字就叫做 MyLibrary

 

Python 模块和 Robot 文件同目录下的栗子

这是目录结构哈

python 模块的代码

def returnlist():
    return [i for i in range(10)]


def return_dict():
    return {"a": "hahhahahaahah"}


# 以下划线开头的函数不能作为RF关键字
def _returnlist2():
    return [1, 2]

robot 代码

进入test目录下,运行以下命令

 robot -P . test.robot 

执行结果

知识点

  • _前缀的方法不会作为关键字,在Python里面, _ 开头的方法是私有方法,RF 不会识别到它
  • Python 方法作为关键字也是大小写不敏感
  • RF 中会把关键字的 _ 和单个空格忽略掉,所以 returndict、return dict、return_dict 都是调用同一个关键字

 

Python 类作为测试库的栗子

项目目录

所有 Python 测试代码都在 tlib2.py 里面哦

最终运行是在【15_扩展关键字】目录下运行的,命令如下

robot -P . testrf

 

栗子一:类初始化不需要传参

python 代码

class SubLibrary:
    def __init__(self):
        pass

    def returnint(self):
        return 2020

    def _returnint2(self):
        return 4

robot 代码

测试结果

知识点

  • 在类里面, _ 前缀的方法不会当做关键字
  • 同样,类中声明的方法当做关键字的话,大小写不敏感

 

栗子二:类初始化需要传参

python 代码

from robot.api import logger
class SubLibrary2: def __init__(self, host, port, table='test'): self.host = host self.port = port self.table = table def printaddr2(self): logger.console('host:%s,port:%s,table:%s' % (self.host, self.port, self.table))

robot 代码

测试结果

知识点

如果类的 __init__ 初始化方法需要传参,则在导入库后面跟对应的参数列表

拓展 Python 知识点:先有类对象,还是先执行类初始化方法?

 __new__ 方法产生对象

 __init__ 对象的初始化方法

先 new 一个对象,再 init 一个对象

 

栗子三:类名和模块名相同

python 代码

from robot.api import logger

class tlib2:
    def __init__(self, host, port):
        self.host = host
        self.port = port

    def printaddr(self):
        logger.console('host:%s,port:%s' % (self.host, self.port))

robot 代码

测试结果

知识点

如果类名和模块名相同,可以不用导入类名

 

栗子四:使用路径法导入 Python 模块

Python 代码用的还是栗子三的

robot 代码

测试结果

知识点

如果用路径法,需要注意导入 Python 模块需要有文件后缀哦,且用 / 来表示目录下

重点:使用路径法,只能导入和模块名相同的类名!

 

Python 扩展库的搜索规则

统一的规则

  • 先根据 robot 文件自身当前目录下查找库文件
  • 如果没有找到则再根据 --pythonpath 和 -P 提供的搜索路径进行搜索
  • 最后找 Python 安装的路径

 

Python 库引入了其他模块

背景

当 robot 文件导入的 Python 测试库引入了其他模块时,应该怎么写导入路径?

正确写法

确保导入的模块路径和RF导入的模块起始路径统一

看栗子

 testother.robot  导入 test.py 模块, test.py  模块引入了 login.py 模块的方法

目录结构

login.py 代码

from robot.api import logger


def login_test():
    logger.console('test login')

test.py 代码

from pylib.login import login_test
# from login import login_test 报错

def test():
    login_test()

robot 的代码

在 othertest 目录下运行下面命令

robot -P . testother.robot

测试结果

结论

  • 可以看到 robot 文件引入的路径是 pylib 开头, test 模块引入 login 模块的路径也是 pylib 开头
  • 如果路径是 login 开头导入,那么运行robot文件将会报错(如下图,包含了解析错误)

 

Python 库中的 class 存在继承

背景

当 robot 文件导入 Python 测试库的类继承了另一个类,应该怎么写导入路径?

正确写法

  • 确保导入的模块路径和RF导入的模块起始路径统一
  • 使用的时候 RF 文件只需导入子类即可

看栗子

 test.robot 引入了 other.py  模块下的 Child 类,而 Child 类继承了 Base.py 模块下的 Father 类

目录结构

base.py 的代码

from robot.libraries.BuiltIn import logger


class Father:
    def __init__(self):
        logger.console('init Father')

    def money(self):
        return '$10000'

other.py 的代码

from robot.api import logger
from pylib.Base import Father


class Child(Father):
    def __init__(self):
        Father.__init__(self)
        logger.console('init Child')

    def use_money(self):
        return self.money()

    def make_money(self):
        return '$9999'

robot 的代码

在 testClass 目录下运行下面命令

robot -P . test.robot

测试结果

 

posted @ 2020-05-30 11:51  小菠萝测试笔记  阅读(1946)  评论(0编辑  收藏  举报