Catsunami
Catsunami

Reputation: 579

How to expect a failure in robot framework in Python?

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?

Upvotes: 4

Views: 9363

Answers (2)

Dan Constantinescu
Dan Constantinescu

Reputation: 1541

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

Upvotes: 1

Catsunami
Catsunami

Reputation: 579

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.

Upvotes: 2

Related Questions