robotframework--inline python evaluation

164 阅读1分钟

变量语法也可以用于计算Python表达式。基本的语法是expression,即表达式周围有双花括号。表达式可以是任何有效的Python表达式,例如{{expression}},即表达式周围有双花括号。表达式可以是任何有效的Python表达式,例如{{1 + 2}}或{{\['a', 'list'\]}}。表达式周围的空格是允许的,因此{{1 + 2}}和${{['a', 'list']}}也是有效的。除了使用普通标量变量外,列表变量和字典变量也分别支持@{{expression}}和&{{expression}}语法。

*** Test Cases ***
001_TC_python evaluation
    log    ${1+2}
    log    ${{1+2}}
    ${var}    set variable    1    2    3    4    5
    log    ${{['a', 'list']}}
    log    (${{len(@{var}) > 3}}, ${{$var[0] if $var is not None else None}})
    log    (${{decimal.Decimal('0.11')}}, ${{datetime.date(2019, 11, 5)}})
    log     (${{random.randint(0, 100)}}, ${{datetime.date.today()}})
    log     (${{[1, 2, 3, 4]}}, ${{ {'id': 1, 'name': 'Example', 'children': [7, 9]} }})
    log    (${{math.pi}}, ${{platform.system()}})

ride 执行结果

Starting test: RobotAutoTest.TestCase.MyTest.My Test Suit3 test variable.001_TC_python evaluation
20210722 11:25:05.677 :  INFO : 3
20210722 11:25:05.679 :  INFO : 3
20210722 11:25:05.680 :  INFO : ${var} = ['1', '2', '3', '4', '5']
20210722 11:25:05.681 :  INFO : ['a', 'list']
20210722 11:25:05.690 :  INFO : (True, 1)
20210722 11:25:05.692 :  INFO : (0.11, 2019-11-05)
20210722 11:25:05.694 :  INFO : (30, 2021-07-22)
20210722 11:25:05.696 :  INFO : ([1, 2, 3, 4], {'id': 1, 'name': 'Example', 'children': [7, 9]})
20210722 11:25:05.699 :  INFO : (3.141592653589793, Windows)
Ending test:   RobotAutoTest.TestCase.MyTest.My Test Suit3 test variable.001_TC_python evaluation