带有额外选项或参数的Mocha测试 [英] Mocha tests with extra options or parameters
问题描述
我正在使用Mocha为Node.js应用程序编写测试用例.测试用例需要一个API密钥作为额外的输入选项或参数. API密钥是私有的,因此我不想将其直接包含在测试文件中,因为每个人都可以在GitHub上看到它.我知道可以在以下位置找到适用于Mocha的一些选项:
I am writing test cases for my Node.js application using Mocha. The test cases need an API key as an extra input option or parameter. The API key is private, so I don't want to include it directly in the test files as everyone then can see it on GitHub. I know there are some options available for Mocha at:
但是是否可以包含一些参数,以使测试人员可以在命令行中为测试指定自己的API密钥?如:
But is it possible to include some parameters to let testers specify their own API key for the test in the commandline? Such as:
./node_modules/mocha/bin/mocha test/*.js --key YOUR_KEY
推荐答案
我不认为Mocha本身支持向测试传递额外的参数,但是您可以使用环境变量:
I don't think Mocha itself supports passing extra parameters to your tests, but you could use environment variables:
env KEY=YOUR_KEY mocha test/*.js # assumes some sort of Unix-type OS.
并在测试文件中读取它们:
And read them in your test files:
var key = process.env.KEY;
这篇关于带有额外选项或参数的Mocha测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!