使用 Robot 撰写关键字
Compose keywords with Robot
假设我有一个 Python 库来操作博客文章:
class BlogPost(object):
def __init__(self):
...
def create_a_blog_post(self):
...
def add_category(self, category):
...
def add_title(self, title):
...
我想要以下测试用例:
*** Test Cases ***
Create post with category
Create a blog post with category "myCategory"
Create post with title
Create a blog post with title "myTitle"
Create post with both
Create a blog post with category "myCategory" and title "myTitle"
我是否应该为只有类别、只有标题或两者都创建单独的用户关键字?或者有没有办法在关键字中添加任意数量的 "modifiers"?
此外,如果我们必须将一个关键字的结果传递给另一个,如何创建这样的关键字:
*** Keywords ***
Create a blog post with category
Create a blog post
Add category <-- How do I pass the blog post I just created to "Add category"
我故意从我的示例中省略了参数处理,因为这不是这里的重点:)
我会创建一个名为 "create a blog post" 的关键字,然后根据您的喜好传递关键字参数或 key/value 对。
您的测试将如下所示:
*** Test Cases ***
Create post with category
Create a blog post category myCategory
Create post with title
Create a blog post title myTitle
Create post with both
Create a blog post
... category myCategory
... title myTitle
我更喜欢 key/value 对而不是关键字参数,因为它可以让您排列键和值,如 "both" 示例中所示。如果您更喜欢关键字参数,它可能看起来像这样:
Create post with category
Create a blog post category=myCategory
Create post with title
Create a blog post title=myTitle
Create post with both
Create a blog post
... category=myCategory
... title=myTitle
实现只需要在 name/value 对的情况下成对地遍历参数,或者遍历关键字参数。
Also, how would one create such keyword, if we have to pass the result of one keyword to another:
最简单的方法是将第一个关键字的结果保存在一个变量中,然后将该变量传递给下一个关键字。
Create another blog post
${post}= Create a blog post
Add blog post category ${post} thisCategory
Add blog post category ${post} thatCategory
或者,一种稍微更程序化的方法是 Create a blog post
到 return 具有添加类别方法的对象,然后您可以使用 Call Method 调用该对象的方法:
Create another blog post
${post}= Create a blog post
Call method ${post} add_category thisCategory
Call method ${post} add_category thatCategory
Add blog post category ${post} thisCategory
Add blog post category ${post} thatCategory
假设我有一个 Python 库来操作博客文章:
class BlogPost(object):
def __init__(self):
...
def create_a_blog_post(self):
...
def add_category(self, category):
...
def add_title(self, title):
...
我想要以下测试用例:
*** Test Cases ***
Create post with category
Create a blog post with category "myCategory"
Create post with title
Create a blog post with title "myTitle"
Create post with both
Create a blog post with category "myCategory" and title "myTitle"
我是否应该为只有类别、只有标题或两者都创建单独的用户关键字?或者有没有办法在关键字中添加任意数量的 "modifiers"?
此外,如果我们必须将一个关键字的结果传递给另一个,如何创建这样的关键字:
*** Keywords ***
Create a blog post with category
Create a blog post
Add category <-- How do I pass the blog post I just created to "Add category"
我故意从我的示例中省略了参数处理,因为这不是这里的重点:)
我会创建一个名为 "create a blog post" 的关键字,然后根据您的喜好传递关键字参数或 key/value 对。
您的测试将如下所示:
*** Test Cases ***
Create post with category
Create a blog post category myCategory
Create post with title
Create a blog post title myTitle
Create post with both
Create a blog post
... category myCategory
... title myTitle
我更喜欢 key/value 对而不是关键字参数,因为它可以让您排列键和值,如 "both" 示例中所示。如果您更喜欢关键字参数,它可能看起来像这样:
Create post with category
Create a blog post category=myCategory
Create post with title
Create a blog post title=myTitle
Create post with both
Create a blog post
... category=myCategory
... title=myTitle
实现只需要在 name/value 对的情况下成对地遍历参数,或者遍历关键字参数。
Also, how would one create such keyword, if we have to pass the result of one keyword to another:
最简单的方法是将第一个关键字的结果保存在一个变量中,然后将该变量传递给下一个关键字。
Create another blog post
${post}= Create a blog post
Add blog post category ${post} thisCategory
Add blog post category ${post} thatCategory
或者,一种稍微更程序化的方法是 Create a blog post
到 return 具有添加类别方法的对象,然后您可以使用 Call Method 调用该对象的方法:
Create another blog post
${post}= Create a blog post
Call method ${post} add_category thisCategory
Call method ${post} add_category thatCategory
Add blog post category ${post} thisCategory
Add blog post category ${post} thatCategory