在循环中运行测试用例而不是复制代码 [英] Running test case In loop instead of copy the code

查看:48
本文介绍了在循环中运行测试用例而不是复制代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在机器人中进行了几次测试.所有测试的idea都是一样的:

- 加载一些参数到模块- 跑步- 将预期结果与实际结果进行比较

不同的测试之间唯一不同的是input预期结果.

我想用不同的输入重复运行测试 - 每次迭代都将被视为不同的测试用例 - 而不是 复制相同的代码测试用例并更改输入.

每次迭代都会有自己的测试用例标签\文档\名称(可以说是迭代次数)

例如:

FOR ${TC} IN @{TCS} #TCS 是输入和预期输出的数组*** 测试用例 ***# 编辑标签\文档\测试名称module.load ${TC['input']}${output} = module.runisValid ${output} ${TC['expectedOutput']}结尾

机器人有可能吗?

谢谢:)

解决方案

您可以使用

I have several tests in robot. the Idea of all the tests is identical:

- load some parameter to module
- run
- compare expected to actual results

The only thing is different from test to test, is the input and the expected results.

I would like to run the test repeatedly but with different inputs- and each iteration will be considered as different test case - Instead of copy the same code for all of the test cases and change the inputs.

each iteration will have its own test case tag \ documentation \ name (lets say the iteration number)

for example:

FOR  ${TC}  IN  @{TCS}                            #TCS is array of inputs and expected output
   *** Test Cases ***
   # edit the tag \ documentation \ test name
   module.load  ${TC['input']}
   ${output} =  module.run
   isValid   ${output}  ${TC['expectedOutput']}
END

Is it possible in robot?

Thanks:)

解决方案

You could use the test template feature of the framework, especially the template tests with for loop.

Using it would give the following advantages:

  • No code duplication. You need one keyword with the test logic, that will be invoked with all list elements.
  • Each iteration will be independent from the other. So if one iteration fails, the next will be still executed.
  • It is flexible. The number of iterations is dynamic, you can create an input list in a test or suite setup phase.

Example, note that I am providing my inputs from a variable file.

*** Settings ***
Variables    VariableFile.py

*** Test Cases ***
Math test
    [Template]    Multiplication by 2
    FOR    ${TC}    IN    @{TCS}
        input=${TC.input}    output=${TC.output}
    END
    
*** Keywords ***
Multiplication by 2
    [arguments]    ${input}    ${output}
    ${result}=    Evaluate    ${input}*2
    Should Be Equal As Integers    ${output}    ${result}    Calculated result:${result} is not equal with the expected:${output}

Variable file:

class DataSet(object):

    def __init__(self, input, output):
        self.input = input
        self.output = output
        
    def __str__(self):
        return f'i:{self.input} - o:{self.output}'
        

TCS = [DataSet(1,2), DataSet(2,4), DataSet(3,6), DataSet(3,7), DataSet(4,8)]

This is the output:

这篇关于在循环中运行测试用例而不是复制代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆