问题
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