Pybot 在使用 Pycharm 执行 .robot 时给出错误 "No keyword found" 和 "Importing test library failed"

Pybot gives errors "No keyword found" and "Importing test library failed" when executing .robot using Pycharm

我遇到了类似的问题: Robot Framework:: Imported library 'class' contains no keywords

在这里,用户声称已经通过在.robot 文件的设置部分添加FileName.ClassName 解决了这个问题。我通过添加 "Library test.MyLib" 进行了相同的尝试,但给出了错误 "Importing test library 'test.MyLib' failed: Module 'test' does not contain 'MyLib'"。但是,当我删除并恢复为 "Library test" 时,我收到警告 "[WARN ] Imported library 'test' contains no keywords" 和错误"No keyword with name 'hello' found."我的代码如下:

test.py

class MyLib(object):
__all__ = ['hello']

def __init__(self):
    name = "Wrath"

def hello(self, *args):
    name = self.args[0]
    print "Hello "+name

test.robot

*** Settings ***
Documentation  Suite description
Library  test

*** Test Cases ***
Test title
    [Tags]  DEBUG
    hello  Sloth

*** Keywords ***

如果有人就上述问题向我提供建议,那将非常有帮助。已经尝试过官方 Robot Framework 文档和网络上的几个示例,但似乎我做对了。但我有一种感觉,我可能错过了一些次要但至关重要的东西。确切地说,可能是关键字部分?

根据您描述的症状,听起来机器人正在加载另一个 "test.py" 文件,而不是您认为 它正在加载的关键字文件。

一种确定方法是生成一个 syslog,它会告诉您实际导入的是哪个文件。

您可以尝试的另一件事是将您的库重命名为 "test.py" 以外的名称,然后相应地修改您的导入语句。如果它与另一个名称一起使用,则证明您的环境中有多个 "test.py"。

好吧,我的错。看了Robot Framework的官方文档,有点懵。经过多次反复试验和错误后,我发现我做错了什么。我终于 运行 明白了,以下是我的观察:

  1. 首先也是最重要的事情,我分离了库并为我正在执行的每个程序形成了一个干净的目录结构。
  2. 其次,确保 .robot 文件中的关键字(我正在处理静态 API)和参数之间恰好有 2 个空格(即 ' ')。
  3. 将库文件 "MyLibrary.py" 中的 class 重命名为 "MyLibrary" 以外的任何名称,比如 "MyClass"。这有助于避免像我这样的初学者感到困惑。然后,将 class 作为 "Library MyLibrary.MyClass" 包含在 .robot 文件的 "Settings" header 中。

如果你确定了以上几点,你的测试套件应该可以顺利执行,前提是你的程序没有任何语法或逻辑错误并且框架环境是正确的。