Reputation: 21
i have python script named a.py inside that script i have a function named fun that check all the number from 0 to 99 and return True if the integer is even and False if the integer is odd.
def fun():
for i in range(100) :
if i % 2 == 0 :
return True
else :
return False
i want to run this script in Robot Framework
*** Settings ***
Library a.py
*** Test Cases ***
Test
${val} = a.fun
Should Be Equal ${val} ${True}
i can't run all the test cases i have just the result of the first iteration. i want run all the test cases. I know that i can use Robot Framework directly but i dont want to use the Robot Framework syntax.
Upvotes: 0
Views: 440
Reputation: 7970
The issue you have has nothing to do with Robot Framework but your python code just doesnt work as you expected - as i tried to explain in the original code. Here's 100% python code that should visualize your issue and take my original answer into account:
def fun():
for i in range(100):
print(f"verify ${i=}")
if i % 2 == 0:
print(f"Before return of {i=} is even")
return True
print(f"After return of {i=} is even")
else:
print(f"Before return of {i=} is odd")
return False
print(f"After return of {i=} is odd")
print("This will never be printed because for look will 100% exit before code reaches here")
def even_more_fun():
results = []
for i in range(100):
print(f"verify ${i=}")
if i % 2 == 0:
print(f"Before return of {i=} is even")
results.append(True)
print(f"After return of {i=} is even")
else:
print(f"Before return of {i=} is odd")
results.append(False)
print(f"After return of {i=} is odd")
print("This will be printed because we went thru the whole loop but now, results for each value in your given range..")
return results
print("Calling fun() - see the output of your code")
result_of_fun = fun()
print("Calling even_more_fun() - see the output even more closely")
result_of_even_more_fun = even_more_fun()
print("And now we print the results")
print(f"{result_of_fun=}")
print(f"{result_of_even_more_fun=}")
Now, fun()
gets called once.. It will return a single True value because 0 % 2 == 0
evaluates to True. Then your code will stop execution because you have a return state to return that value.
Next, even_more_fun()
- it also gets called once but it will return a list of results for every item in your test data, which is range(100)
in your case.
Copy that script to your machine and run it .. See the difference of outputs from fun() and even_more_fun().
This is how robot and python handles it (and any other sane programming language).
You either have the list of all values you want to test ON YOUR ROBOT CODE and you call fun()
separately with each value OR you fix your python code and return a list of results for each value to make your code work and then return that list.
Upvotes: 1