美文网首页
RobotFramework 自定义关键字

RobotFramework 自定义关键字

作者: 我是石巍 | 来源:发表于2018-10-14 22:44 被阅读0次

RobotFramework 基础关键字不够用

基础关键字库不够用怎么办?想办法自己写一套自定义关键字库也不错。
创建一个 MyKeywords.py 文件:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class MyKeywords(object):    # 这里的类名一定要跟文件名一模一样,否则就会找不到这里面的关键字了
    def this_is_test(self):
        print('It works !!!')

导入自定义关键字

创建一个 TestMyKeywords.robot 文件:

*** Settings ***
Documentation    I wanna test my keywords
Library    MyKeywords.py

*** Test Cases ***
Let's Try
    This Is Test    # 没错,就是 MyKeywords.py 里的 def 名

执行成功

$ robot TestMyKeywords.robot
==============================================================================
TestMyKeywords :: I wanna test my keywords
==============================================================================
Let's Try                                                             | PASS |
------------------------------------------------------------------------------
TestMyKeywords :: I wanna test my keywords                            | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  /private/tmp/robot_try/output.xml
Log:     /private/tmp/robot_try/log.html
Report:  /private/tmp/robot_try/report.html

但实际使用的场景要比这个复杂

  • 当 TestMyKeywords.robot 与 MyKeywords.py 不在同一个目录下的时候,会报错:
$ ls *
TestMyKeywords.robot

MyKwds:
MyKeywords.py

$ robot TestMyKeywords.robot
[ ERROR ] Error in file '/private/tmp/robot_try/TestMyKeywords.robot': Test library 'MyKeywords.py' does not exist.

     提示 Library 不存在,所以需要对 robot 文件中的 Library 进行修改:

Library    MyKwds/MyKeywords.py

    这样就能正常执行成功了。

  • 当 MyKeywords.py 中 import 自己写的一个 Python 文件中的函数,且它们还不在同一个目录下的时候,比如这样:
$ ls *
TestMyKeywords.robot

MyKwds:
MyKeywords.py

MyPkg:
MyPkg.py    __init__.py

$ cat MyKwds/MyKeywords.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from MyPkg import who_am_i  # 这里想要 import 自己写的函数

class MyKeywords(object):   
    def this_is_test(self):
        who_am_i()
        print('It works !!!')

$ cat MyPkg/MyPkg.py
#!/usr/local/env python
# -*- coding: utf-8 -*-

def who_am_i():    # 这就是 MyKeywords.py 要 import 的函数
    print('I am Will.')

    是一个跟前一次不一样的 ERROR:

$ robot TestMyKeywords.robot
[ ERROR ] Error in file '/private/tmp/robot_try/TestMyKeywords.robot': Importing test library '/private/tmp/robot_try/MyKwds/MyKeywords.py' failed: ModuleNotFoundError: No module named 'MyPkg'
Traceback (most recent call last):
  File "/private/tmp/robot_try/MyKwds/MyKeywords.py", line 4, in <module>
    from MyPkg import who_am_i
PYTHONPATH:
  /usr/local/bin
  /usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python37.zip
  /usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7
  /usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload
  /usr/local/lib/python3.7/site-packages
==============================================================================
TestMyKeywords :: I wanna test my keywords
==============================================================================
Let's Try                                                             | FAIL |
No keyword with name 'This Is Test' found.
------------------------------------------------------------------------------
TestMyKeywords :: I wanna test my keywords                            | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output:  /private/tmp/robot_try/output.xml
Log:     /private/tmp/robot_try/log.html
Report:  /private/tmp/robot_try/report.html

    这次提示在 import 时候出错,还列出了 PYTHONPATH,因此要把我们的目录加入到 Python 的 site-packages 下:

$ site_pkgs='/usr/local/lib/python3.7/site-packages'    # 不同的系统 site-package 未必相同,以报错时的提示为准 
$ echo '/tmp/robot_try/MyPkg' >> ${site_pkgs}/mylibrary.pth  # 把绝对路径写进一个自定义的 pth 文件中就可以了

    再一次,成功执行了。


robot_report.png

相关文章

网友评论

      本文标题:RobotFramework 自定义关键字

      本文链接:https://www.haomeiwen.com/subject/elbtzftx.html