remix-project mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
remix-project/docs/unittesting.md

4.1 KiB

Unit Testing Plugin

Click the double check icon to get to the "Solidity Unit Testing" plugin.

If you haven't used this plugin before and are not seeing double check icon, you have to activate it from Remix plugin manager.

Go to the plugin manager (by click the plug icon) and load up the unit testing plugin.

Now double check icon will appear on the left side icon bar. Clicking on icon will load the unit testing module in the side panel.

Generate Test File

Click the button Generate test file to create a new solidity file in the current folder suffixed with _test. This file contains the minimum you need for running unit testing.

Write Tests

Write tests to check the functionality of your contract. Remix injects a built-in assert library which can be used for testing. Visit the library documentation here.

Apart from this, Remix allows usage of some special functions to make testing more structural. They are:

  • beforeEach() - Runs before each test
  • beforeAll() - Runs before all tests
  • afterEach() - Runs after each test
  • afterAll() - Runs after all tests

To get started, see this simple example.

Run Tests

Click the button "Run tests" to executes all tests whose box has been checked below (by default all). The execution is run in a separate environment and the result is displayed below.

Customization

Remix facilitates users with various types of customizations to test a contract properly.

1. Custom Compiler Context

Solidity Unit Testing refers Solidity Compiler plugin for compiler configurations. One can provide customized inputs for Compiler, EVM Version & Enable Optimization.

2. Custom Transaction Context

For a contract method interaction, prime parameters of transaction are from address, value & gas. Usually, we need to test method's behaviour under different values of these parameters.

Remix provides the functionality of custom msg.sender & msg.value of transaction using method devdoc like:

/// #sender: account-0
/// #value: 10
function checkSenderIs0AndValueis10 () public payable{
    Assert.equal(msg.sender, TestsAccounts.getAccount(0), "wrong sender in checkSenderIs0AndValueis10");
    Assert.equal(msg.value, 10, "wrong value in checkSenderIs0AndValueis10");
}

Note: To use custom msg.sender functionality, remix_accounts.sol should be imported in your test file.

Complete example can be seen in examples section.

Regarding gas, Remix estimate the required gas for each transaction internally. Still if a contract deployment fails with Out-of-Gas error, it tries to redeploy it by doubling the gas. Deployment failing with double gas will show error: contract deployment failed after trying twice: The contract code couldn\'t be stored, please check your gas limit

Points to remember

  • A test contract can not have a method with parameters. Having one such method will show error: Method 'methodname' can not have parameters inside a test contract
  • No. of test accounts are 3 before remix-ide release v0.10.0 and 10 afterwards
  • A test file which imports remix_accounts.sol might not compile successfully with Solidity Compiler plugin but it will work fine with Solidity Unit Testing plugin

Continuous integration

remix-tests is also a CLI, it can be used in a continuous integration environment which support node.js. Please find more information in the remix-tests repository

See also: example Su Squares contract and Travis build that uses remix-tests for continuous integration testing.