Interfaccia a riga di comando
remix-tests
remix-tests
è uno strumento che può essere usato come CLI (Command Line Interface) per eseguire i test unitari di Solidity. Questo è lo stesso strumento che funziona come libreria sotto il plugin Solidity Unit Testing
di Remix. Esso è disponibile su NPM come @remix-project/remix-tests
.
Iniziare
È possibile installarlo utilizzando NPM:
Come una dipendenza dev:
npm install --save-dev @remix-project/remix-tests
Come modulo NPM globale:
npm install -g @remix-project/remix-tests
Per confermare l’installazione, eseguire:
$ remix-tests version
0.1.36
La versione deve essere la stessa di quella su NPM.
Come si usa
È possibile vedere tutte le opzioni disponibili utilizzando il comando help
(aiuto).
$ remix-tests help
Usage: remix-tests [options] [command]
Options:
-V, --version output the version number
-c, --compiler <string> set compiler version (e.g: 0.6.1, 0.7.1 etc)
-e, --evm <string> set EVM version (e.g: petersburg, istanbul etc)
-o, --optimize <bool> enable/disable optimization
-r, --runs <number> set runs (e.g: 150, 250 etc)
-v, --verbose <level> set verbosity level (0 to 5)
-h, --help output usage information
Commands:
version output the version number
help output usage information
La struttura generale di un comando è:
$ remix-tests <options> <file/directory path>
Per eseguire tutti i file di test all’interno della directory `examples”
$ remix-tests examples/
Per eseguire un singolo file di test chiamato simple_storage_test.sol
all’interno della cartella examples
$ remix-tests examples/simple_storage_test.sol
NOTA: remix-tests
assume che il nome del file di test finisca con "_test.sol"
. Ad esempio simple_storage_test.sol
Esempio
Si consideri un semplice contratto di archiviazione chiamato simple_storage.sol
:
pragma solidity >=0.4.22 <=0.8.0;
contract SimpleStorage {
uint public storedData;
constructor() public {
storedData = 100;
}
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}
Il file di test simple_storage_test.sol
può essere:
pragma solidity >=0.4.22 <=0.8.0;
import "remix_tests.sol"; // injected by remix-tests
import "./simple_storage.sol";
contract MyTest {
SimpleStorage foo;
function beforeAll() public {
foo = new SimpleStorage();
}
function initialValueShouldBe100() public returns (bool) {
return Assert.equal(foo.get(), 100, "initial value is not correct");
}
function initialValueShouldNotBe200() public returns (bool) {
return Assert.notEqual(foo.get(), 200, "initial value is not correct");
}
function shouldTriggerOneFail() public {
Assert.equal(uint(1), uint(2), "uint test 1 fails");
Assert.notEqual(uint(1), uint(2), "uint test 2 passes");
}
function shouldTriggerOnePass() public {
Assert.equal(uint(1), uint(1), "uint test 3 passes");
}
}
L’esecuzione del file simple_storage_test.sol
produrrà un risultato come questo:
$ remix-tests simple_storage_test.sol
👁 :: Running remix-tests - Unit testing for solidity :: 👁
'creation of library remix_tests.sol:Assert pending...'
◼ MyTest
✓ Initial value should be100
✓ Initial value should not be200
✘ Should trigger one fail
✓ Should trigger one pass
3 passing (0.282s)
1 failing
1) MyTest: Should trigger one fail
error: uint test 1 fails
expected value to be equal to: 2
returned: 1
Contesto del compilatore personalizzato
La maggior parte delle opzioni di `remix-tests” sono presenti per definire un contesto di compilatore personalizzato. Con un contesto di compilatore personalizzato esteso, l’esecuzione del file di test sopra avverrà così:
$ remix-tests --compiler 0.7.4 --evm istanbul --optimize true --runs 300 simple_storage_test.sol
👁 :: Running remix-tests - Unit testing for solidity :: 👁
[14:03:18] info: Compiler version set to 0.7.4. Latest version is 0.8.0
[14:03:18] info: EVM set to istanbul
[14:03:18] info: Optimization is enabled
[14:03:18] info: Runs set to 300
Loading remote solc version v0.7.4+commit.3f05b770 ...
'creation of library remix_tests.sol:Assert pending...'
◼ MyTest
✓ Initial value should be100
✓ Initial value should not be200
✘ Should trigger one fail
✓ Should trigger one pass
3 passing (0.316s)
1 failing
1) MyTest: Should trigger one fail
error: uint test 1 fails
expected value to be equal to: 2
returned: 1
Remember, custom compiler version will require internet connection to load compiler.
Come soluzione CI
remix-tests
può essere usato anche per i test di integrazione continua (CI).
Per un esempio di implementazione, vedere il contratto Su Squares e Travis build che usa remix-tests
per l’integrazione continua.