Testing Chainlink Smart Contracts

This article was first published on Chainlink

Testing Chainlink Smart Contracts

Due to the immutability of smart contracts, it’s crucial that they are tested thoroughly before they are deployed. When it comes to writing automated tests, developers have a couple of options:

  1. Solidity Tests
  2. Javascript/Python/other language tests

Often, it’s useful to test contracts both ways, and you can see from this sample testing repo that it can be quite useful to test both with JavaScript and Solidity. Most dApps interact with contracts in this way, so they're useful tests. Solidity, on the other hand, should most likely be used when you're testing a contract/library where the main point of usage comes from another on-chain contract.

Obviously, to be extra thorough, use both. If you have a simple smart contract like:

pragma solidity >=0.5.0;

contract Background {
    uint[] private values;
    function storeValue(uint value) public {
    function getValue(uint initial) public view returns(uint) {
        return values[initial];
    function getNumberOfValues() public view returns(uint) {
        return values.length;

It can be pretty simple to write some Solidity tests like:

pragma solidity >=0.5.0;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../../../contracts/Background.sol";

contract TestBackground {
    Background public background;
    // Run before every test function
    function beforeEach() public {
        background = new Background();
    // Test that it stores a value correctly
    function testItStoresAValue() public {
        uint value = 5;
        uint result = background.getValue(0);
        Assert.equal(result, value, "It should store the correct value");
    // Test that it gets the correct number of values
    function testItGetsCorrectNumberOfValues() public {
        uint newSize = background.getNumberOfValues();
        Assert.equal(newSize, 1, "It should increase the size");
    // Test that it stores multiple values correctly
    function testItStoresMultipleValues() public {
        for (uint8 i = 0; i < 10; i++) {
            uint value = i;

To keep reading, please go to the original article at:

Comments (No)

Leave a Reply