业力启动-传递参数 [英] karma start - passing parameters
问题描述
是否可以通过Karma命令行传递参数,然后在测试中的某个地方读取该参数?例如,这就是想要的东西:
Is there a way to pass a parameter thru the Karma command line and then read that somewhere in your tests? For instance, this is what want:
karma start -branding="clientX"
然后在我的规格中的某个地方,我需要访问此变量(我需要"clientX"值).
And then somewhere in my specs I would need to access this variable (I need the "clientX" value).
这有可能吗?
推荐答案
可以将参数传输到测试用例.这可能有点棘手.您可以做的是在测试套件中检查__karma__.config.args
:
It is possible to transmit parameters to test cases. It can be a bit tricky. What you can to do is check for __karma__.config.args
in you test suite:
it("get karma args", function () {
console.log(__karma__.config.args);
});
karma run
如果您想使用karma run
传递参数,那么以上就是您所需要的.
karma run
If you want to pass arguments with karma run
, then the above is all you need.
然后,如果先执行karma start
然后执行karma run -- --foo
,则应该在控制台上看到:
Then if you do karma start
and then karma run -- --foo
you should see on the console:
LOG: ['--foo']
请注意,传递给karma run
的参数是如何在__karma__.config.args
中结束的.还要注意,karma run -- --foo
中的第一个双破折号用于将Karma参数与客户端参数"分开. (karma start
没有相同的区别.)
Note how the argument passed to karma run
ended up in __karma__.config.args
. Also note that the first double-dash in karma run -- --foo
is there to separate Karma arguments from "client arguments" it is necessary. (karma start
does not make the same distinction.)
karma start
的工作原理不同.
如果使用由karma init
创建的默认karma.conf.js
,则将无法通过执行karma start --single-run --foo
来以这种方式传递参数.您需要修改karma.conf.js
以传递参数:
If you use a default karma.conf.js
created by karma init
, you won't be able to pass arguments in this way by doing karma start --single-run --foo
. You need to modify your karma.conf.js
to pass the arguments:
module.exports = function(config) {
config.set({
client: {
args: config.foo ? ["--foo"] : [],
},
如果运行karma start --single-run --foo
,则将获得与之前run
相同的输入.
If you run karma start --single-run --foo
, then you'll get the same input as with run
earlier.
如果我必须传递多个参数,我将扫描process.argv
以过滤出仅出于Karma利益的部分,然后将其余部分传递给args
而不是对每种可能性进行测试.
If I had to pass multiple arguments, I'd scan process.argv
to filter out those parts of it that are only for Karma's benefit and pass the rest to args
instead of testing for each possibility.
您可能从以上内容推断出,当您karma start --single-run --something
时,该参数最终在karma.conf.js
中以config.something
出现.
You may have inferred from the above that when you karma start --single-run --something
the argument ends up as config.something
in karma.conf.js
.
此示例已针对Karama 1.1.x和Karma 1.2.0进行了测试.它显示了与我上面讨论的获取命令行参数以通过client.args
传递的方法相同的方法.这对karma start
和karma run
均适用.我还添加了一种无需通过client.args
即可传递值的方法(这是branding
的示例).但是,此方法不适用于karma run
.
This example was tested against Karama 1.1.x and Karma 1.2.0. It shows the same method I've discussed above to get command line parameters to transit through client.args
. This works both with karma start
and karma run
. I also added a method to pass values without going through client.args
(that's the branding
example). However, this method does not work with karma run
.
karma.conf.js
:
module.exports = function(config) {
config.set({
basePath: '',
client: {
// Example passing through `args`.
args: config.foo ? ["--foo"] : [],
// It is also possible to just pass stuff like this,
// but this works only with `karma start`, not `karma run`.
branding: config.branding,
},
frameworks: ['jasmine'],
files: [
'test/**/*.js'
],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
test/test.js
:
it("get karma arg", function () {
console.log("BRANDING", __karma__.config.branding);
console.log("ARGS", __karma__.config.args);
});
如果运行karma start --single-run --foo --branding=q
,则会得到:
LOG: 'BRANDING', 'q'
LOG: 'ARGS', ['--foo']
如果启动Karma然后使用karma run -- --foo --branding=q
,则会得到:
If you start Karma and then use karma run -- --foo --branding=q
, you get:
LOG: 'BRANDING', undefined
LOG: 'ARGS', ['--foo', '--branding=q']
如上所述,使用karma run
时,所有内容都必须通过config.args
才能在测试中可见.
As mentioned above, when using karma run
, everything must go through config.args
to be visible in the test.
这篇关于业力启动-传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!