6

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 ?

4
  • The case matters when directly referencing python code; have you tried ${Status.OK}? Also, it would help if your question included a proper minimal reproducible example rather than several small blocks of data. Commented Jul 2, 2020 at 12:53
  • @BryanOakley Thanks for the comment. But I am working on a hardware. That's why I can't post the entire thing. I tried this. But it didn't work,
    – Vishnu CS
    Commented Jul 2, 2020 at 13:03
  • I never asked for the entire thing. I asked for a minimal reproducible example. Commented Jul 2, 2020 at 13:31
  • @BryanOakley: Now I updated the code. Now it is minimum reproducible. You just need to have TestCase.robot and a Wrapper.py and the contents are same as I posted.
    – Vishnu CS
    Commented Jul 2, 2020 at 16:47

3 Answers 3

6

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}
4
  • I wll try this solution
    – Vishnu CS
    Commented Jul 4, 2020 at 14:52
  • 1
    So how did it go? With this approach, the return_enum_value becomes an "interface" to the enum - you can get the value for any target defined in it. Commented Jul 6, 2020 at 11:13
  • We tweaked this in another way. But I tried your solution. But I was getting No keyword with name 'return_enum_value' found.
    – Vishnu CS
    Commented Aug 29, 2020 at 1:46
  • @TodorMinakov, your above solution worked for me. Though my requirement was to have str Enum instead of int Enum. The statement return Status[member] also works in place of return Status[member].value Commented Nov 15, 2021 at 12:04
1

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
1
  • Thanks for the detailed description. I'll try this . The code I am mentioned in the question is reproducible.
    – Vishnu CS
    Commented Jul 3, 2020 at 6:10
0

No idea if this is relevant to someone but I was able to use enums this way:

my_enum.py:

from enum import Enum

class MyEnum(Enum):
    FOO = 1
    BAR = 2

enum_suit.robot:

*** Settings ***
Variables    my_enum.py

*** Test Cases ***
My Test
    Check Enum

*** Keywords ***
Check Enum
    Log To Console    ${MyEnum.FOO.value}  =>  1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.