Solidity unit testing using remix-tests -part 1

As a Remix IDE plugin

Aniket
Remix Project
5 min readFeb 7, 2020

--

If you are a Solidity developer and looking to write unit tests for your smart contract, you don’t need to learn any other language. You can test your contract in Solidity itself using the tool: remix-tests

Remix-tests is a tool to test Solidity smart contracts which is developed and maintained by Remix Team. remix-tests works underneath Remix IDE plugin “Solidity Unit Testing” and can be also used as a CLI tool. In this article we will go over its use as a Remix IDE plugin.

Solidity Unit Testing: A Remix IDE Plugin

Usually when developing in Solidity you’d click the Solidity button in the Environments section of the Remix homepage.

Solidity Environment on Remix Homepage

This will load a standard set of plugins for Solidity development.

Solidity Environment Plugins

You can also load the plugins as you need them from plugin manager.

Now double check icon will appear on the left side icon bar. Clicking it will land you into the Solidity unit testing plugin.

Solidity Unit Testing Plugin

This shows some initial information and two buttons: Generate test file and Run Tests

For now, these buttons are disabled. To activate them, you need to select a file from File explorers .

Generate & analyse test file

Generate test file button is used to generate a sample test file to start with. Click on it and you will get a new file in file manager .

Let’s observe this file:

  • Its name is suffixed with _test.sol, which is mandatory for a test file to get executed.
  • It imports remix_tests.sol. This import is injected by Remix which is responsible for providing Assert library features. Explicit import of this file is optional as it will be injected internally if not found while processing.
  • We see two contracts with multiple sets of functions. We can define our tests in multiple contracts to use the available feature. Like in this file, we can have a separate beforeAll method for a set of functions. beforeAll is one of the available special functions of the remix-tests. All special functions are as:
beforeEach()— Runs before each test
beforeAll() — Runs before all tests
afterEach() — Runs after each test
afterAll() — Runs after all tests

With remix-tests, we get a built-in Assert library and we can use it test output of defined functions. Inside the methods beforeAll and check1, we use some of the available methods of Assert library. You can see well explained documentation of all the functions on Remix IDE official docs.

You can also use a normal bool return of Solidity for testing like check2.

Write tests

Create a new sample file simple_storage.sol with this code:

pragma solidity >=0.4.22 <0.7.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;
}
}

Now we will write unit tests for it. Create a test file simple_storage_test.sol (suffixed with ‘_test.sol’)

Add pragma, inject remix_tests.sol, import the contract you want to test and declare your test contract. With these updates, the file will look like this:

pragma solidity >=0.4.22 <0.7.0;
import "remix_tests.sol";
import "./simple_storage.sol";

contract MyTest {
}

Time to create an instance of your contract to be tested so that you can access its methods. Lets use beforeEach for this. This way, before each test you will be getting a fresh instance of contract.

pragma solidity >=0.4.22 <0.7.0;
import "remix_tests.sol";
import "./simple_storage.sol";

contract MyTest {
SimpleStorage foo;

function beforeEach() public {
foo = new SimpleStorage();
}
}

Now we will write a test to check initial value of storedData set by the contract constructor. We can use theequal method of Assert library. Let’s write it inside function named initialValueShouldBe100

pragma solidity >=0.4.22 <0.7.0;
import "remix_tests.sol";
import "./simple_storage.sol";

contract MyTest {
SimpleStorage foo;

function beforeEach() public {
foo = new SimpleStorage();
}

function initialValueShouldBe100() public returns (bool) {
return Assert.equal(foo.get(), 100, "initial value is not correct");
}
}

You have successfully written your first test in Solidity. Now you can click on ‘Run Tests’ to run your test. It will show:

Tests Execution Result

Now lets use `set` method to update the value and test if value if updated.

pragma solidity >=0.4.22 <0.7.0;
import "remix_tests.sol";
import "./simple_storage.sol";

contract MyTest {
SimpleStorage foo;

function beforeEach() public {
foo = new SimpleStorage();
}

function initialValueShouldBe100() public returns (bool) {
return Assert.equal(foo.get(), 100, "initial value is not correct");
}

function valueIsSet200() public returns (bool) {
foo.set(200);
return Assert.equal(foo.get(), 200, "value is not 200");
}
}

Running tests will show:

Thus more tests can be added as per the contract implementation. Tests will run in the sequence they are defined in the file.

Write tests for your smart contract using this plugin and express your views/queries on Remix gitter channel.

Compilation & deployment parameter customisation and using remix-testsas a CLI is explained in part 2 of this blog.

Stay Tuned!

--

--