比如
login_tests.robot -> Login Tests
IP_v4_and_v6 -> IP v4 and v6
例如,我们要在一个名为invalid_login.robot脚本中编写测试无效登陆的功能,下面的名字就可以:
*** Test Cases ***
Empty Password
Empty Username
Empty Username And Password
Invalid Username
Invalid Password
Invalid Username And Password
下面的名字就会有点长了:
*** Test Cases ***
Login With Empty Password Should Fail
Login With Empty Username Should Fail
Login With Empty Username And Password Should Fail
Login With Invalid Username Should Fail
Login With Invalid Password Should Fail
Login With Invalid Username And Invalid Password Should Fail
Good:
*** Keywords ***
Login With Valid Credentials
Bad:
*** Keywords ***
Input Valid Username And Valid Password And Click Login Button
如果Setup或者Teadown包含了不相关的步骤,那么可以接受更抽象一点的名称。
如果关键字需要执行的低级步骤已经存在,BuiltIn 关键字中的Run Keywords可以帮你很好的执行它
Good:
*** Settings ***
Suite Setup Initialize System
Good (if only used once):
*** Settings ***
Suite Setup Run Keywords
... Login To System AND
... Add User AND
... Activate Alarms AND
... Check Balance
Bad:
*** Settings ***
Suite Setup Login To System, Add User, Activate Alarms And Check Balance
Good:
*** Settings ***
Documentation Tests to verify that account withdrawals succeed and
... fail correctly depending from users account balance
... and account type dependent rules.
... See http://internal.example.com/docs/abs.pdf
Metadata Version 0.1
Bad (特别是如果测试套件刚好命名为 account_withdrawal.robot):
*** Settings ***
Documentation Tests Account Withdrawal.
Good:
*** Test Cases ***
Valid Login
[Tags] Iteration-3 Smoke
Open Login Page
Input Username ${VALID USERNAME}
Input Password ${VALID PASSWORD}
Submit Credentials
Welcome Page Should Be Open
Bad:
*** Test Cases ***
Valid Login
[Documentation] Opens a browser to login url, inputs valid username
... and password and checks that the welcome page is open.
... This is a smoke test. Created in iteration 3.
Open Browser ${URL} ${BROWSER}
Input Text field1 ${UN11}
Input Text field2 ${PW11}
Click Button button_12
Title Should Be Welcome Page
使用“正常”关键字驱动的风格:
*** Test Cases ***
Valid Login
Open Browser To Login Page
Input Username demo
Input Password mode
Submit Credentials
Welcome Page Should Be Open
使用更高级别的“gherkin”风格:
*** Test Cases ***
Valid Login
Given browser is opened to login page
When user "demo" logs in with password "mode"
Then welcome page should be open
上面的例子你可以参考web demo project
Example:
*** Settings ***
Test Template Login with invalid credentials should fail
*** Test Cases *** USERNAME PASSWORD
Invalid Username invalid ${VALID PASSWORD}
Invalid Password ${VALID USERNAME} invalid
Invalid Both invalid invalid
Empty Username ${EMPTY} ${VALID PASSWORD}
Empty Password ${VALID USERNAME} ${EMPTY}
Empty Both ${EMPTY} ${EMPTY}
*** Keywords ***
Login with invalid credentials should fail
[Arguments] ${username} ${password}
Input Username ${username}
Input Password ${password}
Submit Credentials
Error Page Should Be Open
以上例子同样可以在web demo project 中获取到
Example:
*** Settings ***
Suite Setup Set Active User *** Variables *** # Default system address. Override when tested agains other instances. ${SERVER URL} http://sre-12.example.com/ ${USER} Actual value set dynamically at suite setup *** Keywords *** Set Active User ${USER} = Get Current User ${SERVER URL} Set Suite Variable ${USER}
Good:
*** Test Cases ***
Withdraw From Account
Withdraw From Account $50
Withdraw Should Have Succeeded
*** Keywords ***
Withdraw From Account
[Arguments] ${amount}
${STATUS} = Withdraw From User Account ${USER} ${amount}
Set Test Variable ${STATUS}
Withdraw Should Have Succeeded
Should Be Equal ${STATUS} SUCCESS
Not so good:
*** Test Cases ***
Withdraw From Account
${status} = Withdraw From Account $50
Withdraw Should Have Succeeded ${status}
*** Keywords ***
Withdraw From Account
[Arguments] ${amount}
${status} = Withdraw From User Account ${USER} ${amount}
[Return] ${status}
Withdraw Should Have Succeeded
[Arguments] ${status}
Should Be Equal ${status} SUCCESS
原文地址:How to write good test cases using Robot Framework