Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Unit testing

Omkara edited this page Sep 16, 2018 · 4 revisions

Unit testing solidity smart contracts with remix-tests

Step 1: Create a solidity file simple_storage.sol
pragma solidity ^0.4.7;
contract SimpleStorage {
  uint public storedData;

  function SimpleStorage() public {
    storedData = 100;
  }

  function set(uint x) public {
    storedData = x;
  }

  function get() public view returns (uint retVal) {
    return storedData;
  }

}
Step 2: Create another solidity file and name it simple_storage_test.sol
pragma solidity ^0.4.24;
import "./simple_storage.sol";

contract StorageTest {
  SimpleStorage foo;

  function beforeAll() {
    foo = new SimpleStorage();
  }

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

  function initialValueShouldBe200() public constant returns (bool) {
    //return Assert.equal(foo.get(), 200, "initial value is not correct");
    return foo.get() == 200;
  }
}

contract Storage2Test {
  SimpleStorage foo;
  uint i = 0;

  function beforeEach() {
    foo = new SimpleStorage();
    if (i == 1) {
      foo.set(200);
    }
    i += 1;
  }

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

  function initialValueShouldBe200() public constant returns (bool) {
    //return Assert.equal(foo.get(), 200, "initial value is not correct");
    return foo.get() == 200;
  }

}
Step 3: Save or click Run tests button to run unit tests written on simple_storage_test.sol

Unit testing screenshot