Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

node.js - Mocha tests with extra options or parameters

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:

http://mochajs.org/#usage

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...