58 karma-runner
我使用业力进行跑步测试.我有很多测试并且运行所有测试的过程非常缓慢.我想只运行一次测试才能花更少的时间,因为所有测试运行大约10分钟.
可能吗 ?
谢谢.
Dan*_*.K. 72
如果您使用的是Karma/Jasmine堆栈,请使用:
fdescribe("when ...", function () { // to [f]ocus on a single group of tests
fit("should ...", function () {...}); // to [f]ocus on a single test case
});
Run Code Online (Sandbox Code Playgroud)
......和:
xdescribe("when ...", function () { // to e[x]clude a group of tests
xit("should ...", function () {...}); // to e[x]clude a test case
});
Run Code Online (Sandbox Code Playgroud)
当你在Karma/Mocha上时:
describe.only("when ...", function () { // to run [only] this group of tests
it.only("should ...", function () {...}); // to run [only] this test case
});
Run Code Online (Sandbox Code Playgroud)
......和:
describe.skip("when ...", function () { // to [skip] running this group of tests
it.skip("should ...", function () {...}); // to [skip] running this test case
});
Run Code Online (Sandbox Code Playgroud)
a) 您可以将描述单个文件的模式作为命令行参数传递给 karma start 命令:
# build and run all tests
$ karma start
# build and run only those tests that are in this dir
$ karma start --grep app/modules/sidebar/tests
# build and run only this test file
$ karma start --grep app/modules/sidebar/tests/animation_test.js
Run Code Online (Sandbox Code Playgroud)
来源:https : //gist.github.com/KidkArolis/fd5c0da60a5b748d54b2
b) 您可以使用 Gulp(或 Grunt 等)任务为您启动 Karma。这使您可以更灵活地执行 Karma。例如,您可以将自定义命令行参数传递给这些任务。如果您想实现仅执行更改的测试的监视模式,此策略也很有用。(Karma 监视模式将执行所有测试。)另一个用例是在执行提交之前仅对具有本地更改的文件执行测试。另请参阅下面的 Gulp 示例。
c) 如果您使用 VisualStudio,您可能希望将外部工具命令添加到解决方案资源管理器的上下文菜单中。这样,您可以从该上下文菜单而不是使用控制台开始测试。另见
如何在 Visual Studio 中执行自定义文件特定命令/任务?
示例 Gulp 文件
//This gulp file is used to execute the Karma test runner
//Several tasks are available, providing different work flows
//for using Karma.
var gulp = require('gulp');
var karma = require('karma');
var KarmaServerConstructor = karma.Server;
var karmaStopper = karma.stopper;
var watch = require('gulp-watch');
var commandLineArguments = require('yargs').argv;
var svn = require('gulp-svn');
var exec = require('child_process').exec;
var fs = require('fs');
//Executes all tests, based on the specifications in karma.conf.js
//Example usage: gulp all
gulp.task('all', function (done) {
var karmaOptions = { configFile: __dirname + '/karma.conf.js' };
var karmaServer = new KarmaServerConstructor(karmaOptions, done);
karmaServer.on('browsers_change', stopServerIfAllBrowsersAreClosed); //for a full list of events see http://karma-runner.github.io/1.0/dev/public-api.html
karmaServer.start();
});
//Executes only one test which has to be passed as command line argument --filePath
//The option --browser also has to be passed as command line argument.
//Example usage: gulp single --browser="Chrome_With_Saved_DevTools_Settings" --filePath="C:\myTest.spec.js"
gulp.task('single', function (done) {
var filePath = commandLineArguments.filePath.replace(/\\/g, "/");
var karmaOptions = {
configFile: __dirname + '/karma.conf.js',
action: 'start',
browsers: [commandLineArguments.browser],
files: [
'./Leen.Managementsystem/bower_components/jquery/dist/jquery.js',
'./Leen.Managementsystem/bower_components/globalize/lib/globalize.js',
{ pattern: './Leen.Managementsystem/bower_components/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/mockFactory.js', included: false },
{ pattern: './Leen.Managementsystem/App/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/*.js', included: false },
{ pattern: filePath, included: false },
'./Leen.Managementsystem.Tests/App/test-main.js',
'./switchKarmaToDebugTab.js' //also see /sf/ask/2311647481/
]
};
var karmaServer = new KarmaServerConstructor(karmaOptions, done);
karmaServer.on('browsers_change', stopServerIfAllBrowsersAreClosed);
karmaServer.start();
});
//Starts a watch mode for all *.spec.js files. Executes a test whenever it is saved with changes.
//The original Karma watch mode would execute all tests. This watch mode only executes the changed test.
//Example usage: gulp watch
gulp.task('watch', function () {
return gulp //
.watch('Leen.Managementsystem.Tests/App/**/*.spec.js', handleFileChanged)
.on('error', handleGulpError);
function handleFileChange(vinyl) {
var pathForChangedFile = "./" + vinyl.replace(/\\/g, "/");
var karmaOptions = {
configFile: __dirname + '/karma.conf.js',
action: 'start',
browsers: ['PhantomJS'],
singleRun: true,
files: [
'./Leen.Managementsystem/bower_components/jquery/dist/jquery.js',
'./Leen.Managementsystem/bower_components/globalize/lib/globalize.js',
{ pattern: './Leen.Managementsystem/bower_components/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/mockFactory.js', included: false },
{ pattern: './Leen.Managementsystem/App/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/*.js', included: false },
{ pattern: pathForChangedFile, included: false },
'./Leen.Managementsystem.Tests/App/test-main.js'
]
};
var karmaServer = new KarmaServerConstructor(karmaOptions);
karmaServer.start();
}
});
//Executes only tests for files that have local changes
//The option --browser has to be passed as command line arguments.
//Example usage: gulp localChanges --browser="Chrome_With_Saved_DevTools_Settings"
gulp.task('localChanges', function (done) {
exec('svn status -u --quiet --xml', handleSvnStatusOutput);
function handleSvnStatusOutput(error, stdout, stderr) {
if (error) {
throw error;
}
var changedJsFiles = getJavaScriptFiles(stdout);
var specFiles = getSpecFiles(changedJsFiles);
if(specFiles.length>0){
console.log('--- Following tests need to be executed for changed files: ---');
specFiles.forEach(function (file) {
console.log(file);
});
console.log('--------------------------------------------------------------');
} else{
console.log('Finsihed: No modified files need to be tested.');
return;
}
var files = [
'./Leen.Managementsystem/bower_components/jquery/dist/jquery.js',
'./Leen.Managementsystem/bower_components/globalize/lib/globalize.js',
{ pattern: './Leen.Managementsystem/bower_components/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/mockFactory.js', included: false },
{ pattern: './Leen.Managementsystem/App/**/*.js', included: false },
{ pattern: './Leen.Managementsystem.Tests/App/test/*.js', included: false }];
specFiles.forEach(function (file) {
var pathForChangedFile = "./" + file.replace(/\\/g, "/");
files = files.concat([{ pattern: pathForChangedFile, included: false }]);
});
files = files.concat([ //
'./Leen.Managementsystem.Tests/App/test-main.js', //
'./switchKarmaToDebugTab.js'
]);
var karmaOptions = {
configFile: __dirname + '/karma.conf.js',
action: 'start',
singleRun: false,
browsers: [commandLineArguments.browser],
files: files
};
var karmaServer = new KarmaServerConstructor(karmaOptions, done);
karmaServer.on('browsers_change', stopServerIfAllBrowsersAreClosed);
karmaServer.start();
}
});
function getJavaScriptFiles(stdout) {
var jsFiles = [];
var lines = stdout.toString().split('\n');
lines.forEach(function (line) {
if (line.includes('js">')) {
var filePath = line.substring(9, line.length - 3);
jsFiles.push(filePath);
}
});
return jsFiles;
}
function getSpecFiles(jsFiles) {
var specFiles = [];
jsFiles.forEach(function (file) {
if (file.endsWith('.spec.js')) {
specFiles.push(file);
} else {
if (file.startsWith('Leen\.Managementsystem')) {
var specFile = file.replace('Leen\.Managementsystem\\', 'Leen.Managementsystem.Tests\\').replace('\.js', '.spec.js');
if (fs.existsSync(specFile)) {
specFiles.push(specFile);
} else {
console.error('Missing test: ' + specFile);
}
}
}
});
return specFiles;
}
function stopServerIfAllBrowsersAreClosed(browsers) {
if (browsers.length === 0) {
karmaStopper.stop();
}
}
function handleGulpError(error) {
throw error;
}
Run Code Online (Sandbox Code Playgroud)
VisualStudio 中 ExternalToolCommand 的示例设置:
标题:使用 Chrome 运行 Karma
命令:cmd.exe
参数:/c gulp single --browser="Chrome_With_Saved_DevTools_Settings" --filePath=$(ItemPath)
初始目录:$(SolutionDir)
使用输出窗口:true
如果你想用 angular 运行 karma 测试,你只需要修改你的test.ts文件。
找线 const context = require.context('./', true, /\.spec\.ts$/);
如果要将your.component.spec.ts修改行运行到:const context = require.context('./', true, /your\.component\.spec\.ts$/);
我知道两种方法:
最简单的方法是将vscode-test-explorer扩展及其子项angular-karma-test-explorer和jasmine-test-adapter一起使用,如果需要,您将获得一份当前测试列表,以逐个运行:
对我来说,我是不能够使用扩展方式,因为这个错误,所以我结束了修改test.ts文件(如说明这里通过沙市),只是在这里巩固答案,默认情况下这个样子的:
const context = require.context('./', true, /\.spec\.ts$/);
Run Code Online (Sandbox Code Playgroud)
您应该修改它的RegExp以匹配您要测试的文件,例如,如果您要测试一个名为“ my.single.file.custom.name.spec.ts”的文件,它将看起来像这样:
const context = require.context('./', true, /my\.single\.file\.custom\.name\.spec\.ts$/);
Run Code Online (Sandbox Code Playgroud)
有关require参数的更多详细信息,请参见其Wiki。
当前有一个开放的问题可以改善他们的当前行为,您可以在他们的github页面(https://github.com/karma-runner/karma/issues/1507)上关注他们的进展。
更改您的 karma conf,使其仅包含您想要运行的测试,而不是完整目录。
文件内:[...]
如果您需要/想要在 chrome 中调试测试以避免 js 缩小,您可能需要注释预处理器。
| 归档时间: |
|
| 查看次数: |
32249 次 |
| 最近记录: |