Implementing Test Cases for Smart Contracts in Truffle

Seungwon Go
Dec 17, 2018 · 4 min read

By Seungwon Go, CEO & Founder at ReturnValues (

One of the best things about using Truffle Framework for developing smart contracts is that you can write and run test cases fast and easily. In this chapter I’ll cover how to implement test cases.

If you’ve experienced developing smart contracts and deploying to mainnet or testnet, you might know it takes very long time to run tests. It’s very burdensome to run tests since you need to develop DApp for each test case and pay gas fee through Metamask for every transaction.

In this chapter, I’ll introduce how to test smart contracts in the local environment efficiently. The example we’re going to use is based on ERC20 token that I covered in the previous chapter “Developing, Testing, Deploying ERC20 Token using Truffle Framework”. So you need to read that chapter before reading on.

In Truffle, you use Mocha(, a testing framework, for implementing test cases.

Before dealing with how to implement test cases, let’s write test scenarios. The following is the test scenarios for SampleToken.sol:

[Testcase 1: check if the smart contract has been created as set in the variables]1.1. Is the token name the same as set in the variable?1.2. Is the token symbol is the same as set in the variable?1.3. Is the token decimals is the same as set in the variable?1.4. Is the total supply of the token the same as set in the variable total supply?[Testcase 2 : check if the amount of the token supply has been transffered to the token owner]2.1. Is the total token amount issued are the same as that of the balance of the token owner?[Testcase 3 : check if the features implemented work as intended]3.1. Transfer feature: after transferring some tokens to a certain address, is the amount of the token transferred the same as that of the address that has received?3.2 When trying to transferring more tokens than the token supply, is it properly ‘reverted’?

Before implementing the test scenarios above, let’s have a look at the basic structure of a test case in Mocha test framework. It’ll cover most of your cases in the future.

Basic structure of Mocha

Let me briefly explain what each function does written in the test cases.

before: as the function name implies, it gets run before all the testing functions are run.

beforeEach : you implement test cases inside it() function, and beforeEach is a function that runs before every it() is run.

afterEach : it is a function that is run after every it() is run.

describe : you can have multiple test scenarios. You can use describe for separating test cases for each test scenarios.

it: it is a function where you implement a test case.

after : as the function name implies, it is a function that is run after all the testing functions(all the it()s) are run.

After running the test cases above, you’ll see the screen as below:

If you look at the order in the console, you’ll get a clear sense of how each function works.

Now, let’s set the basic structure for our testing program for test cases of SampleToken.sol we wrote previously.

If you run the testing program, you’ll see the following in the console:

Now, let’s write it() functions for every test case. The code for it() functions is as follows:

const SampleToken = artifacts.require("SampleToken");

arftifacts.require is a method similar to require in Node. It returns the smart contract of which the declared contract name is SampleToken.

We created a SampleToken contract in each beforeEach() function so that we can test with SampleToken contract which has the initial state whenever it() function runs.

beforeEach(async function() {
token = await, _symbol, _decimals, _total_supply, {from: owner});

About the methods that are used in test cases, you can refer to the documentation Mocha provids.

Now, let’s run the test cases we’ve written.

You see the test results of the test scenarios in the console.

Smart contracts can be used for critical transactions and their codes are open to public so they can be vulnerable in terms of security. That is why you need to write various test cases and run tests before deploying them.

You can download all the code written above in the link below at Github.


ReturnValues Blogs