2016-02-04 73 views
12

Voglio passare il flag --grep=PATTERN a karma run, in modo che venga eseguito solo un test specifico. Ma se avvio il mio server karma con karma start karma.conf.js, tutti i test delle unità Jasmine vengono eseguiti immediatamente. Dato che ho centinaia di test, questo richiede troppo tempo.Come eseguire il server Karma senza avviare alcun test Jasmine

Domanda: Come posso avviare il server karma e non eseguire test, ma eseguire solo test quando invoco karma run?

+0

Questo non è nemmeno quasi una risposta, ma io' sono solo curioso Corro più di 600 test piuttosto pesanti che includono casualmente alcune operazioni angolari, DOM e attese asincrone in meno di 2 secondi su una macchina virtuale abbastanza lenta (1 core, 6 GB di RAM, Win7). Non hai pensato di controllare le prestazioni dei test? E potresti definire quanto tempo impiega per eseguire i tuoi test? –

+1

Il codice di test prevede operazioni websocket; Ho uno schema di messaggistica personalizzato sviluppato su ws che sono le unità sotto test quindi non c'è modo di deriderle; i client parlano con un server che ha accumulato ritardi e ritardi incorporati nel client per simulare determinati comportamenti di rete; vale a dire condizioni di gara se si tenta di eseguire un'operazione da due client contemporaneamente ecc; sfortunatamente quei test devono anche essere eseguiti in serie per evitare gare con il server di backend di test. Ad ogni modo, questo non è correlato alla domanda. Trovo difficile credere che karma + gelsomino non fornisca un flusso di lavoro TDD – Dyna

+1

FYI; Sono passato a mocha e sto usando il moka runner basato su nodeJS, che mi consente di eseguire un singolo test passando il flag '--grep' sul livello di cmdline. La storia Karma + gelsomino rimane triste :( – Dyna

risposta

2

Sembra che ciò che si preferisce sia eseguire un singolo test alla volta tramite karma/gelsomino usando il terminale. Se questo è corretto, questo è quello che faccio per eseguire test su singoli file.

Nota: Io uso il gulp per caricare il Karma, ma questo dovrebbe funzionare solo per usare il karma globale.

Ecco il mio metodo gulp se si sceglie di usarlo.

gulp.task('karma', function (done) { 
    karma.start({ 
    configFile: __dirname + '/karma.conf.js' 
    }, done); 
}); 

Nei miei karma.config.js

module.exports = function(config) { 

    config.set({ 

    basePath: '', 

    frameworks: ['jasmine'], 

    // Note the **setFilesUpForTesting** function below. Just change this. 
    files: setFilesUpForTesting(), 

    ... the rest of file 
} // end of module!!!!! 

-> Ora sotto il modulo, negli stessi karma.config.js file di aggiungere il seguente

// ------------------------------------------------------------------ > 
// Below code is for running either single or all test files. 
// To run a single test, just pass in the test file name like so... 

// If the test file name was rs-test-me-now_test.js 
// To spool up Karma you would enter 

// gulp karma --single-test=rs-test-me-now 

// To just run all tests 
// gulp karma 

var fixedPath = [ 
    'public/js/jquery.js', 
    'public/js/jquery-ui.min.js', 
    'public/js/foundation.min.js', 
    'public/js/angular.min.js', 
    'public/js/angular-mocks.js', 
    'public/js/angular-route.min.js', 
    'public/js/angular-sanitize.min.js', 
    'public/js/angular-cookies.js' 
    // all the files you want to include when tests run. I removed most of mine, but left some for reference. 
]; 

var baseTestPath = 'public/test/'; // This is the path from root to your test/spec folder that contains all your tests. 

function setFilesUpForTesting(){ 
    fixedPath.push(testPath()); 
    return fixedPath; 
} 

function testPath(){ 
    return singleTestPath() || fullTestPath(); 
} 

function fullTestPath(){ 
    // If your root folder was butter, and all your tests were labeled 
    // like my_file_spec.js, and all your specs were in a folder under root 
    // called bread, then the path below would be 
    // 'butter/bread/**/*_spec.js' Got it? 
    return 'public/test/**/*_test.js'; // change this to suit your path. 
} 

function singleTestPath(){ 
    var passedArgument = process.argv.filter(function(item){ if(/--single-test=/.test(item)){return item; }}); 
    if(isEmpty(passedArgument)){ return false; } 
    return baseTestPath + '**/*' + fileName(passedArgument) + '_test.js'; 
    // change above to fit your path. 
} 

function fileName(argument){ 
    if(!argument){ return; } 
    return argument[0].replace('--single-test=', ''); // Change single-test if you want to. 
} 

function isEmpty(array){ 
    return array.length === 0; 
} 

Quando voglio eseguire i test, eseguo quindi quanto segue

Per i test di file singoli sorso karma --single-test = test-me-now

// when test-me-now_test.js is the file name. 
// and root/pulblic/test/ has all my test files and test folders. 
// or root/bread/butter/ and file is called test-me-now_spec.js as in the other example I gave above. 

Spero che questo aiuti qualcuno. Fai domande se non riesci a farlo funzionare.

UPDATE

ho appena usato il karma dalla riga di comando e questo funziona come detto sopra quando si utilizza sorso pure.

Se il karma è accessibile dalla riga di comando ora si può fare questo se si sceglie di implementare il file sopra le karma.config.js ...

Nota: devo usare il karma iniziare non corsa,

karma start (will run all tests) 
// or for a single test file 
karma start --single-test=my-test-file-name // but without the _test.js or _spec.js whatever you call your files. I had reasons for doing this, but you can change the functions above to accept the complete file name if needed. 

Nota:

È possibile als o cambia la parola dopo "-" in --single-test = assicurati di aggiornare le informazioni karma.config.js che ho condiviso.

+0

Per chiunque trovi che 'start' funziona ancora tutto test, controlla di non avere un elemento 'browsers' nel tuo' karma.config.js'. Sembra che questo possa far sì che esegua tutti i test e poi esca. – stripybadger

1

Bene per far funzionare il server è abbastanza facile. Tutto quello che devi fare è modificare il tuo file karma.conf.js. Se aggiungi quanto segue, avvierà il server senza eseguire nessuno dei test.

singleRun : true

Ora, quando si esegue che sarà pronto il server, ma in realtà non eseguire alcun test. Ti fornirà anche una finestra del browser pronta per essere avviata. Il test debug inizierà a eseguire i test.

enter image description here

non ho mai provato a eseguire test individuali, ma ho appena verificato che l'utilizzo di karma run da sola di loro tutti i trigger sul mio server. Quindi se si limita il set con karma run allora si dovrebbe essere ordinato.

0

Rispondendo a me stesso, poiché ora esiste una soluzione: test di messa a fuoco (https://jasmine.github.io/2.1/focused_specs.html).

È possibile sostituire il describe() con fdescribe() per eseguire una singola suite di test, o per l'esecuzione di un singolo test è possibile sostituire il it() con un fit():

fit("This test is executed on its own because it is prefix with f (focus test)", (done) => { 
    done(); 
}); 
Problemi correlati