如何运行Robot Framework中被配置排除的测试用例?
Robot Framework:命令行覆盖配置文件的标签排除规则
我给测试套件中运行缓慢的测试用例添加了slow标签:
# tests_1.robot TEST-1 [Tags] slow (...)
为避免常规执行时耗时过久,我在Robot配置文件中添加规则排除这类标签:
# robot-arguments.cfg (... 其他配置项 ...) --exclude slow
常规执行命令时,带slow标签的测试用例会被跳过,符合预期:
$ robot --argumentfile robot-arguments.cfg -t "TEST*" tests/ [ ERROR ] Suite 'Tests' contains no tests or tasks matching name 'TEST*' and not matching tag 'slow'.
但偶尔需要运行这些慢测试,尝试用--include slow参数时出现冲突:
$ robot --argumentfile robot-arguments.cfg -t "TEST*" tests/ --include slow [ ERROR ] Suite 'Tests' contains no tests or tasks matching name 'TEST*' and matching tag 'slow' and not matching tag 'slow'.
错误信息显示标签匹配规则冲突,而我不想修改配置文件(该文件还用于其他场景),尝试--skip slow配置也遇到同样问题。我曾试图阅读Robot Framework源码理解参数优先级,但项目结构复杂未能理清。
解决方案:使用--no-exclude清除配置文件的排除规则
Robot Framework 3.2及以上版本支持--no-exclude参数,用于清除之前所有的--exclude规则(包括配置文件中定义的)。结合--include slow即可只运行带slow标签的测试用例:
robot --argumentfile robot-arguments.cfg --no-exclude --include slow -t "TEST*" tests/
原理说明
Robot Framework的标签过滤逻辑为:
- 测试用例必须满足所有
--include规则 - 同时必须不满足任何
--exclude规则
配置文件中的--exclude slow和命令行的--include slow会形成矛盾要求(测试用例需同时有slow标签和无slow标签),导致无符合条件的用例。--no-exclude会清空之前的排除规则,此时仅需满足--include slow的条件即可执行目标测试。
低版本兼容方案:临时覆盖排除规则
若使用的Robot Framework版本不支持--no-exclude,可通过命令行添加空的--exclude规则抵消配置文件中的排除逻辑:
robot --argumentfile robot-arguments.cfg --exclude "" --include slow -t "TEST*" tests/
空的--exclude规则会覆盖配置文件中的--exclude slow,最终仅过滤出带slow标签的测试用例。
内容的提问来源于stack exchange,提问作者Joël
相关产品推荐
相关产品推荐

