我正在使用newman api运行邮差测试服。它按预期正确执行但我想在文件中导出测试执行期间生成的环境变量,在命令行中可以使用--export-environment:
newman run collectionPreReq.json -e Environment.json -k --export-environment newmanExport.json
同样我正在编写javascript来获取由collectionPreReq导出的环境,但没有得到我正在寻找的东西,代码是
var newman = require('newman');
newman.run({
collection: require('./collectionPreReq.json'),
//reporters: 'cli',
environment: require('./Environment.json'),
insecure: true
}).on('start', function (err, args) {
console.log('running a collection...');
}).on('done', function (err, summary) {
if (err || summary.error) {
console.error('collection run encountered an error.');
}
else {
console.log('collection run completed.:');
console.log("summary environment :");
console.log(summary.environment);
}
});
输出:
running a collection...
collection run completed.:
summary environment :
{ object: [Function], toJSON: [Function] }
答案 0 :(得分:1)
请尝试在javascript文件中使用此新手选项 exportEnvironment:require(' ./ Environment.json'),如
newman.run({
collection: require('./collectionPreReq.json'),
//reporters: 'cli',
environment: require('./Environment.json'),
exportEnvironment: require('./Environment.json'),
insecure: true
})....
当然,这句话可行。
答案 1 :(得分:0)
不可能按NodeJS documentation。
process.env属性返回包含用户的对象 环境。见环境(7)。
此对象的示例如下:
{
TERM: 'xterm-256color',
SHELL: '/usr/local/bin/bash',
USER: 'maciej',
PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
PWD: '/Users/maciej',
EDITOR: 'vim',
SHLVL: '1',
HOME: '/Users/maciej',
LOGNAME: 'maciej',
_: '/usr/local/bin/node'
}
可以修改此对象,但不会进行此类修改 反映在Node.js进程之外。换句话说,以下 例子不起作用:
$ node -e 'process.env.foo = "bar"' && echo $foo
答案 2 :(得分:0)
要控制台记录实际的环境变量,您需要首先对JSON进行字符串化: .....
.on('done', function (err, summary) {
if (err || summary.error) {
console.error('collection run encountered an error.');
}
else {
console.log('collection run completed.:');
console.log("summary environment :");
console.log(JSON.stringify(summary.environment))
}
});