Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import and use user defined enum classes in robot framework using python

I am currently implementing some test cases in robot framework. Assertion checks that the integer is working. I want to change that to enum values. This is my basic requirement.

This is the robot file that I am using (TestCase.robot)

*** Settings ***
Library           Wrapper.py

*** Test Cases ***
TC_01
    ${rv}    Set Variable    ${0}    #${}
    Should Be Equal As Integers    ${rv}    0

In the line Should Be Equal As Integers ${rv} 0, instead of this integer value 0 assertion , I want to convert that integer to some enum value.

something like this.

Should Be Equal As Integers    ${rv}    Status.OK

where Status is a enum class which is defined in the Wrapper.py. This Wrapper.py is included in the TestCase.robot as a Library in this robot file. I can paste the content of that enum here

class Status(Enum):
    OK = 0
    NOT_OK = 1

so that instead of integers we can make it more readable. When I give like this I am getting error as

'(Status.OK)' cannot be converted to an integer: ValueError: invalid literal for int() with base 10: '(status.ok)'

Can you guys help to sort out this issue ?

like image 369
Vishnu CS Avatar asked Jul 02 '20 11:07

Vishnu CS


People also ask

How do I import a Python class into Robot Framework?

To import the Python script inside Robot, we use the keyword Library in the Robot file under ***settings*** . To call the function, we use <file_name> <dot> <function name> . To add keywords inside the function, we use the keyword decorator. Here, BuildIn().

What is import enum in Python?

Enum is a class in python for creating enumerations, which are a set of symbolic names (members) bound to unique, constant values. The members of an enumeration can be compared by these symbolic anmes, and the enumeration itself can be iterated over. An enum has the following characteristics.

How do I import a variable into Robot Framework?

Starting from Robot Framework 5.0, variable files implemented using Python can also be imported using the module name `similarly as libraries`__. When using this approach, the module needs to be in the `module search path`_. All variables from a variable file are available in the test data file that imports it.

How to use enums in Python?

Enums can be displayed as string or repr. 2. Enums can be checked for their types using type (). 3. “ name ” keyword is used to display the name of the enum member. 4. Enumerations are iterable. They can be iterated using loops

Can I import Python modules inside the Robot Framework?

Since the Robot framework is built on top of Python, importing Python modules inside the Robot framework is a simple process. It allows tremendous flexibility to your code and helps you create your own custom keywords that are better suited for your task than its robot counterparts.

What happened to enum class member changed in Python?

Changed in version 3.11. Enum members are instances of their enum class, and are normally accessed as EnumClass.member. In Python versions 3.5 to 3.10 you could access members from other members – this practice was discouraged, and in 3.11 Enum returns to not allowing it:

Can I use a custom library in Robot Framework?

Having your custom library does not mean that you have to code everything from scratch. It is very easy to import other libraries and packages and integrate them into your code. For example, you can use the RPA Framework both in Robot Framework and in Python directly.


Video Answer


2 Answers

A couple of things - first and foremost, you want to compare the value, which is the Enum.member_name.value property, as already pointed out.
The other things - python Enum is a bit special, it's not instantiated - which would stop you from importing a module having it as class with the same name (to directly reference it) - Robot Framework makes an instance of the class in those imports. So direct access to the value is not possible.

There is a solution, though - make a wrapper (function, in my sample here, but can be a sibling class'es method) that will return you the target value. Sample python:

def return_enum_value(member):
    return Status[member].value


class Status(Enum):
    OK = 0
    NOT_OK = 1

And the RF usage:

${the value}=    Return Enum Value  OK
Should Be Equal As Integers         0    ${the value}
like image 68
Todor Minakov Avatar answered Oct 19 '22 09:10

Todor Minakov


I don't think there's a solution exactly as you want - that is being able to write ${Status.OK}. At least I didn't make it work after some time spent messing around with it. If there's actually a solution, please let me know in the comment section.

Also, if I write just:

from enum import Enum


class StatusEnum(Enum):
    OK = 0
    NOT_OK = 1


print(StatusEnum.OK)

it will print out StatusEnum.OK, not 0 as you perhaps expect. You'd need to write Status.OK.value to get 0. More on that in the docs.

So, the closest I was able to do this is this:

Status.py

from enum import Enum
from robot.api.deco import library, keyword


class StatusEnum(Enum):
    OK = 0
    NOT_OK = 1


@library
class Status:

    @keyword
    def status_ok(self):
        return StatusEnum.OK.value

    @keyword
    def status_not_ok(self):
        return StatusEnum.NOT_OK.value

And in RF:

*** Settings *** 
Library    Status.py

*** Test Cases ***
Check OK And NOT OK
    ${OK}=    Status Ok
    ${NOT_OK}=    Status Not Ok
    Should Be Equal As Integers    ${OK}    0
    Should Be Equal As Integers    ${NOT_OK}    1

But that honestly feels like too much trouble when I can just do:

*** Variables ***
${OK}=    0
${NOT_OK}=    1

*** Test Cases ***
Check OK And NOT OK  
    Should Be Equal As Integers    ${OK}    0
    Should Be Equal As Integers    ${NOT_OK}    1
like image 45
pavelsaman Avatar answered Oct 19 '22 08:10

pavelsaman