This page looks best with JavaScript enabled

Robot Framework Advanced (1)

 ·  ☕ 2 min read

pybot Command

  • Run all test cases
1
pybot .
  • Run a certain test suite
1
pybot testsuite.txt
  • Run a test case in a certain test suite
1
pybot --test case_name testsuit.txt
  • Output the test results to a fixed path
1
pybot --ouputdir your_ouput_dir testsuit.txt
  • Run test cases that contain a certain tag
1
pybot --include tag_name testsuit.txt

About Logging

By default, log messages below the INFO level in Robot Framework are not written to the log. This threshold can be changed with the command-line option --loglevel.

There are five log levels in Robot Framework:

  • FAIL

Used when a keyword fails, and only usable by Robot Framework itself

  • WARN

Used to show warnings. Warning messages also appear on the console and in the test execution error section of the log file, but they do not affect the status of a test case

  • INFO

The default message level. By default, messages below this level are not shown in the log file

  • DEBUG

Used for debugging purposes. It is useful when you need to record the internal execution process of a test library. When a keyword fails, the place where the code failed automatically prints traceback information at this level

  • TRACE

A more detailed debugging level. At this level, the arguments and return values of a keyword are automatically written to the log

In a test case, you can call the built-in keyword log to output the corresponding logs:

For example:

1
2
log    "this is my log"    warn
log    ${num}    warn

Output:

1
2
[ WARN ] "this is my log"
[ WARN ] 100

The first argument is the log content, the second argument is the log level, and the default is info.

Declaring Variables

  • Variables

In a test suite:

1
2
${num}    100
${string}   abcdef

In a test case:

1
2
${num}    Set Variable    100
${string}    Set Variable    abcdef
  • Lists

In a test suite:

1
2
@{num_list}       1    2    3
@{string_list}    a    b    c

In a test case:

1
2
@{num_list}    Create  List    1        2         3
@{string_list}    Create  List    a       b          c

There are two ways to access a list:

  1. @{variable_name}[index]
  2. ${variable_name[index]}
  • Dictionaries

In a test suite:

1
&{user}  name=admin    email=admin@admin.com

In a test case:

1
&{user}    Create Dictionary    name=admin    email=admin@admin.com

You can also declare a dictionary variable with the scalar sign, ${user} .

Access methods:

1
2
log    ${user.name}
log    ${user['name']}

Conditional Expressions

Syntax structure:

1
2
3
4
5
Run Keyword If  condition  other_keyword  arguments

...     ELSE IF   condition  other_keyword  arguments

...     ELSE   condition  other_keyword  arguments

For example:

1
2
3
4
5
testCase
    ${n}    Set Variable    3
    Run Keyword If    ${n}==100    log    num is 100
    ...    ELSE IF    ${n}>100    log    num is more than 100
    ...    ELSE    log    num is less than 100

Note that keywords are not case-sensitive, but the ELSE and ELSE IF here are strictly case-sensitive.

Loop Expressions

Syntax structure:

1
2
3
4
5
6
7
:For variable  IN  sequence(or list)

     keyword argument_value

:For variable  IN RANGE loop_limit

     keyword  argument_value

For example:

1
2
3
4
testCase
    ${t}    create list    123    qwe    dfg
    : FOR    ${x}    IN    @{t}
    \    log    ${x}    warn

Arguments and Return Values

In Robot Framework, passing arguments to a keyword or getting a return value from a keyword is a very common operation.

In a keyword definition, arguments are defined with [Arguments], and a default value can also be set as ${version}=0. An argument with a default value does not have to be passed when used; otherwise it must be passed. When passing arguments, you can use the default positional form, or the key/value form.

In a keyword definition, a value is returned with [Return].

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
*** Test Cases ***
testCase
    ${tem} =    获取完整版本    18
    log    ${tem}    warn

*** Keywords ***
获取完整版本
    [Arguments]    ${version}=0
    ${full_version}    set variable    Test-${version}
    [Return]    ${full_version}

Extending Python Variables with the Variables Approach

In Robot Framework, sometimes you need to use Python to handle some logic and obtain a computed value. In this case, you can use a Variable to extend Python variables.

test.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# -*- coding: utf-8 -*-
import random

class Test(object):
    def __init__(self):
        pass

    def random(self):
        return random.randint(1, 100)

myrand = Test()
*** Settings ***
Variables  test.py

*** Test Cases ***
testCase
    log    ${myrand.random()}    warn

Extending Python Functions as Keywords with the Library Approach

In Robot Framework, sometimes you need to use Python to wrap some operations, such as sending email. In this case, you can use the Library approach to import Python functions as keywords in Robot Framework.

mylib.py

1
2
3
# -*- coding: utf-8 -*-
def myprint(name):
    return "Your name is :" + name
1
2
3
4
5
6
*** Settings ***
Library           ../lib/mylib.py
*** Test Cases ***
testCase
    ${name}=    my_print    haha
    log    ${name}    warn

微信公众号
WRITTEN BY
微信公众号