Compose keywords with Robot

48 views Asked by At

Let's assume that I have a Python library to manipulate blog posts:

class BlogPost(object):
    def __init__(self):
        ...
    def create_a_blog_post(self):
        ...
    def add_category(self, category):
        ...
    def add_title(self, title):
        ...

I would like to have the following test cases:

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

Should I create separate user keywords for having only category, only title and for both? Or is there anyway to make add any number of "modifiers" to a keyword?

Also, how would one create such keyword, if we have to pass the result of one keyword to another:

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

I purposefully left out the arguments handling from my examples as it's not the point here :)

1

There are 1 answers

0
Bryan Oakley On BEST ANSWER

I would create a keyword called "create a blog post", and then pass either keyword arguments or key/value pairs depending on your preference.

Your test would then look like this:

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

I prefer key/value pairs over keyword arguments since it lets you line up the keys and the values as shown in the "both" example. If you prefer keyword arguments, it might look like this:

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

The implementation simply needs to iterate either over the arguments in pairs in the case of name/value pairs, or over the keyword args.

Also, how would one create such keyword, if we have to pass the result of one keyword to another:

The simplest way is to save the result of the first keyword in a variable, and then pass that variable to the next keyword.

Create another blog post
    ${post}=  Create a blog post
    Add blog post category  ${post}  thisCategory
    Add blog post category  ${post}  thatCategory

Or, a slightly more programatic approach is for Create a blog post to return an object with methods to add categories, and then you can call methods on that object with 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