我如何在 Cypress 中编写单元测试?这可能吗?
How do I write unit tests in Cypress? Is that even possible?
我正在使用 Cypress 和 Cucumber 来开发一个前端 vue.js 使用行为驱动开发 (BDD)。我已经可以使用 Cypress 进行端到端测试,但我还需要创建和 运行 单元测试。我在赛普拉斯官方网站上找不到任何东西。我如何为这个前端创建和 运行 单元测试?
您可以在 official docs 中找到与单元测试相关的所有方法。
假设您想对应用程序代码中的数学函数进行单元测试,您可以使用 Cypress 来实现:
/// <reference types="cypress" />
import math from '../../math'
describe('Unit Test Application Code', function () {
const { add, divide, multiply, subtract } = math
before(() => {
// check if the import worked correctly
expect(add, 'add').to.be.a('function')
})
context('math.js', function () {
it('can add numbers', function () {
expect(add(1, 2)).to.eq(3)
})
it('can subtract numbers', function () {
expect(subtract(5, 12)).to.eq(-7)
})
it('can divide numbers', function () {
expect(divide(27, 9)).to.eq(3)
})
it('can muliple numbers', function () {
expect(multiply(5, 4)).to.eq(20)
})
})
})
我正在使用 Cypress 和 Cucumber 来开发一个前端 vue.js 使用行为驱动开发 (BDD)。我已经可以使用 Cypress 进行端到端测试,但我还需要创建和 运行 单元测试。我在赛普拉斯官方网站上找不到任何东西。我如何为这个前端创建和 运行 单元测试?
您可以在 official docs 中找到与单元测试相关的所有方法。
假设您想对应用程序代码中的数学函数进行单元测试,您可以使用 Cypress 来实现:
/// <reference types="cypress" />
import math from '../../math'
describe('Unit Test Application Code', function () {
const { add, divide, multiply, subtract } = math
before(() => {
// check if the import worked correctly
expect(add, 'add').to.be.a('function')
})
context('math.js', function () {
it('can add numbers', function () {
expect(add(1, 2)).to.eq(3)
})
it('can subtract numbers', function () {
expect(subtract(5, 12)).to.eq(-7)
})
it('can divide numbers', function () {
expect(divide(27, 9)).to.eq(3)
})
it('can muliple numbers', function () {
expect(multiply(5, 4)).to.eq(20)
})
})
})