Testing Guide
In this section, we'll introduce how to test smart contracts. Because any transaction on the blockchain is not reversible, testing your smart contract is crucial before you deploy the contract.
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 advanced, bare-to-the-metal scenarios.
1) Getting started
We will follow the Deployment Guide using Truffle to create a contract and deploy it. But, before we deploy it, we will add a setter function setGreet to the contract for testing purposes. The source code is given below.
NOTE: We have made some modifications to the contract for testing.
Below is KlaytnGreeting contract source code.
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 KlaytnGreeter 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;
}
}We will test 1) greet() function whether it returns "Hello, Klaytn" message properly, 2) setGreet() function whether it sets 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 TestKlaytnGreeting.sol in the test folder. The Truffle suite provides us with helper libraries for testing, so we need to import those. Let's take a look at the example Solidity test:
Assert : It gives us access to various testing functions, like
Assert.equals(),Assert.greaterThan(), etc.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.
Now, Let's write a test code.
Run your Solidity test code.
Oops, we failed. Let's check the error message,Error: greeting message should match (Tested: Hello, Klaytn, Against: Hello Klaytn). I can notice the missed ',(comma)' at string memory expectedGreet = "Hello Klaytn".
Fix the code and run the test again.
Congratulations! Your test has passed.
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_KlaytnGreeting.js under test directory.
The test code is:
If you are unfamiliar with Mocha unit test, please check the Mocha document.
Use
contract()instead ofdescribe()Structurally, the Truffle test code shouldn't be much different from the usual test code of Mocha. Your test should contain the code that Mocha will recognize it as an automated test. The difference between Mocha and Truffle test is the contract() function. NOTE the use of thecontract()function, and theaccountsarray for specifying available Klaytn accounts.Contract abstractions within your tests Since Truffle has no way of detecting which contract you'll need to interact with during test, you should specify the contract explicitly. One way to do this is by using the
artifacts.require()method.itsyntax This represents each test case with description. The description will print on the console on test-run.truffle-assertionlibrary This library allows you to easily test reverts or other failures by offering thetruffleAssert.reverts()andtruffleAssert.fails()functions.
The output should like the following:
Congratulations! Your test has passed.
4) Specifying test
You can choose the test file to be executed.
For more details, please check Truffle testing and Truffle commands for details.
Last updated