2014-10-24 20 views
32

Tôi sử dụng nghiệp để chạy thử nghiệm. Tôi có nhiều bài kiểm tra và chạy tất cả các bài kiểm tra là quá trình rất chậm. Tôi muốn chạy thử nghiệm duy nhất để dành ít thời gian hơn, bởi vì tất cả các thử nghiệm chạy khoảng 10 phút.Karma chạy thử nghiệm duy nhất

Có thể không?

Cảm ơn.

+2

Nếu bạn sử dụng hoa nhài bạn có thể sử dụng ddescribe/iit. –

+0

Vấn đề Karma liên quan: https://github.com/karma-runner/karma/issues/1507#issuecomment-320383049 – Stefan

Trả lời

0

Thay đổi nghiệp của bạn thành chỉ bao gồm thử nghiệm bạn muốn chạy thay vì thư mục đầy đủ.

Bên trong tập tin: [...]

Bạn có thể muốn bình luận các preprocessors nếu bạn cần/muốn gỡ lỗi thử nghiệm của bạn trong chrome để tránh việc js của bạn giảm bớt.

2

Thay đổi() thành iit() sẽ hoạt động để chạy thử nghiệm đơn lẻ. Ngoài ra, tương tự, đối với mô tả() khối, chúng tôi có thể sử dụng ddescribe()

+1

tùy thuộc vào phiên bản nghiệp của bạn, bạn có thể cần sử dụng 'fdescribe()' và 'fit()' làm brendan chỉ ra – mgojohn

13

Cập nhật: nghiệp đã thay đổi.

Bây giờ hãy sử dụng fit() và fdescribe()

f là viết tắt của tiêu điểm!

+0

ahhh 'f' là viết tắt của' focus' ... kinda câm nhưng tôi sẽ lấy nó lol –

48

Nếu bạn đang sử dụng Karma/Jasmine ngăn xếp, sử dụng:

fdescribe("when ...", function() { // to [f]ocus on a single group of tests 
    fit("should ...", function() {...}); // to [f]ocus on a single test case 
}); 

... và:

xdescribe("when ...", function() { // to e[x]clude a group of tests 
    xit("should ...", function() {...}); // to e[x]clude a test case 
}); 

Khi bạn đang ở trên Karma/Mocha:

describe.only("when ...", function() { // to run [only] this group of tests 
    it.only("should ...", function() {...}); // to run [only] this test case 
}); 

... và:

describe.skip("when ...", function() { // to [skip] running this group of tests 
    it.skip("should ...", function() {...}); // to [skip] running this test case 
}); 
+10

Nhưng nếu một cộng tác viên đã gỡ lỗi một cái gì đó và đổi tên thành một vài thử nghiệm để thu hẹp vấn đề quên làm như vậy và cam kết ? Tôi tìm thấy nó ngu ngốc để chỉnh sửa các thử nghiệm qua lại để tập trung và không tập trung nó. Không có cách nào để sử dụng CLI chỉ để được như thế, "chạy thử nghiệm này, và thử nghiệm này chỉ"? –

+0

Thông thường, có một số trường hợp kiểm tra và nhóm trong một tệp và đôi khi bạn chỉ cần chạy một trong số chúng. Tính năng "tiêu điểm" là "phải có" trong bất kỳ thư viện kiểm tra đơn vị nào. Ngoài ra, bạn có thể thêm "git-hook" để kiểm tra '.only' hoặc' fit' code xuất hiện và từ chối commit nếu tìm thấy. –

+0

Tôi thấy - điểm tốt! Nhưng tôi không thể kiểm tra một git-móc vào VCS, tôi có thể? –

1

a) Bạn có thể vượt qua một mô hình mô tả tập tin duy nhất của bạn như là đối số dòng lệnh để lệnh nghiệp bắt đầu:

# 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 

Nguồn: https://gist.github.com/KidkArolis/fd5c0da60a5b748d54b2

b) Bạn có thể sử dụng một Gulp (hoặc Grunt ect.) Nhiệm vụ bắt đầu Karma cho bạn. Điều này giúp bạn linh hoạt hơn về cách thực thi Karma. Ví dụ, bạn có thể chuyển các đối số dòng lệnh tùy chỉnh cho các tác vụ đó. Chiến lược này cũng hữu ích nếu bạn muốn triển khai chế độ xem chỉ thực hiện các kiểm tra đã thay đổi. (Chế độ đồng hồ Karma sẽ thực hiện tất cả các thử nghiệm.) Một trường hợp sử dụng khác sẽ chỉ thực hiện các kiểm tra cho các tệp có thay đổi cục bộ trước khi bạn thực hiện một cam kết. Cũng xem các ví dụ về Gulp bên dưới.

c) Nếu bạn sử dụng VisualStudio, bạn có thể muốn thêm lệnh công cụ bên ngoài vào menu ngữ cảnh của trình khám phá giải pháp. Bằng cách này, bạn có thể bắt đầu thử nghiệm từ trình đơn ngữ cảnh thay vì sử dụng bảng điều khiển.Xem thêm nộp

How to execute custom file specific command/task in Visual Studio?

Ví dụ 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 https://stackoverflow.com/questions/33023535/open-karma-debug-html-page-on-startup 
     ] 
    }; 

    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) { 

lỗi ném; }

thiết lập Ví dụ về ExternalToolCommand trong VisualStudio:

Tiêu đề: Run Karma sử dụng Chrome

Command: cmd.exe

Arguments:/c ngụm đơn --browser =" Chrome_With_Saved_DevTools_Settings "--filePath = $ (ItemPath)

Thư mục ban đầu: $ (SolutionDir)

Sử dụng cửa sổ Output: true

Các vấn đề liên quan