Testing Guide
This chapter covers how to test smart contract. Since blockchain transaction is not reversible, testing is very important before deployment.
Testing with Truffle
Truffle provides an automated testing framework. This framework lets you write simple and manageable tests in two different ways:
In
JavascriptandTypeScript, for exercising your contracts from the outside world, just like application.In
Solidity, for exercising your contracts in advances, bare-to-the-metal scenarios.
1) Get Started
Add a setter function setGreet to the contract for testing purpose. The source code is given below.
pragma solidity ^0.5.6;
contract Mortal {
/* Define variable owner of the type address */
address payable owner;
/* This function is executed at initialization and sets the owner of the contract */
constructor () public { owner = msg.sender; }
/* Function to recover the funds on the contract */
function kill() public payable { if (msg.sender == owner) selfdestruct(owner); }
}
contract MEVerseGreeter is Mortal {
/* Define variable greeting of the type string */
string greeting;
/* This runs when the contract is executed */
constructor (string memory _greeting) public {
greeting = _greeting;
}
/* Main function */
function greet() public view returns (string memory) {
return greeting;
}
/* Newly added function for testing. */
function setGreet(string memory _greeting) public {
// only owner can change greeting message
require(msg.sender == owner, "Only owner is allowed.");
greeting = _greeting;
}
}Test Scenario
Whether
greet()function returns "Hello, MEVerse" properlyWhether
setGreet()function returns new greeting message properly and reverts when non-owner account attempts to update the greeting.
First, we will install the Chai assertions library (or any different assertions library you use) for generic assertions, and the truffle-assertions library for the smart contract assertions.
2) Writing test in Solidity
Testing with Solidity can be a little bit more intuitive than JavaScript tests. Solidity test contracts live alongside JavaScript tests as .sol files.
Create a file called TestMEVerseGreeting.sol in the test folder. The Truffle suite provides helper libraries for testing, so let's take a look at the example Solidity test:
DeployedAddresses : Every time you change your contract, you must redeploy it to a new address. You can get the deployed contract addresses through this library.
Assert : It gives us access to various testing functions, like
Assert.equals(),Assert.greaterThan(), etc.
Run Solidity test code
It failed. Let's take a look at the error message.
Error: greeting message should match (Tested: Hello, MEVerse, Against: Hello MEVerse)
Notice the missed ',(comma)' at string memory expectedGreet = "Hello MEVerse".
Fix the code and run the test again.
Test success!
If you want to test your contract on Testnet, use network option.
3) Writing test in JavaScript
Truffle uses the Mocha testing framework and Chai assertion library to provide a solid framework for JavaScript test. JavaScript test gives you more flexibility and enables you to write more complex tests.
Let's create a file and name it 0_MEVerseGreeting.js under test directory.
The test code is:
Run test file 0_MEVerseGreeting.js)
For more details, please check Truffle testing and Truffle commands for details.
Last updated