前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Cypress web自动化15-Hooks使用方法

Cypress web自动化15-Hooks使用方法

作者头像
上海-悠悠
发布2020-05-18 15:52:49
7630
发布2020-05-18 15:52:49
举报

前言

Cypress 提供了 hooks 函数,方便我们在组织测试用例的时候,设置用例的前置操作和后置清理。 类似于 python 的 unittest 里面的 setUp 和 setUpclass 功能

Hooks

Cypress 提供了 hooks 函数。 这些有助于设置要在一组测试之前或每个测试之前运行的条件。它们也有助于在一组测试之后或每次测试之后清理条件。

代码语言:javascript
复制
describe('Hooks', () => {
  before(() => {
    // runs once before all tests in the block
  })

  after(() => {
    // runs once after all tests in the block
  })

  beforeEach(() => {
    // runs before each test in the block
  })

  afterEach(() => {
    // runs after each test in the block
  })
})

Hooks 和测试执行的顺序如下:

  • before()钩子运行(一次)
  • beforeEach() 每个测试用例前都会运行
  • it 运行测试用例
  • afterEach() 每个测试用例之后都会运行
  • after() 钩子运行(一次)

执行案例

写2个测试用例,带上 hooks 函数,查看用例执行顺序,

代码语言:javascript
复制
/**
 * Created by dell on 2020/5/13.
 * hook_demo.js
 * 作者:上海-悠悠 QQ交流群:939110556
 */

describe('Hooks', () => {
  before(() => {
      // runs once before all tests in the block
      cy.log("所有的用例之前只执行一次,测试准备工作")
  })
  after(() => {
      // runs once after all tests in the block
      cy.log("所有的用例之后只执行一次")
  })
  beforeEach(() => {
      // runs before each test in the block
      cy.log("每个用例之前都会执行")
  })
  afterEach(() => {
      // runs after each test in the block
      cy.log("每个用例之后都会执行")
  })
  it('test case 1', () => {
      cy.log("test case 1")
      expect(true).to.eq(true)
  })
  it('test case 2', () => {
      cy.log("test case 2")
      expect(true).to.eq(true)
  })
})

运行结果

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 执行案例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档