简体   繁体   English

如何使用 python 在机器人框架中导入和使用用户定义的枚举类

[英]How to import and use user defined enum classes in robot framework with python

I am currently implementing some test cases in robot framework.我目前正在机器人框架中实现一些测试用例。 Assertion checks that the integer is working.断言检查 integer 是否正常工作。 I want to change that to enum values.我想将其更改为枚举值。 This is my basic requirement.这是我的基本要求。

This is the robot file that I am using ( TestCase.robot )这是我正在使用的机器人文件( 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.Should Be Equal As Integers ${rv} 0行中,而不是这个 integer 值0断言,我想将 integer 转换为某个枚举值。

something like this.像这样的东西。

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

where Status is a enum class which is defined in the Wrapper.py .其中Status是在Wrapper.py中定义的枚举 class 。 This Wrapper.py is included in the TestCase.robot as a Library in this robot file.Wrapper.py包含在TestCase.robot作为此机器人文件中的Library 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?大家能帮忙解决一下这个问题吗?

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.有几件事 - 首先,您要比较值,即Enum.member_name.value属性,正如已经指出的那样。
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.其他的东西 - python 枚举有点特殊,它没有被实例化 - 这会阻止你导入一个具有相同名称的模块 class (直接引用它) - Robot Framework 在这些导入中创建了 ZA2F2ED4F8EBC2CBB4C21A21 的实例。 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:样品 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}

I don't think there's a solution exactly as you want - that is being able to write ${Status.OK} .我认为没有您想要的解决方案 - 能够编写${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.它会打印出StatusEnum.OK ,而不是您可能期望的0 You'd need to write Status.OK.value to get 0 .您需要编写Status.OK.value才能获得0 More on that in the docs .更多关于docs的内容。

So, the closest I was able to do this is this:所以,我能做到的最接近的是:

Status.py状态.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM