简体   繁体   English

使用机器人框架测试具有 For 循环的函数的输出

[英]Test output of function that have For loop using robot-framework

I have function that have For loop and return for each iteration True or False我有具有 For 循环并为每次迭代返回 True 或 False 的函数

def testcase(x,testType=""):
        parameter_list = open("Parameter_List.csv")
        Parameters = csv.reader(parameter_list)
        if x =='compagne' :
            for row in Parameters :
                param = row[0].split(";")
                if param == ["Name","Format","Arraysize","MinVal","MaxVal","Default Val"] :
                    pass
                else :
                    parameter = Parameter(param[0], param[1], param[2], param[3], param[4], param[5])
                    return parameter.robotALLtest()
    
        elif x == 'one test' and testType in ["Min Value", "Max Value", "Lower Min", "Greater Max", "Persistence ON", "Persistence OFF", "Rset"] :
            for row in Parameters:
                param = row[0].split(";")
                if param == ["Name", "Format", "Arraysize", "MinVal", "MaxVal", "Default Val"]:
                    pass
                else:
                    parameter = Parameter(
                    param[0], param[1], param[2], param[3], param[4], param[5])
                    return parameter.robotTestParameter(testType)
        else :
            for row in Parameters:
                param = row[0].split(";")
                if x == param[0] :
                    parameter = Parameter(param[0], param[1], param[2], param[3], param[4], param[5])
                    break
            return parameter.robotTestParameter(testType)

I used RobotFreamwork to test the output我使用 RobotFreamwork 来测试输出

*** Settings ***
Library  test_cases.py

*** Test Cases ***
Test Parameter
    
    ${value}  test_cases.testcase   ${'one test'}   ${'Lower Min'}
    SHOULD BE EQUAL     ${value}    ${True}

But i have just the result for the first iteration.但我只有第一次迭代的结果。 What can i do to get all the results?我该怎么做才能获得所有结果?

In the example you gave:在您给出的示例中:

        elif x == 'one test' and testType in ["Min Value", "Max Value", "Lower Min", "Greater Max", "Persistence ON", "Persistence OFF", "Rset"] :
            for row in Parameters:
                param = row[0].split(";")
                if param == ["Name", "Format", "Arraysize", "MinVal", "MaxVal", "Default Val"]:
                    pass
                else:
                    parameter = Parameter(
                    param[0], param[1], param[2], param[3], param[4], param[5])
                    return parameter.robotTestParameter(testType)

your code looks like "lets read this csv file, if the row looks like a header, lets skip that but if its actual test data, it goes to the else branch.您的代码看起来像“让我们读取这个 csv 文件,如果该行看起来像一个标题,让我们跳过它,但如果它的实际测试数据,它会转到 else 分支。

And that else branch will return parameter.robotTestParameter(testType) from the testcase() , thus, you only loop 2 rows from the Parameters and first non-header row is the only that is applied.并且那个 else 分支将从testcase() return parameter.robotTestParameter(testType) ,因此,您只从 Parameters 循环 2 行,并且第一个非标题行是唯一应用的。

So, essentially, your code works exactly how you wrote it.因此,本质上,您的代码完全按照您编写的方式工作。

On python side, you could do something like在 python 方面,你可以做类似的事情

result = True
for row in Parameters:
   param = row[0].split(";")
   if param == ["Name", "Format", "Arraysize", "MinVal", "MaxVal", "Default Val"]:
       pass
   else:
       parameter = Parameter(
       param[0], param[1], param[2], param[3], param[4], param[5])
       if parameter.robotTestParameter(testType) == False:
           result = False
return result

BUT this is not very good approach to solve the situation .. I'd start looking and learning on how to use Datadriver library / https://github.com/Snooz82/robotframework-datadriver但是这不是解决这种情况的好方法..我会开始寻找和学习如何使用 Datadriver 库/https://github.com/Snooz82/robotframework-datadriver

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

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