2-hour introductory robot Framework

Source: Internet
Author: User
Tags how to write test cases i18n wxwidgets

1, Introduction 1.1, Introduction robot

The Robot framework is a keyword-driven, automated testing framework. With this framework, testers can use Python to encapsulate keywords and build executable test cases using keywords in a non-code environment

Robot Framework official Website: http://robotframework.org/

1.2, installation 1.2.1, Robot Framework

Robot Framework supports Python and Jython, this document is for Ubuntu under Python

: https://pypi.python.org/pypi/robotframework, download the latest version of the installation package if Windows can download the EXE installation package directly, Ubuntu can use tar.gz package for source installation, of course, Windows also supports source installation

After downloading, unzip the tar package and go to the folder to execute the sudo pythonsetup.py install to complete the installation. After the installation is complete, you can perform the pybot--version test on the terminal for successful installation

1.2.2, Robot Framework IDE (RIDE)

Ride is a third-party test case editor for Robot, which can edit use cases, edit keywords, and generate files in formats such as Html,txt. At the same time ride can be directly test execution. Ride is based on Wxpython development, so before installing ride need to install Wxpython, its official website is: http://www.wxpython.org/

Windwos Download EXE package to install

In Ubuntu under the terminal command line installation, the method is as follows:

1,curl HTTP://APT.WXWIDGETS.ORG/KEY.ASC | sudo apt-key add–

2, modify the/etc/apt/sources.list file, append the following content

Deb Http://apt.wxwidgets.org/gutsy-wx Main

DEB-SRC HTTP://APT.WXWIDGETS.ORG/GUTSY-WX Main

3,sudo apt-get Update

4,sudo apt-get Install python-wxgtk2.8 python-wxtools wx2.8-i18n

5,sudo apt-get Install python-wxgtk2.8 python-wxtools wx2.8-i18n Libwxgtk2.8-dev Libgtk2.0-dev

Ride installation after Wxpython installation is complete

: http://code.google.com/p/robotframework-ride/, installation method with robot installation package

1.2.3, official quick Start and example

Download the starter Manual: Robotframework-quickstart-20090113.zip and Unzip, enter the folder to see a quickstart.html, open the file to see the official Quick Start document. At the same time, enter the folder in the terminal to execute the command Pybot quickstart.html can see robot performed a series of tests. This help document is a rare example of example itself, the principle is that robot can recognize the table in the HTML page, as long as the contents of the table and robot execution of the syntax of the use case, you can successfully execute the use case. After execution, you can see the report file generated under the current folder.

2, simple use robot Framework2.1, Hello World

Create a Demo folder under/home, for example, named Robot_test. Open the Ride editor using the command ride.py in the terminal. Click the Ride menu bar File->open directory to select the Robot_test folder and then OK.

Create a new robottest.py in the Robot_test directory and then write the Python code in the file as follows

[Python]View Plaincopy
    1. Import OS
    2. Import Sys
    3. Class Robottest:
    4. def first_keyword (self):
    5. fp = open ("/TMP/ROBOT_FK", "a")
    6. Fp.write ("This is the firstkeyword\r\n")
    7. Fp.close ()

Such a testlib file is created, and then the test case is created in ride:

On the top-side menu bar of Ride, select File->new Project as follows

In the right-hand edit box, open the setting option below

Click documentation Large Text edit box, enter the text, you can enter Chinese and save, you can create a robot_test.html file in the Robot_test directory to open the view style

First you need to load the newly edited robottest.py this library, by clicking on the left robot_test, locate the Library button on the right and click Browse to find the robottest.py file.

Then add a use case

In Ride, right-click Robot_test to select the new Test case, enter a name for the

Then click on first case to see the Use Cases edit box, the following table to add the initial keyword keyword as follows

So a simple use case is done.

Use case execution:

Click on the ride most measured button

You can see that ride starts executing the test case, after the execution is complete as

After entering the/tmp directory to see a ROBOT_FK file, you can open the see a string of characters this isthe first keyword, which means our test was successful.

2.2. Brief introduction of robot working principle

Robot's test cases and configurations are edited using a format file such as Html,txt, which is a more commonly used format for editing use cases in HTML-drawn tabular form for higher readability.

Robot determines that the table's configuration is done by identifying the header in the HTML table, such as settings used to configure the repository, test cases to edit the testing case, variables to configure the default variables, and so on.

Each robot project starts working by loading related resources, usually testlib, or importing other profiles, etc., and then finds all the test cases tables and executes each case sequentially. Each step of each test case usually has a keyword to perform the action on that step. Robot will go to the library specified in settings to find the keyword, if found to execute the code corresponding to the keyword, if the keyword can not be found in the Lib Code, the table in the Search HTML table header is keywords table, if not found will be an error. When the test case is executed and there is no error, the case is considered pass.

3, Robot framework detailed 3.1, Robot format

The configuration described in the official documentation is as follows

Instead, the HTML file format generated by the ride editor is as follows:

If you use a different HTML editor to edit the robot-related configuration, it is recommended that you edit the standard style in the official description.

3.2, TestLib

Testlib is the core of the robot Framework keyword, which is used to encapsulate the underlying keywords. Robot with a part of very common testlib such as xml,string, etc., there are other third-party development of the Testlib such as Web page testing related, database-related, in robot only need to import these libraries correctly, Then you can use the keywords encapsulated in these lib. In the automated test development, the most important process is to develop a good testlib, packaging a strong reusability, clear and unambiguous keywords.

Testlib Writing Rules:

1,testlib is a python file that is written in Python code, so testlib usually ends with. py.

2,testlib only and has only one class, and the class name is the same as the Testlib file name. The name of class defined in robottest.py as described in 2.1 is also robottest

The function defined in 3,class is the underlying keyword in robot, so some private functions, which are not intended to open as a keyword, start with the function name "__"

4,class function names are usually defined as Defabc_def_ghi (self, XXX): In such a format, the words are separated by an underscore, so that the defined functions name can be used to edit the test case directly using the keyword ABC def ghi or ABC def GHI is also allowed. Therefore, it is recommended to use only lowercase letters and underscores when defining functions in class, which also conforms to Python's programming specifications.

5, other specifications conform to Python specifications, such as the use of system functions such as Os,sys the import is still to import

Testlib that meet the above conditions can be used by robot. Readers can build a testlib and implement one or two keywords and import them into the ride. Import method Reference 2.1 example, if the ride in the library color display is red, it is likely that the testlib a bit of a problem, the most direct problem is that the class name and file name are different causes the Lib is not found, or the head import a library error.

3.3, Robot's main configuration (HTML)

Any use of ride editing HTML need not be too concerned about the format problem, so specific to the table format-related problems are not covered, now introduce some common configuration, these configurations are usually found in ride, only need to fill in the content. The following is an example of the example in section 2.1

3.3.1, Setting

Click Robot Test in Ride to see the settings open on the right to see more configurations

3.3.1.1, Documentation

A description document, usually used to describe the purpose of the HTML, similar to a Help document or description

3.3.1.2, Suite Setup

The actions that are performed at startup of each test suite, using the same keywords in the test case

3.3.1.3, Suite Teardown

Each test suite is executed at the end of the action, using the same keyword as in the testing case.

3.3.1.4, Test Setup

Each test case starts when the action is executed, using the same keyword as in the test cases. If Setup is specified in a specific test case, the setup in the setting is executed without executing the test setup in the

3.3.1.5, Test Teardown

Each test case is executed at the end of the action, using the same keyword as in the test cases. The teardown in the same case will overwrite the setting test teardown.

3.3.1.6, Test Timeout

Use case timeout time, the use case that exceeds the time that has not been completed is forced to exit. Units in seconds, can also be written as follows, the time entered with the ride automatically will be converted to the following form

3.3.1.7, Tags

tags, as a use case tag, are very common in robot. force tags and default tags can be configured in setting, and the role of Tag,tag can be configured in the use case described later

3.3.1.8, Library

Specify the Testlib, the system comes with or already installed Testlib can enter the name directly, the testlib you write will need to join the path, relative path absolute path can be, relative to the path of the HTML file

3.3.1.9, Resource

Introduce a resource file. Usually a good robot use case contains keywords related to the use case, use cases and some basic configuration, etc., if you need to use a lot of public modules, public keywords, these public resources will usually be edited into a separate HTML, so that business-related HTML can reference

3.3.2, testcases

This section begins by describing how to write test cases.

3.3.2.1, writing methods

Is the style of the test case table described in the official help document. The first column is the use case name, and the second example begins with the use case action. If the use case has more than one action, then step 2 moves to the bottom of step 1, that is, the first column of step 2 is empty, the following example

Case One Keyword A Arg1 Arg2
Keyword B Arg1 Arg2 Arg3
Case Keyword C
Keyword D Arg1 Arg2

The two case has 2 steps, and the robot identifies it as if the first column is non-empty and the row is considered a new one.

The table above is missing the table header, the official style header is the first column is the test case, the second column is the action, the third column is argument, in fact not all cases of the second case is an action, it is possible to return a value, Some keyword may also return 2 return values or more, and may take up more space before the action. The ride edit generated use Case table header is only Test Cases. So the general use case is usually in the form of:

Use case name return value = Key words Parameter 1 Parameter 2
3.3.3.2, variables

When you write a test script, each step is not independent. The output from the previous step is often required to pass in the input to the latter step. You can use variables to pass these values in your code, but if you're writing test cases in a table, it's not that easy to pass parameters. Robot provides such a variable mechanism to easily pass parameters in writing use cases.

A variable is defined in the form of a ${variable name}, which defines the variable, assigns a value to the variable, and passes in as a parameter. For example, define a test case named Var, and have two keywords the first is get Var, there are 1 return values, no input parameters, the function gets the value of var. The other keyword is set var, with 1 input parameters, no return value, and a function to set the VAR value. The steps of the Var case first Call get Var to get Var and then call set Var and pass in the value. As follows:

var case ${test_var}= Get Var
Set Var ${test_var}

The ${test_var} is defined in the use case to take a value and assign a value. If a copy is required as the return value, it is written before the keyword operation and added =, and if the value is written as a parameter in the keyword operation, the parameters need to be aware of the order.

By learning to use variables and knowing how to use them, you can write test cases basically, and here are some common configurations in test case. The configuration in case needs to be marked with "[]"

3.3.2.3, Documentition

As in setting, used to make some comments, descriptions, etc., which will be printed when robot executes.

3.3.2.4, Setup

The action to be performed first when the case starts, and if Setup is defined here, the test setup in setting will be invalidated.

3.3.2.5, Teardown

When the case ends, the test teardown in setting will be invalidated if teardown is defined here.

3.3.2.6, Timeout

Use case time-out.

3.3.2.7, Tags

Consistent with tags in setting, used to mark use cases.

3.3.3, Keyword

Robot as a keyword-driven test framework, keyword is the core content of its use-case layer. A function defined in testlib that can be used as a keyword when editing a use case. Users can customize the keyword at the same time, and user-defined keyword is usually the combination and encapsulation of keyword in the underlying testlib. The function of keyword in testlib is the simplest single. If the test cases all use keyword in testlib it is possible that a use case contains more than 10 steps or more. Such use cases are poorly logical and readable. People who read a use case may only care about what a step does and don't care how it is done, such as the next step in data processing is to verify the results. Validation results may require processing of databases, writing files, parsing data structures, and so on. These things should be written on top of the use case people may not care, or do not want to care. A good keyword package makes it easier to write use cases and the higher the quality of use cases

3.3.3.1, keyword writing method

The officially defined keyword table style is as follows

And the test case is defined in the same way. In Ride, you can create a keyword as follows. Right-click Robot test to select New User Keyword and name my Keyword.

The example in 2.1 is used here to call two first keyword in my keyword

The saved HTML style is

Then edit the first case to have its test step call my keyword

The editor finishes executing the first case and then looks at the Robot_fk file under/tmp/, adding two lines of "This is the first keyword" each time it executes.

It is important to note that user-defined keyword cannot be duplicated with keyword in testlib. Since robot first went back to Testlib to search for keyword when executing the use case, the keyword would not be executed if the user defined keyword that was duplicated in Testlib.

Input and output of 3.3.3.2, keyword

If you use a keyword as a function, in addition to the function processing flow, the user is most concerned about the input and output. The input is the parameter, and the output is the return value of keyword. Here is an example of the input and output of a keyword. First add two methods to the robottest.py used in 2.1, the first method is named Plus, the input parameter is 2, the function is to return two parameters of the and. The other method, named Multi, has an input parameter of 3, and the function returns the product of 3 parameters. Define a keyword named ALG keyword, with 3 input parameters, 1 return values, with a function of three parameter 22 summation, 3 and a product and return. Add the following code to the robottest.py

[Python]View Plaincopy
  1. Import OS
  2. Import Sys
  3. Class Robottest:
  4. def first_keyword (self):
  5. fp = open ("/TMP/ROBOT_FK", "a")
  6. Fp.write ("This is the firstkeyword\r\n")
  7. Fp.close ()
  8. def plus (self, arg_a, Arg_b):
  9. return int (arg_a) + int (arg_b)
  10. def multi (self, arg_a, Arg_b, Arg_c):
  11. return int (arg_a) * INT (arg_b) * INT (arg_c)

Create a new keyword named ALG keyword in ride. Use the [Arguments] declaration parameter as shown below.

Edit parameters in ride in the same way as

Then edit the test steps in the ALG keyword, as follows

After saving the HTML page as

Here ride in order to limit the number of table columns when generating an HTML page, ${c} will be used in a row and before ... Indicates that this is a connection to the previous row. Finally add the value of [Return] return ${d}

Add the return value in ride in return, and the full keyword edit as follows

After completing the keyword, add a test case in ride named second, call the ALG keyword and pass in the parameter, and return the value to print

After editing, tick second case and click on the button to execute.

Execution results are as follows

The above results show that the ALG keyword is used correctly in the second case and returns the correct value. This function allows you to encapsulate a single keyword into a high-level keyword. Keyword also has other common configurations

3.3.3.3, Documentation

As in setting, used to make some comments, descriptions and so on.

3.3.3.4, Teardown

The last action the keyword performs when it finishes. The keyword will terminate if it encounters an error during execution, and if there are resources released in subsequent operations, these operations will not execute, may cause system crashes, memory leaks, etc., so you can add a resource release operation to the teardown.

3.3.4, Variable

Robot can define variables independently of the case, which can set default values. This is more like a global variable in the code. When there are some public configurations, it can be used in every test case, such as IP address, port number, default user name and password, and so on. It is recommended to use all uppercase letters when defining global variables. In the 3.3.3 example, the second case uses the ALG keyword, which can be replaced with a global variable.

First create a global variable in ride, right-click Robot test in Ride to select New Scalar, then name the variable and set the default

The same method creates two more global variables, and you can view the HTML table when you are done.

After modifying the second force, the three parameters are replaced by the original three-in-one into the use case

Then click Run to view the results.

4. Robot Advanced Application

The majority of test cases can be completed with the above robot application, and robot provides more powerful keywords to make use case writing more flexible

4.1. If branch

Robot supports an if decision to perform different actions depending on the situation when the use case executes. If-related keywords are "Run Keyword if", "else if", "Else"

Using the PLUS keyword created in 3.3.3, create a use case call plus input two parameters, if the returned value is less than 0, then print Loga, greater than 0, less than 10 printing logb, greater than or equal to 10 printing logc. First, add three functions in robottest.py to print LOGA,LOGB,LOGC separately, call logger in robot to import the Robot API package

And then create a new use case named third case after editing use cases, as follows

You can then try to modify the plus parameters to test the results.

If there is a relatively special place, if you determine whether a variable is a string, you can not directly use ${ret} to judge, you need to give ${ret} quotes as follows:

Runkeyword If ' ${ret} ' = = ' abc ' DoSomething Arg1

If you do not quote, use ${ret} = = ' abc ', assuming that the value of ${ret} is qwe, execution will error, tell you qwe this variable is not defined. In this case, robot the value of the variable as the variable name in the code, so it needs to be quoted when the string is processed. This is the power of Python, it can be converted to a variable name by a string.

There are many techniques for using the IF, see the official Help document in detail

http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.8.1

4.2. Circulation

Robot can use a for loop to perform some operations over and over, and it is common practice that if an operation returns a list, and the next step is to process each element of the table separately, the following actions are required:

${table_list}= DoSomething
: For ${table} Inch @{table_list}
Dofirst Step ${table}
Dosecond Step Arg2

Use: For and in to iterate through the elements in the list, but note that you need to convert the variable through @ to a list when using for traversal, otherwise it will not traverse correctly. The loop is terminated by the keyword "Exit for loop" during traversal. The For loop also supports a variety of ways, such as in range to set the number of traversal times, or to traverse directly within a few items, in detail to see the Help document for loop section

Http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.8.1#for-loops

4.3. Tags

Previously mentioned the concept of tags, is used to mark the use case. You can set force tags and default tags in setting, or you can use [tags] in a use case to mark a use case. The most direct application of tags is that you have the option of executing test cases. For example, there are 10 use cases, of which 5 use cases are labeled group_a with [tags], and 5 are marked as group_b. When using Pybot to execute a use case, if the parameter--includegroup_a then robot only executes the use case with group_a tag, if--excludegroup_a then robot the use case does not execute, others execute, And a use case can add multiple tags. In the version test, you can add tags to the use case, such as a product has 100 use cases, the 1.0 version needs to test the use case to add 1.0 of the tags,1.1 version needs to test cases plus 1.1 tags, so in the execution of the use case can be used to differentiate version by tags, very convenient.

You can set the default tags and force tags in setting. With these two configurations, you can not add tags to each use case separately, and using these configurations will add tags to your use case, see

Http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.8.1#tagging-test-cases

The links above describe how the force Tags,default tags and the use cases themselves of tags coexist.

4.4, Pybot

Pybot is the robot of the execution of the program, in fact ride implementation of the use case is also the use of Pybot, so it is important to understand how pybot is used.

The simplest use case execution is Pybot xxx, where xxx is the robot resource file. Just like the quickstart.html in 1.2.3. Use this command to perform tests. The Help manual can be seen through pybot--help. Commonly used such as--include,--exclude,--argumentfile,--listener,--variable, can also set the path of the log output, and so on users need to see Help

Transferred from: http://blog.csdn.net/yydoraemon/article/details/13847781

2-hour introductory robot Framework

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.