机器人框架和DRY:功能步骤重复测试用例步骤

时间:2018-04-30 13:21:24

标签: selenium automated-tests robotframework

我想自动化两个测试用例:
1)登录系统

Valid Login  
    Given browser is opened to login page  
    When user "demo" logs in with password "mode"  
    Then welcome page should be open

2)登录后执行任何操作

Change first name
    [Setup]    Log in 
    Given user is on account page
    When user edits first-name field
    Then new first name is displayed in first-name field

*** Keywords ***
Log in
   Open browser to login page
   Log in with username "demo" and password "mode"
   Verify that welcome page is opened

对于第二个,我必须编写一个完全重复第一个测试用例步骤的关键字。

这在测试中发生了很多,所以我最终在我的项目中重复了两次重复步骤。是否有一个巧妙的技巧来避免这种代码重复,同时保持顶级测试用例看起来像Gherkin?

此问题是否有最佳做法?

1 个答案:

答案 0 :(得分:0)

我将Gherkin风格的登录步骤移动到关键字,并编写了Valid Login测试用例,但没有做任何事情。

Test Setup为你调用它或者在测试中明确地写它是我的首选。

一个很好的资源:How to write good test cases using Robot Framework

*** Settings ***
Test Setup            Log in

*** Test Cases ***

Valid Login
    No Operation

Change first name
    Given user is on account page
    When user edits first-name field
    Then new first name is displayed in first-name field

*** Keywords ***

Log in
    Given browser is opened to login page
    When user "demo" logs in with password "mode"  
    Then welcome page should be open

enter image description here