4

We use robot framework at work to do some automated testing, and I need to add a few tests. This format is already in the repo, I cannot change anything drastically.

I am using a keywords file that looks like this:

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

from robot.api.deco import keyword

class testkeywords(object):
   """
   """

   def __init__(self):
      pass

   @keyword('I compare ${first_number} with ${second_number}')
   def compare(self, first_number, second_number):
      if (int(first_number) != int(second_number)):
         raise ValueError('Numbers are not the same.')

The .robot file has two tests, one that passes and one that fails:

*** Settings ***
Library         testkeywords.py

*** Variables ***
${num_a}       5
${num_b}       6

*** Test Cases ***
Compare same numbers
   I compare ${num_a} with ${num_a}

Compare different numbers
   I compare ${num_a} with ${num_b}

The Compare different numbers test fails as expected, but it's still a FAIL. How can I set it to expect a failure and hence pass the keyword?

3
  • Downvoter please explain. I provided a minimum example and the question was pretty clear. Commented Jun 1, 2018 at 17:58
  • In my view the question is well written and the examples provided are clear. What made me think, and perhaps the downvoter too, was that the question was shortly followed up by your own answer. This would indicate to me that the amount of time spent researching wasn't that high. Given that the solution was an out-of-the-box keyword suggests that to me too. Learn from the experience and perhaps read the help on level of investigation prior to asking a question is expected. Commented Jun 2, 2018 at 17:16
  • @A.Kootstra, while I certainly saw the keyword in my search, I did not notice the double spaced required to get the keyword to be recognized. The format of the .robot files that I was looking at did not match anything I saw online and I thought it might require different keywords. I actually spent a few hours playing around with it until I noticed the double space. Maybe the documentation for the framework should be a bit better. For what it's worth, most python questions on SO can be solved by looking at documentation. At least I bothered posting an answer... Commented Jun 4, 2018 at 3:09

2 Answers 2

2

I figured it out. You can just modify the test to expect an error as follows:

Compare different numbers
   Run Keyword And Expect Error  *  I compare ${num_a} with ${num_b}

Alternatively to look for a specific error (rather than wildcard):

Compare different numbers
       Run Keyword And Expect Error  ValueError: Numbers are not the same.  I compare ${num_a} with ${num_b}

Note the double space between the command, error and keyword.

Sign up to request clarification or add additional context in comments.

Comments

1

You can also expect a sub-string from the complete error by using this format:

Compare different numbers
   Run Keyword And Expect Error  *are not the same*  I compare ${num_a} with ${num_b}

Documentation: Run Keyword And Expect Error

1 Comment

Please add further details to expand on your answer, such as working code or documentation citations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.