rf介绍
TRANSCRIPT
An introduction to
Robot Framework 李亚飞/louieli 00185421
2012.09.06 v2.0
目录
RF是什么
RF特性介绍
DEMO
好的测试框架应具备
1.易用 2.稳定 3.方便扩展 4.可以很好的组织用例 5.测试执行 6.灵活性 7.调试 8.日志 9.报告 10.框架支持工具
RF是什么
● Generic test automation framework – Utilizes the keyword-driven testing approach
● Implemented with Python – Jython supported fully and IronPython initially – Can be extended natively using Python or Java – Other languages supported via XML-RPC based remote interface
● Open source – Apache 2.0 license – Sponsored by Nokia Siemens Networks – Active development and growing community – Hosted on Google Code
RF框架
RF特性—测试用例组成
Table name Used for Aliases
Setting table 1) Importing test libraries, resource files and variable files 2) Defining metadata for test suites and test cases
Setting, Settings, Metadata
Variable table Defining variables that can be used elsewhere in the test data
Variable, Variables
Test case table Creating test cases from available keywords Test Case, Test Cases
Keyword table Creating user keywords from existing lower-level keywords
Keyword, Keywords, User Keyword, User Keywords
RF特性—简单易用的表格语法 Test Case Action Argument Argument
Valid Login Open Login Page
Input Name demo
Input Password mode
Submit Credentials
Welcome Page Should
Be Open
Setting Variables Do Something first
argument
second argument
${value} = Get Some
Value
Should Be Equal ${value} Expected value
RF特性—支持多种文件格式
Test Case Action Argument Argument
My Test [Documentation] Example test
Log ${MESSAGE}
My Keyword /tmp
Another Test Should Be Equal ${MESSAGE} Hello, world!
html格式
RF特性—支持多种文件格式
TSV 格式
*Setting* *Value* *Value* *Value*
Library OperatingSystem
*Variable* *Value* *Value* *Value*
${MESSAGE} Hello, world!
*Test Case* *Action* *Argument* *Argument*
My Test [Documentation] Example test
Log ${MESSAGE}
My Keyword /tmp
Another Test Should Be Equal ${MESSAGE} Hello, world!
*Keyword* *Action* *Argument* *Argument*
My Keyword [Arguments] ${path}
Directory Should
Exist
${path}
RF特性—支持多种文件格式
文本格式
RF特性—支持多种文件格式
reStructuredText format
可以方便的用python读取,不常用
RF特性—支持多种用例风格
Keyword-driven style
前面已经讲过了
RF特性—支持多种用例风格
Data-driven style
Test Case User Name Password
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}
RF特性—支持多种用例风格
Data-driven style
RF特性—支持多种用例风格
Behavior-driven style
Test Case Step
Valid Login Given login page is open
When valid username and
password are inserted
and credentials are
submitted
Then welcome page should
be open
RF特性—灵活的用例组织
目录是Test Suite
文件是Test Suite
文件中Test Cases部分才是真正的 Test Case
RF特性—灵活的用例组织
Initialization files
RF特性—Variables
1.用例中的测试数据经常变动 2.用例需要跨平台、跨浏览器 3.需要更复杂的对像,而不仅仅是数字或字符串 4.不同关键字之间、测试库之间、用例之间的通信 5.用例中需要操作的值很长很复杂
RF特性—Variables
Variable types Scalar variables ${NAME} ${OBJ}
List variables @{USER} @{USER}[0] @{items}
Using list variables as scalars Test Case Action Argument Argument Argument
Example @{items} = Create List first second
Length Should Be ${items} 2
Append To List ${items} third
Length Should Be ${items} 3
Log Many @{items}
Environment variables %{USER} %{JAVA_HOME}
RF特性—Variables
Setting variables in command line
--variable EXAMPLE:value --variable HOST:localhost:7272 --variable USER:robot --variable ESCAPED:Qquotes_and_spacesQ --escape quot:Q --escape space:_
Built-in variables
Variable Explanation
${CURDIR} An absolute path to the directory where the test data file is located. This variable is case-sensitive.
${TEMPDIR} An absolute path to the system temporary directory. In UNIX-like systems this is typically /tmp, and in Windows c:\Documents and Settings\<user>\Local Settings\Temp.
${EXECDIR} An absolute path to the directory where test execution was started from. New in Robot Framework 2.1.
${/} The system directory path separator. / in UNIX-like systems, \ in Windows.
${:} The system path element separator. : in UNIX-like systems and ; in Windows.
RF特性—Variables
Variable Priorities
1.Variables from the command line 2.Variable table in a test case file 3.Imported resource and variable files 4.Variables set during test execution 5.Built-in variables
RF特性—Variables
Variable Scopes
1. Global scope 2. Test suite scope 3. Test case scope 4. User keyword scope
1. set from the command line 2. BuiltIn keyword Set Global Variable 3. Built-in variables 推荐大写
1. created in Variable tables 2. imported from resource and variable files 3. BuiltIn keyword Set Suite Variable 4. test suite scope is not recursive 推荐大写
1. from the return values of keywords 2. BuiltIn keyword Set Test Variable 推荐小写
User keywords get their own variables from arguments passed to them and return values from the keywords they use 推荐小写
RF特性—自定义关键字
RF特性—Resource and Variable files
通过Resource files 可以很方便的共享测试设置、公共变量、用户自定义关键字。
Variable files 提供了更强大的机制,复杂变量的创建变得如此简单。Variable files被视为python 代码,所以想创建什么就创建什么。
RF特性—Resource files
Importing resource files
Setting Value Value
Resource myresources.html
Resource ../data/resources.html
Resource ${RESOURCES}/common.tsv
Resource ../common_resource.txt
RF特性—Resource files
RF特性—Variable files
Setting Value Value Value
Variables myvariables.py
Variables ../data/variables.py
Variables ${RESOURCES}/common.py
Variables taking_arguments.py arg1 ${ARG2}
Importing a variable file
Command line
--variablefile myvariables.py --variablefile path/variables.py --variablefile /absolute/path/common.py --variablefile taking_arguments.py:arg1:arg2
RF特性—Variable files
导入这段代码就会得到AREA1和AREA2两个变量
RF特性—Variable files
Extended variable syntax
RF特性—Variable files
1.搜索全名变量(OBJECT.name),若没找到则启用扩展变量语法 2.创建一个基本变量${OJBECT} 3.搜索变量OBJECT,若没找到,抛异常,用例失败 4.大括号中的内容作为python代码执行,若执行出错, 抛异常,用例失败 5.设置扩展变量的值python代码的执行结果
Extended variable syntax
RF特性—Variable files
Extended variable assignment
RF特性—Variable files
1.要赋值的变量(${OBJECT.age})必须是标量,变量名字必须包含至少一个点。 否则按普通赋值处理 2.如果要赋值的变量(${OBJECT.age})已经存在,用新值覆盖旧值 3.创建基本变量${OBJECT} 4.创建属性变量name 5.搜索变量OBJECT,若没找到,${OBJECT.age}按普通赋值处理 6.如果找到的OBJECT变量是string或number,扩展语法不启用, ${OBJECT.age}按普通赋值处理 7.如果以上6步全部满足,设置属性变量。如果设置失败,抛异常,用例失败
Extended variable assignment
RF特性—循环
Test Case Action Argument Argument Argument Arguments
Example 1 :FOR ${animal} IN cat dog
Log ${animal}
Log 2nd
keyword
Log Outside
loop
Example 2 :FOR ${var} IN one two
... three four five six
... seven
Log ${var}
RF特性—循环
Test Case Action Argument Argument Argument Arguments
Example :FOR ${element} IN @{ELEMENTS}
Start Element ${element}
Test Case Action Argument Argument Argument
Exit Example :FOR ${var} IN @{SOME LIST}
Run Keyword If '${var}' == 'EXIT' Exit For Loop
Do Something ${var}
Test Case Action Argument Argument Argument Argument
Example Repeat Keyword 5 Some Keyword arg1 arg2
Repeat Keyword 42 times My Keyword
Repeat Keyword ${var} Another KW argument
RF特性—测试执行
pybot [options] datasources jybot [options] datasources interpreter /path/to/robot/runner.py [options] datasources java -jar robotframework-<version>.jar [options] datasources jybot --include smoke --variable HOST:10.0.0.42 path/to/tests/ pybot --variable BROWSER:IE --name IE --log none --report none --output out/ie.xml login pybot --argumentfile default_options.txt --name example my_tests.html
pybot --argumentfile default_options.txt --name example my_tests.html
RF特性—测试执行
--test Example --test mytest --test yourtest --test example* --test mysuite.mytest --test *.suite.mytest --suite example-?? --suite mysuite --test mytest --test your* --suite parent.child --suite myhouse.myhousemusic --test jack* --include example --exclude not_ready --include regression --exclude long_lasting
pybot --argumentfile default_options.txt --name example my_tests.html
RF特性—测试执行
--xunitfile 生成Xunit compatiable result file 加时间戳 pybot --timestampoutputs --log mylog.html --report NONE tests.html 设置title pybot --logtitle Smoke_Test_Log --reporttitle Smoke_Test_Report --include smoke my_tests/ 设置颜色 --reportbackground blue:red --reportbackground green:yellow:red --reportbackground #00E:#E00
pybot --argumentfile default_options.txt --name example my_tests.html
RF特性—RIDE
pybot --argumentfile default_options.txt --name example my_tests.html
RF特性—丰富的测试库
pybot --argumentfile default_options.txt --name example my_tests.html
RF特性—创建自己的测试库
pybot --argumentfile default_options.txt --name example my_tests.html
Python Java
Remote library Any languages
Static API Dynamic API
Hybrid API
RF特性—创建自己的测试库
pybot --argumentfile default_options.txt --name example my_tests.html
RF特性—创建自己的测试库
pybot --argumentfile default_options.txt --name example my_tests.html
RF特性—创建自己的测试库
pybot --argumentfile default_options.txt --name example my_tests.html
Setting Value Value Value Value
Library Remote http://localhost:8270 WITH NAME Example1
Library Remote http://10.0.0.42:7777 WITH NAME Example2
RF特性—Listener Interface
pybot --argumentfile default_options.txt --name example my_tests.html
RF特性—活跃的社区
pybot --argumentfile default_options.txt --name example my_tests.html
DEMO
pybot --argumentfile default_options.txt --name example my_tests.html
For more information
pybot --argumentfile default_options.txt --name example my_tests.html
http://code.google.com/p/robotframework/ http://robotframework.googlecode.com/hg/doc/quickstart/quickstart.html http://www.virtuousprogrammer.com/?p=264 http://code.google.com/p/robotframework/wiki/UserGuide http://code.google.com/p/robotframework/wiki/PythonTutorial