在运行测试之前清理测试数据库 [英] Cleaning out test database before running tests

查看:121
本文介绍了在运行测试之前清理测试数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道在运行测试套件之前清理数据库的最佳方式是什么(有一个npm库或推荐的方法) before()函数。



我正在使用node / express,mocha和sequelize。

解决方案

之前的函数与清理数据库一样好。如果您只需要清理数据库,即在运行所有测试之前,您可以在单独的文件函数之前在全局中单独执行



globalBefore.js



  before(function(done){
//删除数据库数据这里
done()
})



single-test-1.js



  require('./ globalBefore)
//实际测试1这里



single-test-2.js



  require('./ globalBefore)
//实际测试2这里

请注意globalBefore只会运行一次即使需要两次



测试原理



尝试限制使用外部依赖,如测试中的数据库。外部依赖性越少,测试越容易。您希望能够并行运行所有的单元测试,并且共享资源(如数据库)会使这一困难。



看看Google Tech谈论写作可测试的javascript
http://www.youtube.com/watch?v=JjqKQ8ezwKQ



还要查看重新连线模块。它的功能非常好。


What is the best way to clean out a database before running a test suite (is there a npm library or recommended method of doing this).

I know about the before() function.

I'm using node/express, mocha and sequelize.

解决方案

The before function is about as good as you will do for cleaning out your database. If you only need to clean out the database once i.e. before you run all your tests, you can have a global before function in a separate file

globalBefore.js

before(function(done) {
   // remove database data here
   done()
}) 

single-test-1.js

require('./globalBefore)
// actual test 1 here

single-test-2.js

require('./globalBefore)
// actual test 2 here

Note that the globalBefore will only run once even though it has been required twice

Testing Principles

Try to limit the use of external dependecies such as databases in your tests. The less external dependencies the easier the testing. You want to be able to run all your unit tests in parallel and a shared resource such as a database makes this difficult.

Take a look at this Google Tech talk about writing testable javascript http://www.youtube.com/watch?v=JjqKQ8ezwKQ

Also take look at the rewire module. It works quite well for stubbing out functions.

这篇关于在运行测试之前清理测试数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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