<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sohab Sk</title>
    <description>The latest articles on DEV Community by Sohab Sk (@0xsohab).</description>
    <link>https://dev.to/0xsohab</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1249745%2F1251dd5e-5b60-45c7-a224-8f7dd32d87df.jpg</url>
      <title>DEV Community: Sohab Sk</title>
      <link>https://dev.to/0xsohab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/0xsohab"/>
    <language>en</language>
    <item>
      <title>The Ultimate Guide to Smart Contract Testing in 2024</title>
      <dc:creator>Sohab Sk</dc:creator>
      <pubDate>Wed, 02 Oct 2024 19:04:33 +0000</pubDate>
      <link>https://dev.to/0xsohab/the-ultimate-guide-to-smart-contract-testing-in-2024-48ik</link>
      <guid>https://dev.to/0xsohab/the-ultimate-guide-to-smart-contract-testing-in-2024-48ik</guid>
      <description>&lt;p&gt;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 &lt;strong&gt;smart contract testing&lt;/strong&gt; comes into play.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyyg009jk2fx9mz6imenh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyyg009jk2fx9mz6imenh.png" alt="Chukti Smart Contract Testing library"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Smart Contract Testing is Essential
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing Chukti: Your Go-To Smart Contract Testing Tool
&lt;/h2&gt;

&lt;p&gt;Meet &lt;strong&gt;Chukti&lt;/strong&gt;, 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with Chukti
&lt;/h2&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx chukti init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a detailed setup guide, follow this guide.&lt;br&gt;
&lt;a href="https://chukti.vercel.app/guide/getting-started.html" rel="noopener noreferrer"&gt;https://chukti.vercel.app/guide/getting-started.html&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How to Use Chukti for Smart Contract Testing
&lt;/h2&gt;

&lt;p&gt;Here's a comprehensive guide on how to use Chukti to test smart contracts with different scenarios.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: Create a Feature Test File
&lt;/h3&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="kd"&gt;Feature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; Counter contract example

  &lt;span class="kn"&gt;Scenario&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; Deploy and interact with the Counter contract
    &lt;span class="nf"&gt;Given &lt;/span&gt;I have a smart contract located at &lt;span class="s"&gt;"contracts/Counter.sol"&lt;/span&gt;
    &lt;span class="nf"&gt;When &lt;/span&gt;I deploy the smart contract with constructor arguments &lt;span class="s"&gt;"[10]"&lt;/span&gt; and send &lt;span class="s"&gt;"0"&lt;/span&gt; Ether
    &lt;span class="nf"&gt;Then &lt;/span&gt;I validate the deployment status is &lt;span class="s"&gt;"success"&lt;/span&gt;

    &lt;span class="nf"&gt;When &lt;/span&gt;I call the read function &lt;span class="s"&gt;"getNumber"&lt;/span&gt; from the contract with arguments &lt;span class="s"&gt;"[]"&lt;/span&gt;
    &lt;span class="nf"&gt;Then &lt;/span&gt;I store the result in &lt;span class="s"&gt;"currentNumber"&lt;/span&gt;
    &lt;span class="nf"&gt;And &lt;/span&gt;I validate the value stored in &lt;span class="s"&gt;"currentNumber"&lt;/span&gt; should be &lt;span class="s"&gt;"equal to"&lt;/span&gt; &lt;span class="s"&gt;"10"&lt;/span&gt;

    &lt;span class="nf"&gt;When &lt;/span&gt;I call the write function &lt;span class="s"&gt;"increment"&lt;/span&gt; from the contract with arguments &lt;span class="s"&gt;"[]"&lt;/span&gt; and send &lt;span class="s"&gt;"0"&lt;/span&gt; Ether
    &lt;span class="nf"&gt;Then &lt;/span&gt;I validate the status of the last transaction is &lt;span class="s"&gt;"success"&lt;/span&gt;

    &lt;span class="nf"&gt;And &lt;/span&gt;I call the read function &lt;span class="s"&gt;"getNumber"&lt;/span&gt; from the contract with arguments &lt;span class="s"&gt;"[]"&lt;/span&gt;
    &lt;span class="nf"&gt;Then &lt;/span&gt;I store the result in &lt;span class="s"&gt;"currentNumber"&lt;/span&gt;
    &lt;span class="nf"&gt;And &lt;/span&gt;I validate the value stored in &lt;span class="s"&gt;"currentNumber"&lt;/span&gt; should be &lt;span class="s"&gt;"equal to"&lt;/span&gt; &lt;span class="s"&gt;"11"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Run the Test
&lt;/h3&gt;

&lt;p&gt;Execute the written test with a single command: &lt;code&gt;npx chukti test&lt;/code&gt;. This will generate a detailed web-based report of the test results, making it easy to analyze and identify any issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Solidity Code
&lt;/h2&gt;

&lt;p&gt;Here's a sample smart contract that you can use to test with Chukti:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Detailed Breakdown of the Test File
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Smart Contract Location&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="nf"&gt;Given &lt;/span&gt;I have a smart contract located at &lt;span class="s"&gt;"contracts/Counter.sol"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step ensures that the smart contract named &lt;code&gt;Counter.sol&lt;/code&gt; exists in the &lt;code&gt;contracts&lt;/code&gt; folder of your Chukti project, along with its compiled &lt;strong&gt;ABI&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Deploying the Contract&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="nf"&gt;When &lt;/span&gt;I deploy the smart contract with constructor arguments &lt;span class="s"&gt;"[10]"&lt;/span&gt; and send &lt;span class="s"&gt;"0"&lt;/span&gt; Ether
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step deploys your contract on a local blockchain instance with the constructor argument &lt;code&gt;10&lt;/code&gt;. Yes, Chukti automatically spins up a local blockchain on your machine for testing and shuts it down once the tests are complete.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Validating Deployment&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="nf"&gt;Then &lt;/span&gt;I validate the deployment status is &lt;span class="s"&gt;"success"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step checks whether the deployment was successful. You can also use this step to test for failed deployments under different conditions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reading Contract Data&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="nf"&gt;When &lt;/span&gt;I call the read function &lt;span class="s"&gt;"getNumber"&lt;/span&gt; from the contract with arguments &lt;span class="s"&gt;"[]"&lt;/span&gt;
&lt;span class="nf"&gt;Then &lt;/span&gt;I store the result in &lt;span class="s"&gt;"currentNumber"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These steps call the &lt;code&gt;getNumber&lt;/code&gt; function with no arguments and store the returned value in a variable named &lt;code&gt;currentNumber&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Asserting Values&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="nf"&gt;And &lt;/span&gt;I validate the value stored in &lt;span class="s"&gt;"currentNumber"&lt;/span&gt; should be &lt;span class="s"&gt;"equal to"&lt;/span&gt; &lt;span class="s"&gt;"10"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step asserts that the value stored in &lt;code&gt;currentNumber&lt;/code&gt; matches the expected value, which is &lt;code&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Writing to the Contract&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="nf"&gt;When &lt;/span&gt;I call the write function &lt;span class="s"&gt;"increment"&lt;/span&gt; from the contract with arguments &lt;span class="s"&gt;"[]"&lt;/span&gt; and send &lt;span class="s"&gt;"0"&lt;/span&gt; Ether
&lt;span class="nf"&gt;Then &lt;/span&gt;I validate the status of the last transaction is &lt;span class="s"&gt;"success"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step calls the &lt;code&gt;increment&lt;/code&gt; function to increase the number by &lt;code&gt;1&lt;/code&gt;. The subsequent step checks if the transaction was successful.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Final Validation&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="nf"&gt;And &lt;/span&gt;I call the read function &lt;span class="s"&gt;"getNumber"&lt;/span&gt; from the contract with arguments &lt;span class="s"&gt;"[]"&lt;/span&gt;
&lt;span class="nf"&gt;Then &lt;/span&gt;I store the result in &lt;span class="s"&gt;"currentNumber"&lt;/span&gt;
&lt;span class="nf"&gt;And &lt;/span&gt;I validate the value stored in &lt;span class="s"&gt;"currentNumber"&lt;/span&gt; should be &lt;span class="s"&gt;"equal to"&lt;/span&gt; &lt;span class="s"&gt;"11"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, these steps call the &lt;code&gt;getNumber&lt;/code&gt; function again to verify that the number was incremented correctly, and assert that the new value is &lt;code&gt;11&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Interesting right? I know!&lt;/p&gt;

&lt;p&gt;Read the detailed guide here:&lt;br&gt;
&lt;a href="https://chukti.vercel.app/guide/how-to-write-test.html" rel="noopener noreferrer"&gt;https://chukti.vercel.app/guide/how-to-write-test.html&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Using Chukti for Smart Contract Testing
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Low-Code Approach&lt;/strong&gt;: 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency&lt;/strong&gt;: By simplifying the testing process, Chukti helps save time and effort, allowing developers to focus on building and improving their smart contracts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Human-Readable Syntax&lt;/strong&gt;: The use of Gherkin syntax makes the tests easy to understand and maintain, promoting better collaboration among team members.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Detailed Reporting&lt;/strong&gt;: Chukti provides detailed web-based reports, making it easier to analyze test results and identify issues quickly.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Real-World Applications of Smart Contract Testing
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Decentralized Finance (DeFi)&lt;/strong&gt;: In DeFi applications, smart contracts handle large volumes of transactions and assets. Testing ensures that these contracts function correctly and securely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supply Chain Management&lt;/strong&gt;: Smart contracts automate and streamline supply chain processes. Testing helps verify that these contracts execute as intended, reducing the risk of disruptions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Healthcare&lt;/strong&gt;: Smart contracts can manage patient records and automate insurance claims. Testing ensures the accuracy and security of these sensitive operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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 &lt;strong&gt;simplify your testing&lt;/strong&gt; process, improve collaboration, and ultimately deliver better smart contracts.&lt;/p&gt;

&lt;p&gt;Explore the Chukti project on Peerlist and give it an upvote if you like it. &lt;br&gt;
&lt;a href="https://peerlist.io/sohabsk/project/chukti" rel="noopener noreferrer"&gt;https://peerlist.io/sohabsk/project/chukti&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find Chukti on Product Hunt: &lt;br&gt;
&lt;a href="https://www.producthunt.com/posts/chukti?embed=true&amp;amp;utm_source=badge-featured&amp;amp;utm_medium=badge&amp;amp;utm_souce=badge-chukti" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fapi.producthunt.com%2Fwidgets%2Fembed-image%2Fv1%2Ffeatured.svg%3Fpost_id%3D493455%26theme%3Dlight" alt="Chukti - A Low Code Smart Contract Testing Library | Product Hunt"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>smartcontract</category>
      <category>opensource</category>
      <category>web3</category>
    </item>
  </channel>
</rss>
