RobotFramework--搭建和语法

Robot Framework

Robot Framework 是一种基于 Python可扩展 关键字驱动 开源自动化测试框架,运行在Jython (JVM) and IronPython (.NET)。主要用作验收测试和验收驱动开发。

  • RF关键字包括:内置库关键字,和其他支持的测试库的关键字组成。
  • 工作原理:RF通过关键字(方法名)找到对应的库,然后由这个测试库(方法的具体实现)去操作被测系统。

一步步安装Robot Framework

1. 安装python

If you have Python 2 >=2.7.9 or Python 3 >=3.4 installed from python.org, you will already have pip and setuptools, but will need to upgrade to the latest version.

  • pip , setuptools ,都是 python 包管理工具。
# 升级 pip , setuptools
python -m pip install -U pip setuptools
  • 追加path环境变量:C:\Python27;C:\Python27\Scripts(包含 pip.py, ride.py 等命令)
  • 通过 pip 和 setuptools 命令安装的软件包在: C:\Python27\ Lib\site-packages
2. 安装各种robot软件包
pip install robotframework

# 安装 RF 的集成环境,RIDE,对应 Python27\Scripts\ride.py
pip install robotframework-ride

# 安装 RF 支持的 selenium 库: Selenium2Library
pip install robotframework-selenium2library

# 下载cx_Oracle
# pip install robotframework-databaselibrary     

# 安装 requests
pip install requests              
pip install -U robotframework-requests

# 显示所有安装的软件
pip list                        

Test Cases

*** Test Cases ***
1.1 Workflow tests

工作流测试,即按照工作流程写测试用例。

  • The syntax requires that keywords and arguments. It is generally recommended to use four spaces.
1.2. Higher-level tests

One common style is the given-when-then format popularized by behavior-driven development (BDD)

1.3. Data-driven tests

如果测试场景很相似只是输入或者输出数据不一样。这种通常使用数据驱动方式测试避免重复的工作流。例如表单提交。

  • With Robot Framework the [Template] setting turns a test case into a data-driven test where the template keyword is executed using the data defined in the test case body.
  • 用法: [Template] func关键字,每一条数据就是传递给func函数的实际参数。
  • Test Template: 作用于测试用例集(在测试用例集中设置)
  • [Template]: 作用于单个用例(在单个用例中设置)

2. Keywords

2.1. Library keywords

主要分为三类: standard libraries, external libraries and custom libraries.

  • Standard libraries: 例如 OperatingSystem, Screenshot and BuiltIn(可直接用)
  • External libraries: 例如 Selenium2Library(需要下载导入后才可用)
  • BuiltIn常用的关键字例如:Should Contain(作断言)
2.2 User keywords

用户定义的关键字(由其他关键字组合而成)

  • 右键:New User Keyword
  • Arguments参数: 由管道符分隔例如,Login ${username} | ${password}
*** Keywords ***
Login arg1 arg2

3. Variables

  • 右键: New List Variable, @{username}新建变量。

Variables can also be given from the command line which is useful if the tests need to be executed in different environments. For example this demo can be executed like:

robot --variable USERNAME:johndoe --variable PASSWORD:J0hnD0e QuickStart.rst
*** Variables ***
${username} ma

4. Organizing test cases

  • Test Suites: File-->New Project-->Type选Directory或者File
4.1 Setup/Teardown

钩子在测试用例集和单个测试用例中均可设置

  • Suite Setup / Suite Teardown,作用于测试用例集(在用例集上设置)
  • Test Setup / Test Teardown,作用于每个测试用例(在用例集上设置)
  • Setup / Teardown,只作用于当前测试用例(在单个测试用例中设置)
*** Settings ***
Test Setup        Login
Test Teardown Close Browser
4.2 Tags
  • Force Tags,对所有的测试用例有效(在用例集上设置)
  • Default Tags,对所有的测试用例有效(在用例集上设置)
  • Tags,对单个测试用例有效,可以编辑选择自定义的tag,可以编辑移除Default Tags,不可移除Force Tags(在单个测试用例中设置)
robot --include tag1 testsuite1     # 执行测试用例集中标记为tag1的所有用例
robot --exclude tag1 testsuite1     # 不执行测试用例集中标记为tag1的所有其他用例

5. Creating test libraries

自定义第三方测试库: Robot Framework offers a simple API for creating test libraries using either Python or Java, and the remote library interface also allows using other programming languages.

  • 常用测试库的详细API使用说明: http://robotframework.org/robotframework/#user-guide

你可能感兴趣的:(RobotFramework--搭建和语法)