[英]Can `describe` contain a mix of `describe` and `it` in Mocha?
I have this simple example unit test in Mocha:我在 Mocha 中有这个简单的示例单元测试:
const {Builder, By, Key, until} = require('selenium-webdriver')
const chai = require('chai')
chai.should()
var customer
var gigger
var admin
/* eslint-disable no-unused-expressions */
/* globals describe,it */
describe('start all tests', async function () {
describe('create customer user', async function () {
it('creates a user browser', async function () {
customer = await new Builder().forBrowser('chrome').build()
customer.should.not.be.null
this.timeout(5000)
await customer.get('http://www.google.com/ncr')
await customer.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN)
await customer.wait(until.titleIs('webdriver - Google Search'), 1000)
})
it('creates a gigger browser', async function () {
gigger = await new Builder().forBrowser('chrome').build()
gigger.should.not.be.null
})
it('creates an admin browser', async function () {
admin = await new Builder().forBrowser('chrome').build()
admin.should.not.be.null
})
})
describe('close it all down', async function () {
it('close all browsers down', async function () {
await customer.quit()
await gigger.quit()
await admin.quit()
})
})
})
Everything works fine.一切正常。 BUT If I un-comment the last
describe
:但是如果我取消评论最后一个
describe
:
//describe('close it all down', async function () {
it('close all browsers down', async function () {
await customer.quit()
await gigger.quit()
await admin.quit()
})
//})
What actually happens is that close all browsers down
actually runs straight away .实际发生的是
close all browsers down
实际上是立即运行的。
Is that because describe
functions must contain either all describe
functions, rather than it
as well?那是因为
describe
函数必须包含所有describe
函数,而不是it
? If so, this is not documented.如果是这样,则没有记录。
Or, am I missing something?或者,我错过了什么?
ADDENDUM : In fact, how does Mocha actually work?附录:事实上,摩卡实际上是如何工作的? I got so used to just typing it, that I never quite got into the actual workings of it.
我已经习惯了只是打字,以至于我从来没有完全了解它的实际工作原理。 What does Mocha's
describe()
actually do? Mocha 的
describe()
实际上是做什么的? Does it just run the callback?它只是运行回调吗? When another nested
describe()
is called, how does it figure out that it's a nested one, and which one the parent is?当另一个嵌套的
describe()
被调用时,它如何确定它是一个嵌套的,以及它的父对象是哪一个? How does it all work, "in general"? “一般”,这一切是如何运作的?
all your tests are running asynchronously so you cant control the execution flow of them, for what you want to do, you should use an after() see mocha hooks 您所有的测试都异步运行,因此您无法控制它们的执行流程,对于您想要执行的操作,应使用after()参见mocha hooks
For your other question you should see this thread where its really well explained. 对于您的其他问题,您应该在真正解释清楚的地方看到此线程 。 Happy Testing !
测试愉快!
Yes, describe
can contain a mix of describe
and it
in Mocha.是的,
describe
可以在 Mocha 中包含describe
和it
的混合。 And also, the beforeEach
and afterEach
defined in outer describe
will apply to test cases in nested describe
as well.此外,外部
describe
定义的beforeEach
和afterEach
也将适用于嵌套describe
中的测试用例。
Run below example:运行以下示例:
describe('test suite', () => {
before(() => {
console.log(' >>> outer before all');
});
after(() => {
console.log(' <<< outer after all');
});
beforeEach(() => {
console.log(' >>> outer before each');
});
afterEach(() => {
console.log(' <<< outer after each');
});
describe('# nested test suite', () => {
before(() => {
console.log(' >>> # nested before all');
});
after(() => {
console.log(' <<< # nested after all');
});
beforeEach(() => {
console.log(' >>> # nested before each');
});
afterEach(() => {
console.log(' <<< # nested after each');
});
it('nested test', () => { })
})
it('test', () => { })
});
From the output you can see the order of the hooks:从 output 你可以看到钩子的顺序:
test suite
>>> outer before all \
>>> outer before each \ |
✓ test | |
<<< outer after each / |
# nested test suite \ |
>>> # nested before all | |
>>> outer before each \ | |
>>> # nested before each \ | | |
✓ nested test | | | |
<<< # nested after each / | | |
<<< outer after each / | |
<<< # nested after all / |
<<< outer after all /
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.