user1673206
user1673206

Reputation: 1711

Running test case In loop instead of copy the code

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:)

Upvotes: 0

Views: 967

Answers (2)

Bence Kaulics
Bence Kaulics

Reputation: 7271

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:

enter image description here

Upvotes: 1

DanielRadl
DanielRadl

Reputation: 101

The easyest way is to define a Keyword with Arguments and Return Values. You can call this Keyword in every Tescase (with specified tag/documentation etc), with specified Arguments for the Test and check the Reuturn Values.

Upvotes: 1

Related Questions