我有一个模块,我正在使用mocha-phantomjs测试这个模块.我创建了package.json文件 { "name" : "demo-test", "scripts": { "test": "npm run test-debug", "test-debug": "mocha-phantomjs ./test/Test1.html" }, "dependencies" : { "m
{ "name" : "demo-test", "scripts": { "test": "npm run test-debug", "test-debug": "mocha-phantomjs ./test/Test1.html" }, "dependencies" : { "mocha" : "1.13.x", "commander" : "1.2.x", "which" : "~1.0.5", "mocha-phantomjs": "3.3.2" }, "devDependencies" : { "chai" : "1.8.x", "coffee-script" : "1.6.x", "requirejs" : "2.1.x", "jquery" : "2.1.0" } }
然后我运行npm install然后运行npm test来运行测试.它工作正常并运行test1.html的测试.
现在我希望测试目录下的所有文件(test1,test2,…)在运行npm test时执行.
我可以通过在package.json文件中单独调用来运行所有html文件,但是如果有办法加载所有Html文件.
通常,您可以将Tests.html文件传递给mocha-phantomjs运行器,该运行器将加载您要使用脚本标记运行的所有测试文件.Tests.html将包含:
<script src="controller-tests/one-controller-test.js"></script> <script src="controller-tests/another-controller-test.js"></script> <script src="controller-tests/yet-another-controller-test.js"></script> <script src="service-tests/one-service-test.js"></script> <script src="service-tests/another-service-test.js"></script> <script src="service-tests/yet-another-service-test.js"></script>
或者,如果您使用的是RequireJS或另一个AMD库,则可以加载一个test-init.js文件,并在该文件中单独或以嵌套方式输入所有测试文件,如下所示:
Tests.html
<script src="test-init.js"></script>
测试init.js
require('controller-tests/init.js'); require('service-tests/init.js');
控制器的测试/ init.js
require('one-controller-test.js'); require('another-controller-test.js'); require('yet-another-controller-test.js');
服务测试/ init.js
require('one-service-test.js'); require('another-service-test.js'); require('yet-another-service-test.js');