---
title: Test unitari
project: Total.js V5
version: 0.0.20
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/total-js-v5/0-0-20/reference/unit-tests/
updated: 2026-11-18T16:57:34.578Z
---

# Test unitari

> Il test runner di total5/test: Test.push(), Test.run(), Test.print(), Test.start(), Test.fake(), Test.restore(), Test.count e Test.items.

Il runner `Test` di `total5/test`. La guida [Test unitari](../../guides/unit-tests/) spiega come usarlo, con esempi.

## API

### Test

Type: `class`

Il test runner globale creato da `require('total5/test')`.

Returns: `Object`

Carica `total5` quando non è caricato e crea l'oggetto globale `Test` con `push()`, `run()`, `print()`, `start()`, `fake()`, `restore()`, `count` e `items`. Esegui il file di test con `node`, oppure gli script di test di un progetto con `total5 test` (vedi [Riga di comando](../../reference/command-line/)).

Example: Caricare
```js
require('total5/test');

Test.push('Numbers', function(next) {
	Test.print('sum', 1 + 1 === 2 ? null : 'Wrong sum');
	next();
});

Test.run();
```

### Test.push(name, fn)

Type: `method`

Aggiunge un gruppo di test.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome stampato come intestazione del gruppo. |
| `fn` | `Function` | yes |  | `function(next)` |

Returns: `undefined`

I gruppi vengono eseguiti nell'ordine in cui sono stati aggiunti. `fn` riceve `next` e deve chiamarlo quando il gruppo è terminato, anche dopo verifiche asincrone.

Example: Gruppo asincrono
```js
Test.push('Download', function(next) {
	RESTBuilder.GET('https://www.example.com').callback(function(err) {
		Test.print('GET', err);
		next();
	});
});
```

### Test.run([callback])

Type: `method`

Esegue tutti i gruppi uno alla volta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | `function()` chiamata invece di terminare il processo. |

Returns: `undefined`

Stampa l'intestazione di ogni gruppo, poi il numero di verifiche e il tempo. Senza `callback` il processo termina con codice di uscita 0 quando tutti i gruppi sono terminati.

Example: Eseguire e fermare il server
```js
Test.run(function() {
	F.exit();
});
```

### Test.print(message, [err])

Type: `method`

Stampa il risultato di una verifica.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `String` | yes |  | Il nome della verifica. |
| `err` | `Error|String` | no |  | Il motivo di un fallimento. |

Returns: `undefined`

Senza `err` (o con un valore falsy) stampa `[OK] message` e conta la verifica. Con `err` stampa `[FAIL] message`, genera l'errore (una stringa diventa un `Error`) e termina il processo con codice di uscita 1.

Example: Verificare un valore
```js
var value = 'Total.js'.slug();
Test.print('slug()', value === 'total-js' ? null : 'Unexpected: ' + value);
```

### Test.start(message)

Type: `method`

Stampa un'intestazione tra due linee divisorie.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `String` | yes |  | Il testo dell'intestazione. |

Returns: `undefined`

`Test.run()` la chiama con il nome di ogni gruppo. Chiamala tu per separare le parti di un gruppo lungo.

Example: Intestazione
```js
Test.start('Second part');
```

### Test.fake(name, action)

Type: `method`

Sostituisce un'azione con una finta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'azione. |
| `action` | `Object` | yes |  | La definizione dell'azione finta, lo stesso oggetto di `NEWACTION()`. |

Returns: `Object` L'azione finta registrata.

Mette da parte l'azione `name` (solo la prima volta fino a `Test.restore()`) e registra `action` con lo stesso nome tramite `NEWACTION()`. Le chiamate di `ACTION(name)` eseguono allora l'azione finta. Funziona anche per un nome che non ha un'azione.

Example: Sostituire e ripristinare
```js
NEWACTION('Users/read', {
	action: $ => $.callback('real')
});

Test.push('Fake', async function(next) {
	Test.fake('Users/read', { action: $ => $.callback('fake') });
	var value = await ACTION('Users/read').promise();
	Test.print('fake', value === 'fake' ? null : 'Unexpected: ' + value);
	Test.restore();
	value = await ACTION('Users/read').promise();
	Test.print('restore', value === 'real' ? null : 'Unexpected: ' + value);
	next();
});

Test.run();
```

### Test.count

Type: `property`

Il numero di verifiche stampate da `Test.print()`.

Returns: `Number`

`Test.run()` lo stampa alla fine come `Tests: <count>`.

Example: Leggere
```js
Test.run(function() {
	console.log('Checks:', Test.count);
	process.exit(0);
});
```

### Test.restore()

Type: `method`

Rimette le azioni originali.

Returns: `undefined`

Rimuove ogni azione finta registrata con `Test.fake()` e rimette le azioni originali. Un'azione finta di un nome che non aveva un'azione viene solo rimossa.

Example: Dopo un gruppo
```js
Test.push('Payments', function(next) {
	Test.fake('Payments/charge', { action: $ => $.callback({ paid: true }) });
	// ... the checks
	Test.restore();
	next();
});
```

### Test.items

Type: `property`

I gruppi aggiunti con `Test.push()`.

Returns: `Array`

Un array di oggetti `{ name, fn }`. `Test.run()` li esegue uno alla volta; l'array li conserva.

Example: Contare i gruppi
```js
console.log(Test.items.length, 'groups');
```
