背景

  • Cypress 的断言库是基于 Chai 断言库的
  • 并且增加了对 Sinon-Chai,Chai-jQuery 断言库的支持,带来了强大的断言功能
  • Cypress 支持 BDD(expect/should)和 TDD(assert)格式的断言

BDD、TDD 格式断言的简单栗子

BDD

Cypress系列(61)- 断言最佳实践 - 图1

TDD

Cypress系列(61)- 断言最佳实践 - 图2

Cypress 命令内置的断言

Cypress 命令通常具有内置的断言,这些断言将导致命令自动重试,以确保命令成功(或者超时后失败)

  1. it('cypress 命令自带断言', function () {
  2. cy.wrap({body: {name: 'poloyy'}})
  3. .its('body')
  4. .should('deep.eq', {name: 'poloyy'})
  5. });

Cypress 有哪些常见内置断言操作的命令

Cypress系列(61)- 断言最佳实践 - 图3

Cypress 提供两个方法来断言

隐性断言:should()、and()

  1. cy
  2. .get('form')
  3. .should('be.visible')
  4. .and('have.class', 'open')

显性断言:expect

expect 允许传入一个特定的对象并且对它进行断言

  1. expect(true).to.be.true

混合使用隐性断言和显性断言

  1. cy.get('.action-email')
  2. .type('fake@email.com')
  3. .should(($el) => {
  4. expect($el).to.have.value('fake@email.com')
  5. expect($el).to.be.visible
  6. })

TDD、BDD 常见断言

BDD 形式的断言

Cypress系列(61)- 断言最佳实践 - 图4
Cypress系列(61)- 断言最佳实践 - 图5
Cypress系列(61)- 断言最佳实践 - 图6

TDD 形式的断言

Cypress系列(61)- 断言最佳实践 - 图7
Cypress系列(61)- 断言最佳实践 - 图8
Cypress系列(61)- 断言最佳实践 - 图9

https://www.cnblogs.com/poloyy/p/13744006.html