测试结构
必要组成部分: describe()
和it()
//当前测试套件中,所有测试用例执行之前运行
before(function(){
//测试前置条件
}
//当前测试套件中,所有测试用例执行结束之后运行
after(function(){
//测试前置条件
}
//当前测试套件中,每个测试用例执行之前运行
beforeEach(function(){
//测试前置条件
}
//当前测试套件中,每个测试用例执行之后运行
afterEach(function(){
//测试前置条件
}
从 Mocha 借鉴过来的测试接口提供了 describe()
,context()
,it()
和specify()
方法。
context()
与 describe()
相同, specify()
与 it()
相同,所以选择最适合你的就行了。
// -- Start: Our Application Code --
function add (a, b) {
return a + b
}
function subtract (a, b) {
return a - b
}
function divide (a, b) {
return a / b
}
function multiply (a, b) {
return a * b
}
// -- End: Our Application Code --
// -- Start: Our Cypress Tests --
describe('Unit test our math functions', function() {
context('math', 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)
})
specify('can divide numbers', function() {
expect(divide(27, 9)).to.eq(3)
})
specify('can multiply numbers', function() {
expect(multiply(5, 4)).to.eq(20)
})
})
})
// -- End: Our Cypress Tests --