Skip to content

Given-当-然后

一句话解释

Given-当-然后是 BDD 场景格式: Given 一些状态, 当操作 occurs, 然后这个应该 result.

用大白话说

Given-当-然后是 方式到 描述测试场景 in plain, structured language. 它 comes 从 BDD (Behavior-Driven Development) and 制作测试 readable 通过每个人 — developers, testers, and business stakeholders.

The three 部分:

  • Given: 开始状态 or preconditions. 'Given 用户是 logged in' 设置上 初始上下文.
  • When: 操作 or 事件. '当它们点击登出 button' 描述什么 happens.
  • Then: expected outcome. '然后它们应该 see 登录 page' 验证 result.

You 能 extend 使用 'And' for 多个条件: 'Given 用户是 logged in And 它们有 项 in 它们的 cart, 当它们点击 checkout, 然后它们应该 see payment page.'

This 格式 serves 双 duty: 它's 两者都 specification (readable 通过 business people) and executable 测试 (tools 如 Cucumber translate 它到 code).

示例

javascript
// Gherkin syntax (Cucumber)
Given('a user is logged in', () => {
  // setup logged-in state
});
When('they click the logout button', () => {
  // perform click action
});
Then('they should see the login page', () => {
  expect(currentPage).toBe('/login');
});

three clauses 映射到 设置, 操作, and assertion in 测试.

相关术语

相关资源

相关课程

  • BDD

    Behavior-driven development.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表