用Rob编写关键字

2024-09-27 09:23:42 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个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"

我应该为只有类别、只有标题和两者创建单独的用户关键字吗?或者,是否有必要在关键字中添加任意数量的“修饰符”

此外,如果我们必须将一个关键字的结果传递给另一个关键字,如何创建这样的关键字:

*** 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"

我故意省略了示例中的参数处理,因为这不是重点:)


Tags: selfaddtitledefcreatewith文章blog
1条回答
网友
1楼 · 发布于 2024-09-27 09:23:42

我会创建一个名为“创建博客帖子”的关键字,然后根据您的偏好传递关键字参数或键/值对

您的测试将如下所示:

*** 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

与关键字参数相比,我更喜欢键/值对,因为它可以让您排列键和值,如“两者”示例中所示。如果您更喜欢关键字参数,它可能如下所示:

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

对于名称/值对,实现只需要在成对的参数上迭代,或者在关键字args上迭代

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返回一个带有添加类别的方法的对象,然后您可以使用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

相关问题 更多 >

    热门问题