RobotFramework 之 用例标签机制
用例标签机制
Force Tags
新建两个robot文件,通过Force Tags
关键字,在 test1.robot
文件中的 Settings
表里打上标签,标签名为 smokingtest
这样, test1.robot
文件中的所有用例都会打上 smokingtest
标签
test1.robot
*** Settings ***
Force Tags smokingtest
*** Test Cases ***
Case1
log to console case1
Case2
log to console case2
test2.robot
*** Test Cases ***
Case3
log to console case1
Case4
log to console case2
通过命令 robot --include smokingtest test*.robot
,执行所有以test开头
的robot文件中,带 smokingtest
标签的用例
只执行了 test1.robot
文件中的 Case1
和 Case2
,因为 test2.robot
文件中 Case3
和 Case4
用例没有 smokingtest
标签
[Tags]
在robot文件中的 Test Cases
表里的用例打上标签,标签名为 tag1
test1.robot
*** Settings ***
Force Tags smokingtest
*** Test Cases ***
Case1
[Tags] tag1
log to console case1
Case2
log to console case2
test2.robot
*** Test Cases ***
Case3
[Tags] tag1
log to console case1
Case4
log to console case2
通过命令 robot --include tag1 test*.robot
,执行所有以test开头
的robot文件中,带 tag1
标签的用例
只执行了 test1.robot
文件中的 Case1
和 test2.robot
文件的 Case3
一个用例,可以打上多个标签
test1.robot
*** Settings ***
Force Tags smokingtest
*** Test Cases ***
Case1
[Tags] tag1 tag2
log to console case1
Case2
[Tags] tag2
log to console case2
test2.robot
*** Test Cases ***
Case3
[Tags] tag1 tag3
log to console case1
Case4
log to console case2
通过命令 robot --include tag* test*.robot
,执行所有以test开头
的robot文件中,标签名以tag
开头的用例
如果想要筛选没有打标签的用例,该如何筛选?
比如上面,Case4
就是没有带上任何标签的
Default Tags
可以在 Settings
表里通过Default Tags
关键字,给没有打上标签的用例,打上默认标签
test1.robot
*** Settings ***
Force Tags smokingtest
Default Tags notags
*** Test Cases ***
Case1
[Tags] tag1 tag2
log to console case1
Case2
[Tags] tag2
log to console case2
test2.robot
*** Settings ***
Default Tags notags
*** Test Cases ***
Case3
[Tags] tag1 tag3
log to console case1
Case4
log to console case2
然后,通过命令 robot --include notags test*.robot
,执行所有以test开头
的robot文件中,标签名为默认标签notags
的用例
这时候,就只执行了Case4
,也就是标签名为默认标签的用例
【拓展】:如果想对目录下的所有robot文件都打上标签,该如何进行?
__init__.robot
在test文件夹下,新建__init__.robot
文件,通过Force Tags
关键字,在__init__.robot
文件中的 Settings
表里打上标签,标签名为 default
*** Settings ***
Force Tags default
然后,通过命令robot --include default test
,执行test文件夹下所有robot文件中,标签名为默认标签default
的用例
此时,test文件夹下所有robot文件中,所有用例都执行了
根据标签选择测试用例
执行包含 标签 ‘tag1’ 的用例
-- include tag1
执行同时包含 标签 ‘tag1’ 和标签 ‘tag2’ 的用例
-- include tag1ANDtag2
执行同时包含 标签 ‘tag1’ 和标签 ‘tag test’ 的用例
-- include tag1AND"tag test"
执行不包含 标签 ‘tag1’ 的用例
-- exclude tag1
执行包含 标签 ‘tag1’ 但不包含标签 ‘tag test’ 的用例
-- include tag1OR"tag test"
执行标签格式为 ‘t*t’ 的用例,如“test”
-- exclude t*t
指定关键测试用例
如果本次测试中有关键测试用例没有通过,那么整个测试就被视为测试不通过。反之,整个测试就视为通过
缺省情况下,RF执行测试时,每个测试用例都被视为关键测试用例
可以通过参数 --critical 或 -c
和 --noncritical 或 -n
后面加上 tag名称
,来指定测试用例是否为关键测试用例
--critical ok
指定只有具有ok
标签的用例才是关键用例--noncritical not_pass
指定不具有not_pass
标签的用例才是关键用例,其他用例都不是关键用例--critical ok* --noncritical not*
指定只有具有以ok
开头标签的用例,且没有以not
开头的标签的用例,才是关键用例,其他用例都不是关键用例
通常都是在主流程用例中打上标签
test.robot
*** Settings ***
Default Tags not_pass
*** Test Cases ***
Case1
[Tags] ok
log to console case1
Case2
should be true '123'=='1234'
这里模拟 Case1
为主流程用例,Case2
为非关键用例, Case1
会执行通过,而Case2
则是执行失败
报告中,Case2
虽然执行失败,但整个报告显示的还是通过,因为Case2
为非关键用例,及时失败了,不影响整个报告的结果