In the world of blockchain, ensuring the reliability and security of your smart contracts is paramount. Smart contracts, which are self-executing contracts with the terms directly written into code, have revolutionized various industries. However, their immutability means that any bugs or vulnerabilities can lead to significant issues. This is where smart contract testing comes into play.
Why Smart Contract Testing is Essential
Smart contracts operate on blockchain platforms like Ethereum, where they manage high-value transactions and sensitive data. Testing these contracts is crucial to prevent potential exploits and ensure they function as intended. Proper testing can save developers from costly errors and enhance the trust of users and investors.
Introducing Chukti: Your Go-To Smart Contract Testing Tool
Meet Chukti, a low-code smart contract testing library designed to simplify and streamline the testing process. With Chukti, you can write tests in plain English, making it accessible even for those who are not deeply familiar with coding. This tool is perfect for developers looking to ensure their smart contracts are robust and reliable.
Getting Started with Chukti
Chukti comes with a command line tool that helps you initialize a smart contract testing project in no time. Open your terminal and run the following command to initialize a new Chukti project:
npx chukti init
For a detailed setup guide, follow this guide.
https://chukti.vercel.app/guide/getting-started.html
How to Use Chukti for Smart Contract Testing
Here's a comprehensive guide on how to use Chukti to test smart contracts with different scenarios.
Step 1: Create a Feature Test File
Start by defining your tests using Gherkin syntax, which is similar to writing a story. This approach makes the tests easy to understand and maintain. Here's an example:
Feature: Counter contract example
Scenario: Deploy and interact with the Counter contract
Given I have a smart contract located at "contracts/Counter.sol"
When I deploy the smart contract with constructor arguments "[10]" and send "0" Ether
Then I validate the deployment status is "success"
When I call the read function "getNumber" from the contract with arguments "[]"
Then I store the result in "currentNumber"
And I validate the value stored in "currentNumber" should be "equal to" "10"
When I call the write function "increment" from the contract with arguments "[]" and send "0" Ether
Then I validate the status of the last transaction is "success"
And I call the read function "getNumber" from the contract with arguments "[]"
Then I store the result in "currentNumber"
And I validate the value stored in "currentNumber" should be "equal to" "11"
Step 2: Run the Test
Execute the written test with a single command: npx chukti test
. This will generate a detailed web-based report of the test results, making it easy to analyze and identify any issues.
Example Solidity Code
Here's a sample smart contract that you can use to test with Chukti:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 private number;
constructor(uint256 _number) {
number = _number;
}
function getNumber() public view returns (uint256) {
return number;
}
function setNumber(uint256 _number) public {
number = _number;
}
function increment() public {
number++;
}
function decrement() public {
number--;
}
function resetNumber() public {
number = 0;
}
function incrementBy(uint256 _value) public {
number += _value;
}
}
Have you reviewed the example Solidity code we'll be testing? Great! Now, let's dive into the feature test file, which serves as our test script. You might be wondering where the actual code is. Well, that's the beauty of Behavior-Driven Development (BDD). With Chukti, you simply reuse predefined step definitions (written in plain English) from the Chukti Library, which contains all the necessary reusable code for your smart contract testing. Let's break down what the test file does behind the scenes.
Detailed Breakdown of the Test File
- Smart Contract Location
Given I have a smart contract located at "contracts/Counter.sol"
This step ensures that the smart contract named Counter.sol
exists in the contracts
folder of your Chukti project, along with its compiled ABI.
- Deploying the Contract
When I deploy the smart contract with constructor arguments "[10]" and send "0" Ether
This step deploys your contract on a local blockchain instance with the constructor argument 10
. Yes, Chukti automatically spins up a local blockchain on your machine for testing and shuts it down once the tests are complete.
- Validating Deployment
Then I validate the deployment status is "success"
This step checks whether the deployment was successful. You can also use this step to test for failed deployments under different conditions.
- Reading Contract Data
When I call the read function "getNumber" from the contract with arguments "[]"
Then I store the result in "currentNumber"
These steps call the getNumber
function with no arguments and store the returned value in a variable named currentNumber
.
- Asserting Values
And I validate the value stored in "currentNumber" should be "equal to" "10"
This step asserts that the value stored in currentNumber
matches the expected value, which is 10
.
- Writing to the Contract
When I call the write function "increment" from the contract with arguments "[]" and send "0" Ether
Then I validate the status of the last transaction is "success"
This step calls the increment
function to increase the number by 1
. The subsequent step checks if the transaction was successful.
- Final Validation
And I call the read function "getNumber" from the contract with arguments "[]"
Then I store the result in "currentNumber"
And I validate the value stored in "currentNumber" should be "equal to" "11"
Finally, these steps call the getNumber
function again to verify that the number was incremented correctly, and assert that the new value is 11
.
Interesting right? I know!
Read the detailed guide here:
https://chukti.vercel.app/guide/how-to-write-test.html
Benefits of Using Chukti for Smart Contract Testing
- Low-Code Approach: Chukti's low-code nature allows developers to write tests in plain English, making it accessible even for those who are not deeply familiar with coding.
- Efficiency: By simplifying the testing process, Chukti helps save time and effort, allowing developers to focus on building and improving their smart contracts.
- Human-Readable Syntax: The use of Gherkin syntax makes the tests easy to understand and maintain, promoting better collaboration among team members.
- Detailed Reporting: Chukti provides detailed web-based reports, making it easier to analyze test results and identify issues quickly.
Real-World Applications of Smart Contract Testing
Smart contracts are used in various industries, including finance, supply chain, healthcare, and more. Ensuring their reliability through rigorous testing is crucial to prevent costly errors and security breaches. Here are a few real-world applications where smart contract testing plays a vital role:
- Decentralized Finance (DeFi): In DeFi applications, smart contracts handle large volumes of transactions and assets. Testing ensures that these contracts function correctly and securely.
- Supply Chain Management: Smart contracts automate and streamline supply chain processes. Testing helps verify that these contracts execute as intended, reducing the risk of disruptions.
- Healthcare: Smart contracts can manage patient records and automate insurance claims. Testing ensures the accuracy and security of these sensitive operations.
Conclusion
In the rapidly evolving world of blockchain and smart contracts, having a reliable testing framework is essential. Chukti offers a low-code, efficient, and user-friendly solution for smart contract testing, making it easier for developers to ensure their contracts are robust and secure. By leveraging Chukti, you can simplify your testing process, improve collaboration, and ultimately deliver better smart contracts.
Explore the Chukti project on Peerlist and give it an upvote if you like it.
https://peerlist.io/sohabsk/project/chukti
Top comments (0)