You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Robot Framework与DRY原则:解决测试用例步骤重复问题

Hey there! Let's work through fixing that test step duplication using the DRY (Don't Repeat Yourself) principle—this is such a common pain point in test automation, and the fix will make your suite way easier to maintain.

The Root Issue

Looking at your two test cases, the entire login flow is repeated: once in the "Valid Login" test, and again as the setup for the "Edit Name" test. If you ever need to update login credentials, adjust the login page URL, or tweak how you verify the welcome page loads, you'd have to make those changes in multiple places—error-prone and inefficient.

The DRY Solution: Reuse Your Log in Keyword

You already have a Log in keyword defined, so let's lean into that! We'll refactor your tests to call this keyword wherever login is needed, eliminating all duplicated steps. Here's how to do it:

1. Polish the Reusable Log in Keyword

First, make sure your keyword is self-contained (and optionally parameterized for flexibility). If you only ever use the "demo"/"mode" credentials, you can keep it simple. If you might need different credentials later, add parameters:

*** Keywords ***
# Option 1: Fixed credentials (matches your current use case)
Log in
    Open browser to login page
    Log in with username "demo" and password "mode"
    Verify that welcome page is opened
    Navigate to account page  # Add this if your second test starts at the account page

# Option 2: Parameterized for flexibility (uses default credentials unless specified)
Log in
    [Arguments]    ${username}=demo    ${password}=mode
    Open browser to login page
    Log in with username "${username}" and password "${password}"
    Verify that welcome page is opened
    Navigate to account page

2. Refactor Your Test Cases

Now rewrite your tests to use the Log in keyword instead of repeating the login steps. This keeps each test focused on its unique purpose:

*** Test Cases ***
1) 系统登录(有效登录)
    Given 浏览器已打开至登录页面
    When 用户“demo”使用密码“mode”登录
    Then 应打开欢迎页面

2) 登录后操作(修改名字)
    [Setup]    Log in  # Reuse the keyword here instead of repeating login steps
    Given 用户处于账户页面
    When 用户编辑名字字段
    Then 新名字显示在名字字段中

Why This Works

  • Single Source of Truth: All login logic lives in one keyword. Any changes to the login flow only need to be made once.
  • Cleaner Tests: Your test cases now focus on what they're actually testing—no more boilerplate login steps cluttering them up.
  • Consistency: Every test that uses Log in follows the exact same flow, reducing flakiness from inconsistent manual steps.

If most of your tests require login, you can even set Log in as a Suite Setup in the *** Settings *** section, so it runs once before all tests (or Test Setup to run before each test that needs it).

内容的提问来源于stack exchange,提问作者Ekaterina Korneeva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:46:17