Robotframework Notes 11

Source: Internet
Author: User

Syntax basic syntax for test cases

The test case constructs the available keywords in the test case table. Keywords can be imported into the test case file itself, or a resource file or a created keyword table.

The first column in the test Case table contains the name of the test case. A test case begins in this column and the line continues to the end of the next test Case name or table. It is between a error table title and a first Test.

The second column usually has the name of the keyword. One exception is to set the variable return value from the keyword when the second and possibly subsequent column contains the variable name and the name of the keyword is located. In both cases, the name of the column in the keyword contains the keywords that the possible parameters specify.

Test Cases ***valid Login    Open login Page    Input Username    demo    Input Password    mode    Submit Credentials    Welcome Page should be opensetting Variables do    Something first    argument    second argument< C10/>${value} =    Get Some value    should be Equal    ${value}    expected value
Set the test Case table

Test cases can also have their own settings. Setting the names always in the second column is usually the keyword and their values are in the subsequent columns. Set names square brackets they differentiate keywords. The list of available settings is explained below in this section.

Document
Used to specify a test case document.
Label
The test case for the label.
(set), (disassembly)
Specify test installation and disassembly.
Template
The keyword used for the specified template. The test itself contains only the data of the keyword as a parameter.
Timeout
used to set a test case timeout. Timed out to discuss their own part.

Example setting test cases:

Test Cases ***test with Settings    [documentation]    another dummy Test    [Tags]    dummy    Owner-johndoe    Log    Hello, world!
Related settings in the Test Case settings table

The settings table can have the following related test case settings. These settings are primarily the default values for pre-IPO test case specific settings.

The label of the Force, the default label
Mandatory and default value labels.
Test setup, Test disassembly
The default value for test installation and disassembly.
Test template
The default template is used by the keyword.
Test timed out
the default value of the test case timed out. Timed out to discuss their own part.
Using Parameters

The previous example has proven the different parameters of the keywords, this section discusses this important function more thoroughly. How to really implement user keywords and library keywords with different parameters is discussed in different sections.

A keyword can accept 0 or more parameters, and some parameters have default values. What the parameter keyword accepts depends on the document that it implements, usually the best place to search for information about keywords. In this example, some of the documents will use the generated Libdoc tool, but the same information is available for document generation tools such as generic documentation javadoc  .

Mandatory parameters

Most keywords have a certain number of parameters and must always be taken into account. The name of the document parameter with the specified keyword is separated by commas 第一,第二, 第三  . Parameter names are not really important in this case, except that they should explain what the argument is, but it is important to have exactly the same number of parameters in the specified document. Using too little or too much controversy will result in errors.

The following tests use keywords to create directories and copy files from the operating library. Their argument is specified as 路径  and 源,目的地  , which means they put one and two parameters, respectively. The last keyword, no action from the inline, does not accept parameters.

Test Cases ***example    Create Directory    ${tempdir}/stuff    Copy File    ${curdir}/file.txt    ${ Tempdir}/stuff    No operation
Default value

Parameters can usually be given or have default values that are not. The default value in the document is usually separated from the parameter name by an equal sign name =违约 价值  , but the keyword uses Java implementations that may implement the same keyword with different parameters. It is possible that all parameters have default values, but cannot have any positional parameters after parameters with default values.

Use the default values to create the parameters of the file keyword, as shown in the following example 路径、内容= utf - 8编码=  . Trying to use it without any parameters or the above three parameters will not work.

Test Cases ***example    Create file    ${tempdir}/empty.txt    create file    ${tempdir}/utf-8.txt         Hyväesimerkki    Create File    ${tempdir}/iso-8859-1.txt    Hyväesimerkki    iso-8859-1
Variable number of parameters

It is also possible to accept any number of arguments in one word. These so-called mutable parameters can be combined with mandatory parameters and parameters using default values, but they always post them. In the previous document there is an asterisk in the argument name like *可变参数  .

For example, deleting a file and adding a path keyword to the operation library has controversy *路径  and 基地,*部分  , respectively. The former can use any number of parameters, but the latter requires at least one parameter.

Test Cases ***example    Remove Files    ${tempdir}/f1.txt    ${tempdir}/f2.txt    ${tempdir}/f3.txt    @ {Paths} =    Join paths    ${tempdir}    f1.txt    f2.txt    f3.txt    f4.txt
Named parameters

The syntax of a named parameter makes it more flexible with the default value of the parameter, and allows you to explicitly identify what a parameter value means. Technically named parameters are exactly the same as the Working keyword parameter in python.

Basic syntax

It is possible to name the prefix parameter to give a keyword value with the name of the parameter arg =值  . This is especially useful when multiple parameters have default values, possible names have only some parameters and default values that let others use them. For example, if a keyword accepts a parameter __arg1 = a、最长= b,长度= c  , and it is called a parameter 长度=覆盖  , the parameter __arg1  and the 最长  default value, but 长度  gets the value 覆盖  . If this sounds complicated, the name parameter example below wants to make it clearer.

Named parameter syntax is both case and space sensitive. The former means that if you have an argument 参数  , you have to use it arg =值  , nor does it Arg =值  ARG =值  work in principle. The latter means that space is not allowed before the symbol, and the possible space is considered to be part of the given value.

When using the syntax of a named argument to a user's keyword, the argument name must be no $ { }  decoration. For example, the user keyword usage parameter $ { __arg1 } =第一,$ {最长} =  must be used just like 最长=覆盖  .

Use a named parameter after the normal positional parameters, for example, | | |关键字参数=价值位置|  do not work. From the robot framework 2.8 This will result in an explicit error. The relative order of named parameters does not matter.

Please note

Robot frame 2.8 is not possible before the named parameter does not have a default value.

Named parameters and variables

You can use variables to specify parameter names and values. If the value is a single scalar variable, it is passed to the keyword as-is. This allows for the use of any object, not only the string, but also the syntax of the named arguments in the values. For example, calling a keyword arg = $ {对象}  will $ {对象}  not convert the variable to a string of keywords.

If the variable is used to name the parameter, the variable is resolved before the matching parameter name. This is a new feature in the robot frame 2.8.6.

The named parameter syntax requires the equals sign to be casually written in the keyword's phone. This means that a variable alone cannot trigger a named parameter syntax, even if there is a value foo = bar  . This is to be remembered especially for packaging keywords to other keywords. For example, if a key number of variable arguments is like @ { args }  and passes them all to another using the same keyword @ { args }  syntax, the syntax may be 名叫=参数  used for callers not to endorse. Shown in the following example.

Test Cases ***example    Run program    shell=true    # This won't come as a named argument to Run process*** Ke Ywords ***run program    [Arguments]    @{args}    Run Process    program.py    @{args}    # Named Arguments is not recognized from inside @{args}

If the keyword needs to be accepted and passed any of the named parameters, you must accept the change to the free keyword parameter. See the Kwargs example for the wrapper keyword version, which can be advanced by two positional parameters and named parameters.

Escape syntax for named arguments

Use the named parameter syntax only if part of the parameter in the equals sign matches a parameter of the keyword. It is possible to have a positional argument and a literal value foo = quux  , and there is an argument unrelated to the name 喷火  . In this case, the parameter is either an incorrect 喷火  value quux  or, more likely, there is a syntax error.

In rare cases where there is an unexpected match, it is possible to use the backslash character to evade the syntax just like foo \ = quux  . Now the view will get a text value foo = quux  . Note that escaping is not necessary if there is no argument for the name 喷火 , but because it makes the situation more explicit, but it may be a good idea.

Support for named parameters where

The named parameters have been explained by the syntax using keywords. In addition, it is still working in the import library.

Named parameters support the user's keywords and most test libraries. The only exception is the use of the Java library-based static library API. Library document Generation Libdoc Do you have a note about the library? supports named parameters.

Please note

Robot framework 2.8 Before naming parameter syntax does not work with APIs that use the dynamic library with test libraries.

Examples of named parameters

The following example demonstrates the use of a named parameter Syntax library keyword, the user keyword, when importing a remote login Test library.

Settings ***library    Telnet    prompt=$    default_log_level=debug*** Test Cases ***example    Open Connection    10.0.0.42    port=${port}    alias=example    list files    options=-lh    list Files    Path=/tmp    options=-l*** Keywords ***list files    [Arguments]    ${path}=.    ${options}=    List files    options=-lh    Execute command    ls ${options} ${path}
Free keyword Parameters

Robot framework 2.8 adds support for Python-style free keyword parameters ( * * kwargs  ). This means that the keyword can receive all parameters using 名称=值  syntax and other parameters as Kwargs inconsistencies.

The same is true for free keyword parameters that support variable named parameters. In practice this means that the variable can use the name and value, but the jailbreak flag must be visible. For example, foo = $ {酒吧}  and $ { foo } = $ {酒吧}  is valid, as long as the variables used exist. An additional restriction is that the keyword parameter name must be a string for free. A new feature in the name of the support variable 2.8.6 the robot frame, before the variable un-resolved.

Initially only free keyword parameters are associated with the Python-based library, but the robot framework 2.8.2 extended support for dynamic libraries of API and bot framework 2.8.3 will be further extended to the Java Libraries and remote library interfaces. Finally, the user keyword has kwargs support in robot frame 2.9. In other words, now all the keywords can support Kwargs.

Kwargs Example

As the first example of using Kwargs, let's take a look at the process library that runs the process keyword. It has a signature 命令,*参数,* *配置  , which means it requires the command to execute ( 命令  ), its parameters are variable in number of parameters ( *参数  free keyword), and the last optional configuration parameter parameter ( * *配置  ). The following example also shows that variables use the named parameter syntax exactly as they do with the free keyword argument.

Test Cases ***using Kwargs    Run process    program.py    arg1    arg2    cwd=/home/user    Run process    program.py    argument    shell=true    Env=${environ}

See the Free keyword parameter (* * Kwargs) next section to create a test library for more information about using Kwargs syntax for your custom test library.

For the second example, let's create a wrapper user keyword for running program.py  in the example above. The wrapper keyword runs the program to accept any number of arguments and Kwargs, passing forward the name of the run process in the execution command.

Test Cases ***using Kwargs    Run program    arg1    arg2    cwd=/home/user    Run Program    argument    shell=true    env=${environ}*** Keywords ***run program    [Arguments]    @{arguments}    &{ Configuration}    Run Process    program.py    @{arguments}    &{configuration}
The name of the parameter embedded in the keyword

A completely different way to specify the names of the parameters embedded in the keyword. Support for this syntax test library keywords and user keywords.

Failure when test case fails

A test case fails if any of the keywords use fails. Typically this means that the test case that is executed is stopped, a possible test disassembly is performed, and then the next test case continues to execute. You can also use a special sustainable failure if you do not need to stop the test execution.

Error message

The error message that is assigned to a failed test case is directly from the failed keyword. The error message is usually the keyword itself, but some keywords allow them to be configured.

In some cases, such as when using a sustainable failure, a test case can fail many times. In this scenario, the last error message is combined by a personal error. Long error messages are easier to read by automatically cutting reports from among them. The full error message is always visible in the log file as a failed message keyword.

The default error message is normal text, but from the robot framework 2.8 contains the HTML format. This is enabled and tagged by the start of the string error message * HTML *  . This tag will be removed from the final report as shown in the error messages and logs. A custom message that uses HTML is shown in the second example.

Test Cases ***normal Error    Fail This is    a rather boring example ... HTML Error    ${number} =    Get number    should be Equal    ${number} *html* number are not    my <b> Magic</b> number.
Test case names and documents

The test case name comes directly from the test case table: It is exactly what is entered into the test Case column. A unique name should be used in a test case test suite. For this, you can also use automatic variables $ { TEST_NAME }  in the test reference test name. This is a test that can be executed, including keywords for all users, as well as test settings and test disassembly.

The (document) settings allow you to set up a free test case document. The output as shown in the text command line, as well as the resulting test records and test reports. You can use the simple HTML format in documents and variables available for manufacturing document dynamics.

If the document is divided into columns, the cells are connected to the space in one line. This is mostly useful when using the HTML format and the columns are narrow. If the document is split into multiple lines, the created document itself is connected by using line wrapping. If a line break is not added, the backslash has been escaped with a newline character or end.

Test Cases ***simple    [documentation] Simple    documentation    NO operationformatting    [documentation ]    *this is bold*, _this are italic_ and here is  a link:http://robotframework.org    No operationvariables    [D Ocumentation]    Executed at ${host} by ${user}    No operationsplitting    [documentation]    this Documentation is    split to    multiple columns    No operationmany lines    [documentation] here    we Have    ...                An automatic newline    No operation

It is important that test cases have explicit and descriptive names, and in this case they usually do not require any documentation. If a logical test case needs to be logged, it is usually an indication that the keywords in the test case require a better name and that they can be enhanced instead of adding additional documents. Finally, metadata, such as the environment and user information in the last example above, is often a better use of tags.

Label Test Cases

Using tags in the robot framework is a simple, but powerful mechanism for classifying test cases. tags are free text and they can be used for at least the following purposes:

    • The label shows the test report, the log of course, in testing the test case data, so they provide the meta data.
    • Statistical data test Cases (total, passed, failed automatically collected based on tags).
    • Using tags, you can include or exclude execution of test cases.
    • Using tags, you can specify which test cases are critical.

In this section only explains how to set up a label example for a test, the following list shows different ways to do it. These methods can naturally be used together.

The label of the force in the Setup table
All of the test cases in the test case file with this setting always specify the label. If it is used 测试套件初始化文件  , all tests are made with an example test kit to get these tags.
The default label in the Settings table
There are no test cases (tags) to set these labels yourself. The tag test suite file is not supported by default for initialization.
(label) in the test case table
A test case is always these tags. In addition, it will not be possible to assign a label to the default label, so it is possible to overwrite the default label by using a null value. It is also possible to 没有一个  override the default label value.
--settag Command-line Options
All test cases are executed in addition to getting label settings for this option they have other tags.
Set tags, delete tags, fail and execute keywords by
these built-in keywords can be used to manipulate dynamic tags during testing.

The tags are free text, but they are normalized so that the conversion of lowercase and all the space is removed. If a test case gets the same label several times, the other event is deleted than the first one. Tags can create assumptions that use variables that exist in these variables.

Settings ***force Tags      req-42default tags    owner-john    smoke*** Variables ***${host}         10.0.1.42*** Test Cases ***no Own tags    [documentation] This    test has tags owner-john, smoke and req-42.    No operationwith Own tags    [documentation] This    test has tags not_ready, owner-mrx and req-42.    [Tags]    Owner-mrx    not_ready    No operationown tags with variables    [documentation] This    test has tags host-10.0.1.42 and req-42.    [Tags]    Host-${host}    No operationempty own tags    [documentation] This test have only    tag req-42.    [Tags]    No Operationset tags and Remove Tags Keywords    [documentation] This    test has Tags MyTag and Owner-john.    Set Tags    mytag    Remove Tags    Smoke    req-*
Keep labels

Users are usually free to use any tags in their work environment. However, some tags have a pre-defined meaning framework for robots, and using them for other purposes can have unintended consequences. All special tagged robot frames will have a prefix in the future 机器人-  . To avoid problems, you should not use any user tags with a 机器人-  prefix unless the special features are actually activated.

At the time of this writing, the only special markup robot-exit  this is when automatically added to the test to gracefully stop the test execution. More use may be added in the future.

Test installation and uninstallation

The robot framework also has a lot of other test automation frameworks like test setup and cleanup capabilities. In short, a test setup executes a test case before the test execution is disassembled in a test case. Set and disassemble common keyword parameters in the robot frame.

Setup and teardown are always a word. If they need to take care of multiple separate tasks, they can create more advanced user keywords for this purpose. Another solution is to execute multiple keywords using the built-in keyword run keyword.

Test disassembly is particularly in two areas. First, it also executes when a test case fails, so it can be used to clean up the activity, regardless of the state of the test case. In addition, all the key words are disassembled and executed even if one fails. This continuation failure feature can also use normal keywords, but is disassembled in the default case.

The

Simplest way to specify settings for a test case is to set or disassemble the use test case file   test settings   and   Test disassembly   settings table. Individual test cases can also have their own installation or disassembly. Their definitions   (settings)   or (disassembly)   Set up test case tables, they override   test settings   and   test disassembly   settings. No keyword after   (settings)   or   (disassemble)   settings means no installation or disassembly. can also use value   does not have a   indicates that a test is not installed/removed.

Settings ***test Setup       Open application    App atest Teardown    Close application*** Test Cases ***default val ues    [documentation]    setup and teardown from setting table do    somethingoverridden setup    [documentation ]    Own Setup, teardown from setting table    [Setup]    Open application    App B do    Somethingno teardown< C12/>[documentation]    Default setup, no teardown at all do    Something    [teardown]no teardown 2    [ Documentation]    Setup and teardown can be disabled also with special value NONE do    Something    [teardown]
   noneusing variables    [documentation]    Setup and teardown specified using Variables    [Setup]    ${ SETUP} do    Something    [Teardown]    ${teardown}

Executes the name of the keyword to set or disassemble the variable. This helps in different installations or disassembly of different environments to give the keyword name as a variable command line.

Please note

The test kit can be installed and disassembled by its own. A set or sub-test before executing any test suite test suite, same set of disassembly after execution.

Test template

Test templates convert normal keywords to drive test cases in data-driven tests. While the body of a keyword-driven test case is made up of keywords and their possible parameters, the test case template contains only the keyword's parameter template. Instead of repeatedly repeating the same keyword/test and/or all of the tests in a file, you can use it only for each test or file at a time.

The template's keywords can accept normal positional and named parameters, as well as embedded keyword names as parameters. Unlike other settings, it is not possible to define a template with a variable.

Basic usage

How do I use a keyword to accept a normal positional parameter as a template? As in the following example test case. The two tests function exactly the same.

Test Cases **normal test case    Example keyword First    argument    second argumenttemplated test Case    [ Template]    Example keyword First    argument    second argument

As an example, you can specify a template (template) setting that is used by a separate test case. Another way is to use a test template to set the settings in the table, in which case the application template is used for all test cases, test case files. The (template) setting overrides the possible template settings in the Settings table, and a null value (template) means that there are no test templates, even if the test template is used. It is also possible 没有一个  that use value indicates that a test has no template.

If a templated test case has multiple rows of data in the body, the template applies to all rows. This means that the same keyword executes multiple times, once the data is in each row. Templating tests are also special, so that all the rounds even if one or more of the executions fail. You can use this type of continuation failure mode with normal tests, but templated test automatic mode.

Settings ***test Template    Example keyword*** Test Cases ***templated Test case first    Round 1 first     Round 2    Second Round 1    second round 2    third Round 1     third round 2

Using parameter defaults or variable parameters and using named parameters and free keyword parameters, use templates just like they do. Using variables in parameters also supports normal.

Templates and Embedding parameters

An embedded parameter syntax for a VARIANT that is supported by a template from the robot frame 2.8.2. With template syntax is such a keyword, if the name of the template variable, they are considered to be parameters and placeholders replaced with the actual parameters used by the template. Then use the resulting keyword without positional parameters. This is the best illustration of an example:

Test Cases ***normal test case with embedded arguments the    result of 1 + 1 should is 2 the    result of 1 + 2 sho Uld be 3Template with embedded arguments    [Template] The    result of ${calculation} should be ${expected}    1 + 1    2    1 + 2    3*** Keywords ***the result of ${calculation} should be ${expected}    ${result} =    Calcula Te    ${calculation}    should be Equal    ${result}     ${expected}

When you use template embedding parameters, the number of parameters for template keyword names must match the parameters used. Parameter names do not require parameters that match the original keyword, but you can also use completely different parameters:

Test Cases ***different argument names    [Template] The    result of ${foo} should be ${bar}    1 + 1    2    1 + 2    3Only some arguments    [Template] The    result of ${calculation} should be 3    1 + 2    4-1new arguments< C10/>[template] The    ${meaning} of ${life} should be the    result    21 * 2

The main benefit of embedding template parameters is to use explicitly specifying parameter names. When using normal parameters, the same effect can be named by the included column parameter. This is an example of the data-driven style that is illustrated in the next section.

The loop of the template

If you use a template for loop, apply the template at all steps in the loop. The continuation of the failure mode is also in this case, which means that all the steps of the execution are executed even if there is a failure.

Test Cases ***template and for    [Template]    Example keyword    : for    ${item}    in    @{items}    \    ${item}    2nd arg    : for    ${index} in    RANGE    \    1st arg    ${index}
Different styles of test cases

There are several different ways that test cases may be written. The test describes a type of scenario in which a workflow can be written with a keyword-driven or behavior-driven approach. Data-driven methods can be used to test the same workflow for different input data.

Keyword driven mode

Workflow tests, such as valid login tests described earlier, were made up of several keywords and their possible parameters. The normal system structure is the first to the initial state (open the login page in the valid login example), then the system (enter the name, enter the password, submit the voucher), finally verify that the system behavior (Welcome page should be open).

Data-driven style

Another style is to write test case data-driven methods that use only a more advanced keyword, typically creating a user keyword that hides the actual test workflow. These tests are very useful when there is a need to test different input and/or output data for the same scene. It is possible to repeat the same keyword as each test, but the test template feature allows only the specified keyword to be used once.

Settings ***test Template    Login with invalid credentials should fail*** Test Cases * *                USERNAME Passwordi         Nvalid User name                 invalid          ${valid password}invalid PASSWORD                  ${valid user}    invalidinvalid user name and Password    Invalid          invalidempty user Name                   ${empty}         ${valid password}empty Password                    ${valid User }    ${empty}empty User Name and Password      ${empty}         ${empty}

Tips

Named columns make testing easier to understand in the example above. This is possible because the header row is ignored in addition to the first cell.

In the example above, there are six separate tests, one for each invalid user/password combination, and the following example shows how to have only one test for all combinations. When using the test template in the test execution, all the wheels even have failed, so there is no real feature in the difference between the two styles. In the example above, it is easy to see the combination of these tests, but it is possible to make a large number of them in a messy statistic. Which style to use depends on the environment and personal preferences.

Test Cases ***invalid Password    [Template]    Login with Invalid credentials should fail    Invalid          ${ VALID PASSWORD}    ${valid USER}    Invalid    invalid          whatever    ${empty}         ${valid PASSWORD}    ${ VALID USER}    ${empty}    ${empty}         ${empty}
Behavior-driven approach

You can also write test case requirements and non-technical project stakeholders must understand them. These executable requirements are an example of a corner stone process that is often referred to as acceptance Test driven development (ATDD) or specification.

One way to write these requirements/tests is to Given-when-then style to promote behavioral-driven development (BDD). In writing test cases of this style, the initial state is usually expressed as the starting word of the keyword in view of the behavior described with the start of the keyword and then the start of the keyword. Keywords start and OR but can be used if a step is more than an action.

Test Cases ***valid Login    Given login page is open when    Valid username and password are inserted and    CR Edentials is submitted then    Welcome page should be open
Ignore///,/but prefix

Prefix in view, when, and and but to match the keyword search, if there is no matching full name is found. This is the key words and keywords for library users. For example, to open the login page in the example above you can implement the user keyword either with or without the word given. Ignoring prefixes also allows the use of the same keyword with different prefixes. For example the Welcome page should be open or can be used and the Welcome page should be open.

Please note

Ignoring but the prefix is the new 2.8.7 robot frame.

Embed data keywords

It is useful to write specific examples that can be implemented with actual data keywords. Support this by allowing the user keyword to embed the parameter as the keyword name.

Robotframework Notes 11

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.