Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何设置mocha先运行单元测试,然后启动服务器并运行集成测试_Node.js_Unit Testing_Server_Mocha.js_Integration Testing - Fatal编程技术网

Node.js 如何设置mocha先运行单元测试,然后启动服务器并运行集成测试

Node.js 如何设置mocha先运行单元测试,然后启动服务器并运行集成测试,node.js,unit-testing,server,mocha.js,integration-testing,Node.js,Unit Testing,Server,Mocha.js,Integration Testing,我有单元测试和集成测试,需要使用一个“npm run mocha”命令来运行。我的集成测试之前的套件已设置,并与mocha正常工作。选项包括之前的步骤,如: test/beforeTests.js (sets up some important variables) test/*.x.js (bulk of the tests, all the files ending in .x.js are run) 当包含我的集成测试时,单元测试会失败,因为正在运行的服务器与某些存根函数冲突。因此,我想

我有单元测试和集成测试,需要使用一个“npm run mocha”命令来运行。我的集成测试之前的套件已设置,并与mocha正常工作。选项包括之前的步骤,如:

test/beforeTests.js (sets up some important variables)
test/*.x.js (bulk of the tests, all the files ending in .x.js are run)
当包含我的集成测试时,单元测试会失败,因为正在运行的服务器与某些存根函数冲突。因此,我想先运行单元测试,然后启动服务器,运行集成测试并关闭服务器。我尝试将mocha.opts更改为:

test/beforeTests.js (sets up some important variables)
test/*.x.js (bulk of the tests, all the files ending in .x.js are run)
test/startServer.js (start the server)
test/integration/*.x.js (run integration tests)
test/afterTests.js (close the server)
beforeTests.js:

before(function(done) { 
 // do something 
});
startServer.js:

before(function() {
 return runServer()
});
beforeTests和startServer都可以工作,但是它们都是在测试开始时执行的,而不是

beforeTest>do unit tests>start server>do integration tests>stop server


我不能将集成测试合并到一个文件中,也不能将单元测试合并到一个文件中,因为它们太多了。有没有办法设置mocha.opts来做我想做的事。我环顾四周,没有任何东西真正符合我想要做的。

我以前使用的一种技术是为单元测试和集成测试使用两个npm命令,如果我想在一次执行中运行它们,那么将它们合并到一个npm命令中

"test": "npm run test:unit && npm run test:integration"
"test:unit": "mocha test/unit",
"test:integration": "mocha test/integration"
您可以在集成测试中启动服务器

我建议对
npm test
使用
test
,而不是
mocha
npm run mocha
使用
test,因为它是节点项目中更标准的测试命令名


希望它能有所帮助

我之前使用的一种技术是为单元和集成测试使用两个npm命令,如果我想在一次执行中运行它们,那么将它们合并到一个npm命令中

"test": "npm run test:unit && npm run test:integration"
"test:unit": "mocha test/unit",
"test:integration": "mocha test/integration"
您可以在集成测试中启动服务器

我建议对
npm test
使用
test
,而不是
mocha
npm run mocha
使用
test,因为它是节点项目中更标准的测试命令名


希望有帮助

是的,这就是我的方向!谢谢你给我一个明确的例子。我听说在同一个“套件”中同时运行单元测试和集成测试是一种糟糕的做法……是的,这就是我的方向!谢谢你给我一个明确的例子。我听说在同一个“套件”中同时运行单元测试和集成测试是一种糟糕的做法。。。