Answers:
您可以使用规范的网址来运行一个规范
describe("MySpec", function() {
it('function 1', function() {
//...
})
it('function 2', function() {
//...
}
})
现在,您可以通过此网址运行整个规范,http://localhost:8888?spec=MySpec
并使用http://localhost:8888?spec=MySpec+function+1
describe("MySpec", ...)
而不是这个:describe("MySpec blah blah", ...)
?似乎正在进行子字符串匹配。
http://localhost:8888?spec=function+1
也应该工作(通常不需要指定MySpec)
fdescribe
,fit
使用Karma时,您只能使用fit
或fdescribe
(iit
以及ddescribe
2.1之前的Jasmine中)启用一项测试。
这只会运行Spec1
:
// or "ddescribe" in Jasmine prior 2.1
fdescribe('Spec1', function () {
it('should do something', function () {
// ...
});
});
describe('Spec2', function () {
it('should do something', function () {
// ...
});
});
这只会运行testA
:
describe('Spec1', function () {
// or "iit" in Jasmine prior 2.1
fit('testA', function () {
// ...
});
it('testB', function () {
// ...
});
});
iit
和ddescribe
是因果报应的补充,而不是茉莉花。
xit
和fit
混入it
很难阅读且容易出错的人吗?
对于任何对此绊脚石的人,可以从代码本身进行设置的更好的方法是使用此插件:https : //github.com/davemo/jasmine-only
它使您可以像这样在代码上设置规范独占性:
describe.only("MySpec", function() {
it('function 1', function() {
//...
})
it.only('function 2', function() {
//...
}
})
// This won't be run if there are specs using describe.only/ddescribe or it.only/iit
describe("Spec 2", function(){})
为了将其添加到Jasmine核心中已经进行了很长时间的讨论,请参见:https : //github.com/pivotal/jasmine/pull/309
如果您恰巧通过Karma / Testacular使用Jasmine,则应该已经可以访问ddescribe()
和iit()
有几种方法可以做到。
有:Jasmine的功能Focused Specs(2.2):http : //jasmine.github.io/2.2/focused_specs.html
集中规格将使其成为唯一运行的规格。任何声明合适的规范都将重点关注。
describe("Focused specs", function() {
fit("is focused and will run", function() {
expect(true).toBeTruthy();
});
it('is not focused and will not run', function(){
expect(true).toBeFalsy();
});
});
但是,我真的不喜欢编辑测试(fit和fdescribe)以选择性地运行它们的想法。我更喜欢使用像业力这样的测试运行程序,它可以使用正则表达式过滤掉测试。
这是使用grunt的示例。
$ grunt karma:dev watch --grep=mypattern
如果您使用的是gulp(这是我最喜欢的任务运行器),则可以将args 与yargs 传递到gulp -karma中,并通过设置karma的配置来匹配模式。
有点像这样:
var Args = function(yargs) {
var _match = yargs.m || yargs.match;
var _file = yargs.f || yargs.file;
return {
match: function() { if (_match) { return {args: ['--grep', _match]} } }
};
}(args.argv);
var Tasks = function() {
var test = function() {
return gulp.src(Files.testFiles)
.pipe(karma({ configFile: 'karma.conf.js', client: Args.match()}))
.on('error', function(err) { throw err; });
};
return {
test: function() { return test() }
}
}(Args);
gulp.task('default', ['build'], Tasks.test);
见我的要旨:https : //gist.github.com/rimian/0f9b88266a0f63696f21
因此,现在,我可以使用以下描述运行一个规范:
我的本地测试运行:(执行14个操作中的1个(跳过13个))
gulp -m 'triggers the event when the API returns success'
[20:59:14] Using gulpfile ~/gulpfile.js
[20:59:14] Starting 'clean'...
[20:59:14] Finished 'clean' after 2.25 ms
[20:59:14] Starting 'build'...
[20:59:14] Finished 'build' after 17 ms
[20:59:14] Starting 'default'...
[20:59:14] Starting Karma server...
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: All files matched by "/spec/karma.conf.js" were excluded.
INFO [Chrome 42.0.2311 (Mac OS X 10.10.3)]: Connected on socket hivjQFvQbPdNT5Hje2x2 with id 44705181
Chrome 42.0.2311 (Mac OS X 10.10.3): Executed 1 of 14 (skipped 13) SUCCESS (0.012 secs / 0.009 secs)
[20:59:16] Finished 'default' after 2.08 s
您可以预先创建所有规格,然后通过xdescribe
和禁用它们,xit
直到准备好测试它们为止。
describe('BuckRogers', function () {
it('shoots aliens', function () {
// this will be tested
});
xit('rescues women', function () {
// this won't
});
});
// this whole function will be ignored
xdescribe('Alien', function () {
it('dies when shot', function () {
});
});
这是一个带有实际示例的最简化的答案。即使在fdescribe中,您也可以使用它运行很少的代码块。f表示焦点。
同样,在仅描述的none fdescribe块中,可以通过将它们标记为合适来仅选择特定的it块。
请运行以下代码,并观察控制台日志,并阅读代码中的注释。阅读作者的文章也有帮助。https://davidtang.io/2016/01/03/controlling-which-tests-run-in-jasmine.html
//If you want to run few describe only add f so using focus those describe blocks and it's it block get run
fdescribe("focus description i get run with all my it blocks ", function() {
it("1 it in fdescribe get executed", function() {
console.log("1 it in fdescribe get executed unless no fit within describe");
});
it("2 it in fdescribe get executed", function() {
console.log("2 it in fdescribe get executed unless no fit within describe");
});
//but if you and fit in fdescribe block only the fit blocks get executed
fit("3 only fit blocks in fdescribe get executed", function() {
console.log("If there is a fit in fdescribe only fit blocks get executed");
});
});
describe("none description i get skipped with all my it blocks ", function() {
it("1 it in none describe get skipped", function() {
console.log("1 it in none describe get skipped");
});
it("2 it in none describe get skipped", function() {
console.log("2 it in none describe get skipped");
});
//What happen if we had fit in a none fdescribe block will it get run ? yes
fit("3 fit in none describe get executed too eventhough it;s just describe ", function() {
console.log("3 fit in none describe get executed too");
});
});