在Mocha中的每个测试文件之前运行一组操作 [英] Running a set of actions before every test-file in mocha

查看:94
本文介绍了在Mocha中的每个测试文件之前运行一组操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始与Mocha一起测试我的expressjs服务器. 我的测试被分为多个文件,并且其中的大多数包含一些重复的段(通常在将所有固定装置加载到DB的语句之前,等等),这确实很烦人.

我想我可以将它们全部导出到一个文件中,然后在每次测试中均将其导入,但是我想知道是否还有一些更优雅的解决方案-例如运行具有所有setup命令的某个文件,以及包含所有setup命令的另一个文件.拆卸命令.

I've started recently working with mocha to test my expressjs server. My tests are separated to multiple files and most of them contain some duplicated segments (Mostly before statements that load all the fixtures to the DB, etc) and that's really annoying.

I guess I could export them all to a single file and import them on each and every test, but I wonder if there are some more elegant solutions - such as running a certain file that has all the setup commands , and another file that contains all the tear-down commands.

如果有人知道答案,那就太棒了:)

If anyone knows the answer that would be awesome :)

推荐答案

将摩卡测试的常用功能分解为三个基本级别.如果您想一次加载一些夹具以进行一堆测试(并且您编写的每个测试是独立的),请使用before函数将夹具加载到文件顶部.如果需要每次都重新初始化灯具,也可以使用beforeEach功能.

There are three basic levels of factoring out common functionality for mocha tests. If you want to load in some fixtures once for a bunch of tests (and you've written each test to be independent), use the before function to load the fixtures at the top of the file. You can also use beforeEach function if you need the fixtures re-initialized each time.

第二个选项(相关)是将常用功能提取到单独的文件或文件集中,并需要该文件.

The second option (which is related), is to pull out common functionality into a separate file or set of files and require that file.

最后,请注意,mocha具有根目录挂钩:

Finally, note that mocha has a root level hook:

您还可以选择任何文件并添加根"级挂钩.例如,在所有describe()块之外添加beforeEach().这将使对beforeEach()的回调在任何测试用例之前运行,无论它位于哪个文件中(这是因为Mocha有一个隐含的describe()块,称为根套件").

You may also pick any file and add "root"-level hooks. For example, add beforeEach() outside of all describe() blocks. This will cause the callback to beforeEach() to run before any test case, regardless of the file it lives in (this is because Mocha has an implied describe() block, called the "root suite").

我们使用它一次启动Express服务器(并且我们使用环境变量,以便它在与开发服务器不同的端口上运行):

We use that to start an Express server once (and we use an environment variable so that it runs on a different port than our development server):

before(function () {
  process.env.NODE_ENV = 'test';
  require('../../app.js');
});

(在这里我们不需要done(),因为require是同步的.)也就是说,无论有多少不同的测试文件都包含此根级别的before函数,服务器都只启动了一次.

(We don't need a done() here because require is synchronous.) This was, the server is started exactly once, no matter how many different test files include this root-level before function.

以这种方式拆分内容的优势在于,我们可以运行npm test,该npm test运行所有测试,或者在任何特定文件或任何特定文件夹或任何特定测试或一组测试(仅使用它)上运行mocha和describe.only),所选测试的所有先决条件都将运行.

The advantage of splitting things up in this way is that we can run npm test which runs all tests, or run mocha on any specific file or any specific folder, or any specific test or set of tests (using it.only and describe.only) and all of the prerequisites for the selected tests will run.

这篇关于在Mocha中的每个测试文件之前运行一组操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆