发布于 4年前

RobotFramework quickstart demo的文本格式(in plaintext)

RobotFramework Quickstart是RobotFramework(下面简称为RF)提供的一个Demo,可以直接运行,用来体验RF。为了嵌入文字说明,该demo使用的HTML格式,因此在新手看来,比较难以区分HTML文件中哪些是定义用例,哪些是demo的说明。因此,我将该用例转换成了纯文本格式(robot后缀),以方便大家。

下面就是这个quickstart demo的内容,直接复制到一个文本文件中即可,文件后缀名可以是txt或者robot都行,个人推荐使用robot作为后缀名。有两个地方需要说明一下:

  1. 开头添加的# -*- coding: robot -*-这样是用于VIM插件mfukar/robotframework-vim自动识别文本格式的。
  2. 本文件采用的是连续两个空格作为分隔符(也可以采用竖线|作为分隔符)。

下面是文件内容:

# -*- coding: robot -*-

*** Settings ***
Library  OperatingSystem
Library  testlibs/LoginLibrary.py

Test setup  Clear login database
Test teardown

Force tags  quickstart
Default tags  example  smoke

*** Variables ***
${USERNAME}  janedoe
${PASSWORD}  J4n3D0e
${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

*** Test Cases ***
User can create and account and log in
    Create valid user  fred  P4ssw0rd
    Attempt to login with credentials  fred  P4ssw0rd
    Status should be  Logged In

User cannot log in with bad password
    Create valid user  betty  P4ssw0rd
    Attempt to login with credentials  betty  wrong
    Status should be  Access Denied

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

Too short password
    Creating user with invalid password should fail  abCD5  ${PWD INVALID LENGTH}

Too long password
    Creating user with invalid password should fail  abCD567890123
    ...  ${PWD INVALID LENGTH}

Password without lowercase letters
    Creating user with invalid password should fail  123DEFG  ${PWD INVALID CONTENT}

Password without capital letters
    Creating user with invalid password should fail  abcd56789
    ...  ${PWD INVALID CONTENT}

Password without numbers
    Creating user with invalid password should fail  AbCdEfGh
    ...  ${PWD INVALID CONTENT}

Password with special characters
    Creating user with invalid password should fail  abCD56+  ${PWD INVALID CONTENT}

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 ***
Clear login database
    Remove file  ${DATABASE FILE}

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}

Login
    [Arguments]  ${username}  ${password}
    Attempt to login with credentials  ${username}  ${password}
    Status should be  Logged In

# Used by BDD test cases
Given a user has a valid account
    Create valid user  ${USERNAME}  ${PASSWORD}

When she changes her password
    Change password  ${USERNAME}  ${PASSWORD}  ${NEW PASSWORD}
    Status should be  SUCCESS

Then she can log in with the new password
    Login  ${USERNAME}  ${NEW PASSWORD}

And she cannot use the old password anymore
    Attempt to login with credentials  ${USERNAME}  ${PASSWORD}
    Status should be  Access Denied

Database should contain
    [Arguments]  ${username}  ${password}  ${status}
    ${database}=  Get file  ${DATABASE FILE}
    Should contain  ${database}  ${username}\t${password}\t${status}

对于几个测试密码有效性检查的用例,也可以改成给予模板的测试用例,可以少敲好多次键盘:

Too short password
    Creating user with invalid password should fail  abCD5  ${PWD INVALID LENGTH}

Too long password
    Creating user with invalid password should fail  abCD567890123
    ...  ${PWD INVALID LENGTH}

Password without lowercase letters
    Creating user with invalid password should fail  123DEFG  ${PWD INVALID CONTENT}

Password without capital letters
    Creating user with invalid password should fail  abcd56789
    ...  ${PWD INVALID CONTENT}

Password without numbers
    Creating user with invalid password should fail  AbCdEfGh
    ...  ${PWD INVALID CONTENT}

Password with special characters
    Creating user with invalid password should fail  abCD56+  ${PWD INVALID CONTENT}

将上面这几个用例改写成如下一个用例:

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}  

相关文章

©2020 edoou.com   京ICP备16001874号-3