Una buona vita inizia con il test
Un programmatore che non scrive casi di test automatici non è un buon ingegnere di test. Incoraggiamo tutti i progetti a stabilire fin dall'inizio casi di test automatizzati completi. Con lo sviluppo del progetto, l'investimento iniziale sarà ricompensato centinaia di volte.
Continuiamo l'esempio della sezione precedente e diamo un'occhiata a come usare fibjs per scrivere casi di test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21const http = require('http');
const path = require('path');
var hello_server = {
'/:name(fibjs.*)': (req, name) => {
req.response.write('hello, ' + name + '. I love you.');
},
'/:name': (req, name) => {
req.response.write('hello, ' + name);
}
};
var root_server = {
'/hello': hello_server,
'/bonjour': hello_server,
'*': http.fileHandler(path.join(__dirname, 'web'))
};
var svr = new http.Server(8080, root_server);
svr.start();
Una cornice di prova vuota
Iniziamo con un framework di test di base:
1
2
3
4
5
6
7
8
9
10var test = require('test');
test.setup();
describe('hello, test', () => {
it('a empty test', () => {
});
});
test.run();
Salva con nome in test.js
seguito, nella riga di comando fibjs test.js
, viene visualizzato il seguente output, è stato scritto un framework di test di base.
1
2
3
4 hello, test
√ a empty test
√ 1 tests completed (0ms)
Avvia server di prova
Poiché dobbiamo testare il server http, dobbiamo prima avviare il server. Il test case invierà una richiesta al server, quindi testerà il risultato della richiesta per determinare se il server soddisfa i requisiti:
1
2
3
4
5
6
7
8
9
10
11
12
13
14var test = require('test');
test.setup();
var http = require('http');
describe('hello, test', () => {
it('hello, fibjs', () => {
var r = http.get('http://127.0.0.1:8080/hello/fibjs');
assert.equal(r.statusCode, 200);
assert.equal(r.data.toString(), 'hello, fibjs. I love you.');
});
});
test.run();
In questo codice, giudichiamo se la logica del server è normale verificando se il risultato di http.get è il risultato che ci aspettiamo. Secondo questo esempio, possiamo completare rapidamente una serie di test e allo stesso tempo abbiamo ottimizzato il codice:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30var test = require('test');
test.setup();
var http = require('http');
function test_get(url, rep) {
var r = http.get('http://127.0.0.1:8080' + url);
assert.equal(r.statusCode, 200);
assert.equal(r.data.toString(), rep);
}
describe('hello, test', () => {
it('hello, fibjs', () => {
test_get('/hello/fibjs', 'hello, fibjs. I love you.');
});
it('hello, fibjs*', () => {
test_get('/hello/fibjs-great', 'hello, fibjs-great. I love you.');
});
it('hello, JavaScript', () => {
test_get('/hello/JavaScript', 'hello, JavaScript');
});
it('hello, v8', () => {
test_get('/hello/v8', 'hello, v8');
});
});
test.run();
Gestione di gruppo di casi d'uso
Aggiungiamo bonjour test. Sebbene bonjour e hello siano lo stesso insieme di servizi, poiché il percorso è cambiato, dobbiamo anche verificare la correttezza del servizio. Questa volta, per gestire al meglio i casi d'uso, abbiamo eseguito i casi di test Raggruppando, allo stesso tempo, poiché il contenuto di test di hello e bonjour è lo stesso, abbiamo ottimizzato nuovamente il codice e testato i due set di servizi con lo stesso set di codici:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37var test = require('test');
test.setup();
var http = require('http');
function test_get(url, rep) {
var r = http.get('http://127.0.0.1:8080' + url);
assert.equal(r.statusCode, 200);
assert.equal(r.data.toString(), rep);
}
describe('hello, test', () => {
function test_hello(hello) {
describe(hello + ' test', () => {
it('fibjs', () => {
test_get('/' + hello + '/fibjs', 'hello, fibjs. I love you.');
});
it('fibjs*', () => {
test_get('/' + hello + '/fibjs-great', 'hello, fibjs-great. I love you.');
});
it('JavaScript', () => {
test_get('/' + hello + '/JavaScript', 'hello, JavaScript');
});
it('v8', () => {
test_get('/' + hello + '/v8', 'hello, v8');
});
});
}
test_hello('hello');
test_hello('bonjour');
});
test.run();
Attraverso il raggruppamento dei casi d'uso, possiamo visualizzare i risultati dei test in modo più chiaro ed è anche facile saltare e testare un gruppo di casi d'uso individualmente, accelerando la velocità di sviluppo e test. Questi sono i risultati di questa tornata di test:
1
2
3
4
5
6
7
8
9
10
11
12
13 hello, test
hello test
√ fibjs
√ fibjs*
√ JavaScript
√ v8
bonjour test
√ fibjs
√ fibjs*
√ JavaScript
√ v8
√ 8 tests completed (3ms)
Secondo il nostro design del server, abbiamo anche una serie di servizi di file statici.Secondo l'esempio sopra, credo che tu possa scrivere rapidamente questa parte dei casi di test.
Test con un clic
Dopo l'introduzione di cui sopra, possiamo stabilire rapidamente casi di test. Ma per utilizzare questo script di test, devi prima avviare il server, è molto semplice, vogliamo eseguire test.js
per completare direttamente il test. Possiamo utilizzare il seguente codice per ottenere:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43var test = require('test');
test.setup();
var http = require('http');
var coroutine = require('coroutine');
coroutine.start(() => {
run('./main.js');
});
coroutine.sleep(100);
function test_get(url, rep) {
var r = http.get('http://127.0.0.1:8080' + url);
assert.equal(r.statusCode, 200);
assert.equal(r.data.toString(), rep);
}
describe('hello, test', () => {
function test_hello(hello) {
describe(hello + ' test', () => {
it('fibjs', () => {
test_get('/' + hello + '/fibjs', 'hello, fibjs. I love you.');
});
it('fibjs*', () => {
test_get('/' + hello + '/fibjs-great', 'hello, fibjs-great. I love you.');
});
it('JavaScript', () => {
test_get('/' + hello + '/JavaScript', 'hello, JavaScript');
});
it('v8', () => {
test_get('/' + hello + '/v8', 'hello, v8');
});
});
}
test_hello('hello');
test_hello('bonjour');
});
process.exit(test.run());
Dalla riga 6 alla 10 di questo codice, abbiamo aggiunto un main.js
codice di avvio del paragrafo e aspettiamo un po', quindi riavviamo i test.
Controllo della copertura del codice
I buoni casi di test devono considerare che i casi di test devono coprire ogni ramo dell'azienda per garantire che l'attività venga eseguita correttamente.In questo momento, i controlli di copertura del codice possono essere utilizzati per determinare se il test è completo.
Questo processo è molto semplice, basta aggiungere il parametro --cov durante il test:
1fibjs --cov test
Al termine del test verrà generato un file di log di fibjs-xxxx.lcov nella directory corrente, a questo punto è necessario analizzare il log e generare un report:
1fibjs --cov-process fibjs-xxxx.lcov out
Quindi puoi generare una serie di rapporti di analisi nella directory out. Entra nel catalogo per verificare, puoi vedere la pagina seguente:
Puoi vedere che
main.js
la copertura del codice ha raggiunto il 100%, il che significa che il test copre completamente la logica di business. Fare clic più main.js
lontano è possibile visualizzare un rapporto più dettagliato.