Unit Tests in Truffle
Learn how to write and execute tests in Truffle.
In all the previous lessons, we tested our smart contracts manually, via Remix or by sending transactions using web3.js
. Realistically, this could only work for simple smart contracts. If we're working on a complex project with other developers making changes, we need to automate the testing process.
The Truffle Framework allows implementing unit tests that can run a smart contract in a test environment, call methods on it, and validate the results. By adding unit tests to our contracts, we can ensure that our code is working correctly and catch bugs before the contract is deployed.
In this lesson, we'll start with writing tests for the Ownable
smart contract, which is smaller and more straightforward. We'll keep tests for the Auction
smart contract for a later lesson once we’ve learned the ropes.
Example of a test for a smart contract with Truffle
Truffle allows implementing tests in either JavaScript or Solidity, and since the former is more popular, we'll focus only on JavaScript in this course. To add a new test, we need to create an empty JavaScript file in a Truffle project’s test
folder.
Let’s look at an example of a Truffle test file.
Get hands-on with 1400+ tech skills courses.