RobotFramework官方demo Quick Start Guide rst配置文件分析
by:授客 QQ:1033553122
博客:http://blog.sina.com.cn/ishouke
欢迎加入软件性能测试交流QQ群:7156436
7、 启动和结束(Setup and Teardown) 6
1、 Demo下载
下载地址:demo for robotframework rst配置文件分析
2、 QuickStart.rsc文件
.. code:: robotframework
*** Test Cases ***
User can create an account and log in
[Tags] mytag
Create Valid User fred P4ssw0rd
Attempt to Login with Credentials fred P4ssw0rd
Status Should Be Logged In
Invalid password
[Template] Creating user with invalid password should fail
abCD5 ${PWD INVALID LENGTH}
abCD567890123 ${PWD INVALID LENGTH}
123DEFG ${PWD INVALID CONTENT}
abcd56789 ${PWD INVALID CONTENT}
AbCdEfGh ${PWD INVALID CONTENT}
abCD56+ ${PWD INVALID CONTENT}
User can change password
Given A user has a valid account
When she changes her password
Then she can log in with the new password
And she cannot use the old password anymore
Clear Login Database finally
Clear Login Database
*** Keywords ***
Create valid user
[Arguments] ${username} ${password}
Create user ${username} ${password}
Status should be SUCCESS
Creating user with invalid password should fail
[Arguments] ${password} ${error}
Create user example ${password}
Status should be Creating user failed: ${ERROR}
Clear Login Database
Remove file ${DATABASE FILE}
A user has a valid account
Create valid user ${USERNAME} ${PASSWORD}
She changes her password
Change password ${USERNAME} ${PASSWORD} ${NEW PASSWORD}
Status should be SUCCESS
She can log in with the new password
Login ${USERNAME} ${NEW PASSWORD}
She cannot use the old password anymore
Attempt to login with credentials ${USERNAME} ${PASSWORD}
Status should be Access Denied
*** Variables ***
${USERNAME} janedoe
${PASSWORD} J4n3D0e
${error} 123456
${NEW PASSWORD} e0D3n4J
${DATABASE FILE} ${TEMPDIR}${/}robotframework-quickstart-db.txt
${PWD INVALID LENGTH} Password must be 7-12 characters long
${PWD INVALID CONTENT} Password must be a combination of lowercase and uppercase letters and numbers
${ERROR} Creating user failed: Password must be 7-12 characters long
*** Settings ***
Suite Setup Clear Login Database
Test Teardown Clear Login Database
Force Tags quickstart
Default Tags example smoke
Library OperatingSystem
Library Lib/LoginLibrary.py
__ `Creating test libraries`_
3、 测试用例
User can create an account and log in
*** Test Cases ***
User can create an account and log in
[Tags] mytag
Create Valid User fred P4ssw0rd
Attempt to Login with Credentials fred P4ssw0rd
Status Should Be Logged In
用例由关键词及对应参数,设置和对应值组成。关键词和对应参数之间(比如关键词Create Valid User 和参数fred),参数与参数之间(fred和P4ssw0rd),设置和对应值之间(比如Tags和mytag)至少需要2个空格,或1个tab键分隔开,建议使用空格分隔。
行为驱动测试
使用行为驱动风格的测试用例(given-when-then)
*** Test Cases ***
User can change password
Given A user has a valid account
When she changes her password
Then she can log in with the new password
And she cannot use the old password anymore
关键词(比如A user has a valid account)不带位置参数
数据驱动测试
通过[Template]设置把用例转为数据驱动,运行时,会提取case body下方的参数(如例中的abCD5 ${PWD INVALID LENGTH}),重复执行模版关键字(比如Creating user with invalid password should fail)
*** Test Cases ***
Invalid password
[Template] Creating user with invalid password should fail
abCD5 ${PWD INVALID LENGTH}
abCD567890123 ${PWD INVALID LENGTH}
123DEFG ${PWD INVALID CONTENT}
abcd56789 ${PWD INVALID CONTENT}
AbCdEfGh ${PWD INVALID CONTENT}
abCD56+ ${PWD INVALID CONTENT}
4、 关键字
关键字有两种:
类库关键字(Library keywords):来自引入的测试类库
用户关键字(user keywords):为构造测试用例使用表格语法(tabular syntax)创建的
注意:大小写除外,关键字要完全匹配,比如关键词单词之间不能多一个空格,少一个空格
1、类库关键词
测试类库(test libraries)可分成成标准类库(stand libraries),外部类库(external library)
还有自定义类库。标准类库随core framework一起发布,包括通用类库,如OperatingSystem,Screenshot 和Builtln。外部类库,比如Selenium2Library,必须独立安装。如果这些还不能满足需求,则自定义类库,比如本例中的LoginLibrary.py。
为了能使用测试类库提供的关键词,必须使用Library设置导入关键字。例中Remove File来自标准类库OperatingSystem。其它一些关键词,如Attempt to login with credentials来自自定义类库 LoginLibrary。
*** Settings ***
Library OperatingSystem
Library Lib/LoginLibrary.py
__ `Creating test libraries`_
注意:__ `Creating test libraries`_ 不能少,否则不会创建测试类库。
2、用户关键字
Robot Framework的一个强大功能就是可以通过其它关键字创建更高层级的关键词。
*** Keywords ***
Create valid user
[Arguments] ${username} ${password}
Create user ${username} ${password}
Status should be SUCCESS
Creating user with invalid password should fail
[Arguments] ${password} ${error}
Create user example ${password}
Status should be Creating user failed: ${ERROR}
Clear Login Database
Remove file ${DATABASE FILE}
# comment 以下是通过关键字创建的,供一些用例使用的更高层级关键字
A user has a valid account
Create valid user ${USERNAME} ${PASSWORD}
She changes her password
Change password ${USERNAME} ${PASSWORD} ${NEW PASSWORD}
Status should be SUCCESS
She can log in with the new password
Login ${USERNAME} ${NEW PASSWORD}
She cannot use the old password anymore
Attempt to login with credentials ${USERNAME} ${PASSWORD}
Status should be Access Denied
如上,关键字也可以携带参数。关键字还可以有返回值,甚至是FOR循环
5、 变量
变量定义
*** Variables ***
${USERNAME} janedoe
${PASSWORD} J4n3D0e
${error} 123456
${NEW PASSWORD} e0D3n4J
${DATABASE FILE} ${TEMPDIR}${/}robotframework-quickstart-db.txt
${PWD INVALID LENGTH} Password must be 7-12 characters long
${PWD INVALID CONTENT} Password must be a combination of lowercase and uppercase letters and numbers
${ERROR} Creating user failed: Password must be 7-12 characters long
变量也可以通过命令行输入,特别是需要在不同环境执行的时候,这很有用。例如
robot --variable USERNAME:johndoe --variable PASSWORD:J0hnD0e QuickStart.rst
除了一些自定义变量,有一些内置变量也总是可用的。这些变量包括上述使例中使用的${TEMPDIR}和${/}
变量的使用
变量可在大部分测试数据中使用。如下,最常用于关键词的参数。从关键词返回的数据可以赋值给变量,并在后续使用。例如,以下的Database Should Contain 用户关键词设置数据库内容为${database}变量,然后确内置关键词(BuiltIn keyword) Should Contain确认。类库关键词和用户关键词都可以返回值。
*** Test Cases ***
User status is stored in database
[Tags] variables database
Create Valid User ${USERNAME} ${PASSWORD}
Database Should Contain ${USERNAME} ${PASSWORD} Inactive
Login ${USERNAME} ${PASSWORD}
Database Should Contain ${USERNAME} ${PASSWORD} Active
*** Keywords ***
Database Should Contain
[Arguments] ${username} ${password} ${status}
${database} = Get File ${DATABASE FILE}
Should Contain ${database} ${username}\t${password}\t${status}\n
6、 组织测试用例
测试用例的集合,称为套件。每个包含测试用例的输入文件形成一个测试套件。执行 QuickStart.rst时,可在控制台输出看到测试套件 QuickStart 。该名字是从文件名继承的,在报告和logs中也可见。
可把测试用例文件放入某些目录,然后把这些文件目录在放入到其它目录中,即可嵌套。所有这些目录自动形参更高层级的测试套件,套件名字来源于目录名字。
运行执行套件
把.rst,html等配置文件放到套件所在目录下,然后运行命令 robot -s suite_name path_to_dir,如:
E:\Projects\studyproject>robot -s suite ./suite
7、 启动和结束(Setup and Teardown)
如果想让某些关键字在每个测试执行前、执行后都被执行,可在setting表中使用Test Setup和Test Teardown设置。类似的,可以使用Suite Setup和Suite Teardown设置来指整个套件执行前、执行后都需要运行的关键字。
单个用例也可在Test Cases 用例表中使用[Setup]和[Teardown]来自定义setup或teardown。方法同[Template]的使用。
*** Settings ***
Suite Setup Clear Login Database
Test Teardown Clear Login Database
8、 使用标签
Rf允许为测试用例使用标签,以给它们自由元数据。如下setting表,可用[Force Tags]和[Default Tags]为文件中的所有测试用例设置Tags。也可以用[Tags]为单个测试用例定义Tags。
*** Settings ***
Force Tags quickstart
Default Tags example smoke
Tags的一个重要应用是可选择需要执行的测试。例如
robot --include mytag QuickStart.rst #只执行携带mytag标签的用例
robot --exclude mytag QuickStart.rst #只执行不携带mytag标签的用例
注意:如果为单个用例指定了[Tags],那么该用例将无法拥有Default Tags,否则可拥有,而Force Tags则强制为所有用例设置Tags,即便用例已指定[Tags]也不影响。以上为例子,用例User can create an account and log in只包含quickstart和mytag
9、 创建测试类库
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os.path
import subprocess
import sys
class LoginLibrary(object):
def __init__(self):
self._sut_path = os.path.join(os.path.dirname(__file__),
'..', 'sut', 'login.py')
self._status = ''
def create_user(self, username, password):
self._run_command('create', username, password)
def change_password(self, username, old_pwd, new_pwd):
self._run_command('change-password', username, old_pwd, new_pwd)
def attempt_to_login_with_credentials(self, username, password):
self._run_command('login', username, password)
def login(self, username, password):
self._run_command('login', username, password)
def status_should_be(self, expected_status):
if expected_status != self._status:
raise AssertionError("Expected status to be '%s' but was '%s'."
% (expected_status, self._status))
def truncate(self):
self._run_command('truncatefile')
def _run_command(self, command, *args):
command = [sys.executable, self._sut_path, command] + list(args)
process = subprocess.Popen(command, universal_newlines=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
self._status = process.communicate()[0].strip()
参考连接:
https://github.com/robotframework/QuickStartGuide/blob/master/QuickStart.rst