我在同一个
feature
文件中有 2 个类似的测试。它们本质上是相同的测试,只是一个期望找到数据列表,而另一个期望没有数据。
它们都共享相同的步骤名称,但最后一步的实现不同。
myfeature.feature
Scenario Outline: Log in and see data
Given User visits page
When User logs in
Then Should see expected result // Same name step
Scenario Outline: Log in and no data
Given User visits page
When User logs in
Then Should see expected result // Same name step
测试定义文件类似于:
describe('Log in and see data', () => {
Given('User visits page', () => {
// Do page visit
});
When('User logs in', () => {
// Perform log in logic
});
Then('Should see expected result', () => {
cy.get('my-component').should('be.visible');
});
});
describe('Log in and no data', () => {
Given('User visits page', () => {
// Do page visit
});
When('User logs in', () => {
// Perform log in logic
});
Then('Should see expected result', () => {
cy.get('my-component').should('not.exist');
});
});
当我运行 Cypress 时,它显示第一个测试正确通过,但是第二个测试失败,当我查看测试运行时,似乎
Should see expected result
步骤正在命中顶部 Then
块中的 describe
定义对于其他测试...
有关所见行为的问题:
潜台词是您希望将
feature scenario
标题与 describe()
标题相匹配。
但是从基本示例中可以看到步骤定义不使用
describe()
。事实上,我很惊讶没有抛出任何错误。
# cypress/e2e/duckduckgo.feature
Feature: duckduckgo.com
Scenario: visiting the frontpage
When I visit duckduckgo.com
Then I should see a search bar
// cypress/e2e/duckduckgo.ts
import { When, Then } from "@badeball/cypress-cucumber-preprocessor";
When("I visit duckduckgo.com", () => {
cy.visit("https://www.duckduckgo.com");
});
Then("I should see a search bar", () => {
cy.get("input").should(
"have.attr",
"placeholder",
"Search the web without being tracked"
);
});
一旦掌握了这一点,很明显,标题与关键字
Then
和后面的字符串(在功能文件中)以及 Then()
方法的标题相匹配。所以错误变得明显 - 不要尝试使用相同的标题。
我认为任何阅读您报告的人都会感谢您使用更明确的描述
Then('Should not show any data', () => {
cy.get('my-component').should('not.exist');
Then('Should show the data', () => {
cy.get('my-component').should('be.visible');