Robot Framework 源代码阅读笔记 之二

上次走到了具体测试执行的地方,感觉缺乏一个全局观,有点走不下去。


还是再回头看看整个设计思路,所有的模块文档都可以从这里访问到:

使用文档:

http://robotframework.org/robotframework/

接口文档:

http://robot-framework.readthedocs.io/en/3.0.2/autodoc/robot.html


在看这些keywords之前先记录一下对为什么要用keywords这种方式而不是传统的编程方式的思考:

1. 有大师曰过,所有的编程抽象,本质上都是DSL,也就是最后会变成一种领域专用语言,任何我们调用的库都是。所以keywords本质上也是一种DSL。

2. 作为测试领域的DSL,好的keywords设计可以极大的降低测试的编写门槛和开发效率

3. 内置的Keywords应该提供一种测试逻辑完备性,包括各种判断和循环

4. kewords使用上由于是基于英文设计的,应该更符合英语使用者的思维,如果有相应的中文版本那就可以更方便中文使用者,当然可以自己开发中文版


Robotframework自带的默认keywords和库如下,如果要熟练使用,最好都能仔细看看,尤其是builtin的。

BuiltIn
Contains generic often needed keywords. Imported automatically and thus always available.
Collections
Contains keywords for handling lists and dictionaries.
DateTime
Supports creating and verifying date and time values as well as calculations between them.
Dialogs
Supports pausing the test execution and getting input from users.
OperatingSystem
Enables performing various operating system related tasks.
Process
Supports executing processes in the system.
Remote
Part of the remote library interface. Does not have any keywords of its own.
Screenshot
Provides keywords to capture and store screenshots of the desktop.
String
Library for manipulating strings and verifying their contents.
Telnet
Supports connecting to Telnet servers and executing commands on the opened connections.
XML
Library for verifying and modifying XML documents.


Builtin的keywords可以和应该能够解决我们平时常用的测试逻辑,如果不行还能够保持原生python语言的灵活性。用这个思路,看看大概有哪些。

首先提示了,大部分关键字都提供了对错误信息的支持,而且可以是HTLML格式的,目的应该是可以让错误信息更醒目。然后说有些keywords是可以支持python的语句,会用eval执行。内置的keywords如下:

Call Method · Catenate · Comment · Continue For Loop · Continue For Loop If · Convert To Binary · Convert To Boolean · Convert To Bytes · Convert To Hex · Convert To Integer · Convert To Number · Convert To Octal · Convert To String · Create Dictionary · Create List · Evaluate · Exit For Loop· Exit For Loop If · Fail · Fatal Error · Get Count · Get Length · Get Library Instance · Get Time · Get Variable Value · Get Variables · Import Library · Import Resource · Import Variables · Keyword Should Exist · Length Should Be · Log · Log Many · Log To Console · Log Variables · No Operation· Pass Execution · Pass Execution If · Regexp Escape · Reload Library · Remove Tags · Repeat Keyword · Replace Variables · Return From Keyword · Return From Keyword If · Run Keyword · Run Keyword And Continue On Failure · Run Keyword And Expect Error ·Run Keyword And Ignore Error · Run Keyword And Return · Run Keyword And Return If · Run Keyword And Return Status · Run Keyword If · Run Keyword If All Critical Tests Passed · Run Keyword If All Tests Passed · Run Keyword If Any Critical Tests Failed · Run Keyword If Any Tests Failed ·Run Keyword If Test Failed · Run Keyword If Test Passed · Run Keyword If Timeout Occurred · Run Keyword Unless · Run Keywords · Set Global Variable · Set Library Search Order · Set Log Level · Set Suite Documentation · Set Suite Metadata · Set Suite Variable · Set Tags ·Set Test Documentation · Set Test Message · Set Test Variable · Set Variable · Set Variable If · Should Be Empty · Should Be Equal · Should Be Equal As Integers · Should Be Equal As Numbers · Should Be Equal As Strings · Should Be True · Should Contain · Should Contain Any ·Should Contain X Times · Should End With · Should Match · Should Match Regexp · Should Not Be Empty · Should Not Be Equal · Should Not Be Equal As Integers · Should Not Be Equal As Numbers · Should Not Be Equal As Strings · Should Not Be True · Should Not Contain ·Should Not Contain Any · Should Not End With · Should Not Match · Should Not Match Regexp · Should Not Start With · Should Start With · Sleep · Variable Should Exist · Variable Should Not Exist · Wait Until Keyword Succeeds

一一看过还是比较花时间,先跳过。


再大概过一下有哪些库吧:

All packages

All robot packages are listed below. Typically you should not need to import anything from them directly, but the above public APIs may return objects implemented in them.

robot package提供了命令行的调用:

  • run(): Function to run tests.
  • run_cli(): Function to run tests with command line argument processing.
  • rebot(): Function to post-process outputs.
  • rebot_cli(): Function to post-process outputs with command line argument processing.
  • libdoc: Module for library documentation generation.
  • testdoc: Module for test case documentation generation.
  • tidy: Module for test data clean-up and format change.

robot.api对外的API都通过这个模块暴露出来,包括:

  • logger module for test libraries’ logging purposes.
  • deco module with decorators test libraries can utilize.
  • TestCaseFileTestDataDirectory, and ResourceFile classes for parsing test data files and directories. In addition, a convenience factory method TestData() creates either TestCaseFileor TestDataDirectory objects based on the input.
  • TestSuite class for creating executable test suites programmatically and TestSuiteBuilder class for creating such suites based on existing test data on the file system.
  • SuiteVisitor abstract class for processing testdata before execution. This can be used as a base for implementing a pre-run modifier that is taken into use with --prerunmodifier commandline option.
  • ExecutionResult() factory method for reading execution results from XML output files andResultVisitor abstract class to ease further processing the results. ResultVisitor can also be used as a base for pre-Rebot modifier that is taken into use with --prerebotmodifiercommandline option.
  • ResultWriter class for writing reports, logs, XML outputs, and XUnit files. Can write results based on XML outputs on the file system, as well as based on the result objects returned by the ExecutionResult() or an executed TestSuite.
这些接口大概在开发自己的library的时候会比较有用吧。

robot.model package

Package with generic, reusable and extensible model classes.

This package contains, for example, TestSuiteTestCaseKeyword and SuiteVisitor base classes. These classes are extended both by execution and result related model objects and used also elsewhere.


robot.running package

Implements the core test execution logic.

The main public entry points of this package are of the following two classes:

  • TestSuiteBuilder for creating executable test suites based on existing test case files and directories.
  • TestSuite for creating an executable test suite structure programmatically.

It is recommended to import both of these classes via the robot.api package like in the examples below. Also TestCase and Keyword classes used internally by the TestSuite class are part of the public API. In those rare cases where these classes are needed directly, they can be imported from this package.

这个模块是核心测试执行逻辑。有一个例子:
比如有个 activate_skynet.robot 文件内容如下
*** Settings ***
Library    OperatingSystem

*** Test Cases ***
Should Activate Skynet
    [Tags]    smoke
    [Setup]    Set Environment Variable    SKYNET    activated
    Environment Variable Should Be Set    SKYNET
对应的,如果不用robot去执行,可以用TestSuiteBuilder来读
from robot.api import TestSuiteBuilder

suite = TestSuiteBuilder().build('path/to/activate_skynet.robot')
也可以用TestSuite的模块直接build出来

from robot.api import TestSuite

suite = TestSuite('Activate Skynet')
suite.resource.imports.library('OperatingSystem')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])
test.keywords.create('Set Environment Variable', args=['SKYNET', 'activated'], type='setup')
test.keywords.create('Environment Variable Should Be Set', args=['SKYNET'])
还有一个执行的例子

result = suite.run(critical='smoke', output='skynet.xml')

assert result.return_code == 0
assert result.suite.name == 'Activate Skynet'
test = result.suite.tests[0]
assert test.name == 'Should Activate Skynet'
assert test.passed and test.critical
stats = result.suite.statistics
assert stats.critical.total == 1 and stats.critical.failed == 0
然后是生产report的例子

from robot.api import ResultWriter

# Report and xUnit files can be generated based on the result object.
ResultWriter(result).write_results(report='skynet.html', log=None)
# Generating log files requires processing the earlier generated output XML.
ResultWriter('skynet.xml').write_results()
这个例子有点矛盾,应该是先生产XML然后HTML吧


好了,今天到这里,明天继续,看看有没有什么新思路

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值