How can I check list with numbers, that is ordered numerically in Robot Framework

自闭症网瘾萝莉.ら 提交于 2021-02-11 17:57:34

问题


I have problem with checking my list. Actually I need check that list is ordered numerically in Robot Framework.

Let's imagine that we have a list

${nice}= ['13', '12', '10', '7', '6', '6', '6', '4', '3', '2', '2', '1', '1', '1', '0', '0']

I need to verify that the first element is greater than the second, the second greater than the third and so on.

Problem is, that in Robot Framework keyword 'Sort List' doesn't order number list in proper way.

One of the decision is to call Python method 'sort' or 'sorted' in robot framework, but maybe there is better way to do it?


回答1:


Sort List keyword sorts lists as strings so it puts 11 before 2 for example.

If you have to check if a list is ordered numerically, you can do that without sorting. You should simply iterate the list and compare adjacent elements to each other. For example:

*** Variables ***
@{LIST_NOK}     13    12    10    7    6    6    6    4    3    2    2    1    1    1    0    0
@{LIST_OK}     999    765    213     78    21    12   2

*** Test Cases ***
Test
    Check List    ${LIST_OK}
    Check List    ${LIST_NOK}
    
    
*** Keywords ***
Check List
    [arguments]    ${list}
    ${length}=    Get Length    ${list}
    FOR     ${i}    IN RANGE    ${length-1}
        ${first}=    Set Variable    ${list}[${i}]    # element at index i in ${list}
        ${second}=   Set Variable    ${list}[${i+1}]  # element at index i+1 in ${list}
        Run Keyword If    ${first} <= ${second}    Fail    Element ${first} is not greater than ${second}.
    END

If you do not like seeing these ${list}[${i+1}], so extended variable syntax in use, you should go with Python. Either via the Evaluate keyword or via some small test library.




回答2:


You can use python's sorted using Evaluate keyword to get the list elements in descending order and then use Lists Should Be Equal keyword to compare them

Import Library     Collections
@{nice}=    Create List    13    12    10    7    6    6    6    4    3    2    2    1    1    1    0    0                                              
${sorted}=    Evaluate     sorted(${nice}, key=int, reverse=True)                                                                                       
Lists Should Be Equal    ${nice}    ${sorted}   


来源:https://stackoverflow.com/questions/63598557/how-can-i-check-list-with-numbers-that-is-ordered-numerically-in-robot-framewor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!