Home Blockchain The complete guide to blockchain testing – LogRocket Blog

The complete guide to blockchain testing – LogRocket Blog

by Assessor
Published: Last Updated on

Rate this post

Organizations all around the world are turning to blockchain expertise to enhance knowledge storage, safety, and administration. This has resulted within the want to make sure that purposes constructed on the blockchain community are completely examined.

On this article, we are going to talk about what blockchain testing is all about, together with the advantages and downsides, the forms of blockchain testing, the phases, and a few helpful instruments. We may even create and check a wise contract utilizing a number of the beneficial testing instruments.

What’s blockchain testing?

Blockchain testing is the systematic analysis of the blockchain’s numerous practical parts (e.g., good contracts). In contrast to conventional software program testing, blockchain testing entails a number of parts corresponding to blocks, mining, transactions, wallets, and so forth, all of which require particular instruments to check.

Blockchain testing aids within the improvement of assorted high quality phases, starting from system efficiency to the safety of the blockchain utility.

In response to Santu Maity, Enterprise Architect at IBM, the perfect strategy for blockchain testing encompasses your entire atmosphere. This consists of blockchain-based purposes, each cellular and net, that work together with the blockchain system’s practical part, like an API, good contracts, and nodes.

Advantages of blockchain testing

Blockchain testing ensures that each one entities concerned in a blockchain community have been correctly validated for operation. In consequence, it offers organizations with a safe and practical infrastructure.

Blockchain testing aids within the supply of high quality merchandise, thereby enhancing consumer expertise. It additionally eliminates flaws in a decentralized system the place cash is concerned to be able to stop monetary harm.

Challenges of blockchain testing

One of the vital important challenges is an absence of testing instruments. Presently, there are few testing instruments obtainable for every blockchain framework; subsequently, utilizing the fallacious device could cause issues.

One other difficulty within the blockchain ecosystem is an absence {of professional} experience. As a result of blockchain expertise continues to be comparatively new within the tech world, it has not seen widespread adoption amongst software program builders.

Yet one more problem is testing technique. Blockchain testing necessitates a radical data and understanding of how blockchain works.

The safety of the blockchain community may also be tough. Blockchain purposes at the moment are being utilized in quite a lot of financial sectors. In consequence, knowledge safety is essential for stopping nefarious actions.

The lack to supply an ample efficiency and cargo testing offers little or no data on the efficiency of blockchain purposes underneath sure circumstances.

Varieties of blockchain testing

The next is a complete listing of the forms of testing one can carry out on a blockchain:

  1. Purposeful testing determines the effectiveness of assorted practical parts of the blockchain system
  2. Node testing aids within the unbiased testing of every node on the community to make sure a problem-free connection
  3. Efficiency testing identifies system movement restrictions and recommends an optimum answer
  4. API testing contributes to a transparent interplay between purposes within the blockchain community by making certain that requests and responses between these purposes are correctly operated

Phases of blockchain testing

Initiation part

The initiation part is the primary stage of testing a blockchain system. Right here, the testers develop into acquainted with the system’s lifecycle by analyzing and comprehending its performance, permitting them to realize a greater understanding of all parts concerned. An in depth map is generated that features all the system parts and subcomponents, in addition to all the interfaces, to supply a superb understanding of how the system works general.

Design part

Within the design part, the important thing parts of the system that have to be examined are recognized, and a effectively detailed check technique tailor-made to the blockchain system is developed. This check technique describes the system’s check circumstances and check atmosphere specs.

Planning part

Throughout this part, it’s determined how every sort of check shall be carried out, with an estimate of what number of checks shall be carried out at every stage and to what extent.

If the system isn’t obtainable, various testing methods have to be devised. Organising a non-public blockchain for testing is an alternate check technique. API testing, practical testing, efficiency testing, safety testing, and so forth are examples of those checks.

End result part

That is the ultimate part, which features a report on the general check carried out within the system. System efficiency, low stage verify, and validation of blocks, transactions, and good contracts are the basic workout routines that have to be executed throughout this part.

Blockchain testing instruments

Ethereum tester

Ethereum Tester is an Ethereum testing device that features Web3 Integration, API, and Sensible Contracts. This enables improvement groups to recreate a production-like Ethereum blockchain.

An open supply testing library, it’s easy to implement and has manageable API assist for quite a lot of testing necessities.


This device is primarily used to regionally check Ethereum contracts. It generates a blockchain simulation that enables anybody to check a number of accounts.

Exonum TestKit

Exonum TestKit focuses on testing the exercise of your entire service of the blockchain utility. We will use the device to carry out API testing and transaction execution with out having to fret about community operations or consensus algorithms.

Corda testing device

Corda is an open supply, distributed ledger platform primarily based on blockchain expertise. Contract testing, integration testing, movement testing, and cargo testing are all made simpler with the inbuilt testing device.


Truffle is a blockchain testing device with options that transcend primary testing, corresponding to working with Chai and Mocha. It’s a well-known identify amongst Ethereum builders for figuring out superb testing options, corresponding to automated contract testing.


The Populus framework consists of Ethereum’s testing performance, which is effectively built-in as a set of properties centered towards contract deployment testing. These frameworks are principally constructed across the pytest framework, permitting for its quite simple implementation.

Testing a wise contract

Now let’s transfer on to the tutorial part of the article. Right here, we are going to develop and check a pattern good contract with Truffle now that you just’ve grasped the fundamentals of blockchain testing.

On this instance, we are going to create a automobile monitoring system for an auto store. We’ll cowl the next:

  • Organising the event atmosphere
  • Making a Truffle challenge
  • Writing the good contract
  • Compiling the good contract
  • Migrating the good contract
  • Testing the good contract

Organising the atmosphere

To work with Truffle, you’ll must have the next software program put in in your pc:

  • Node.js
  • npm
  • Git

Run the next command in your terminal as soon as they’ve been put in efficiently:

npm set up -g truffle

The command above will set up Truffle globally in your pc. Sort “truffle model” within the terminal to see if Truffle is put in accurately.

Create a brand new Truffle challenge

Create a brand new challenge listing with the next:

mkdir truffle-project

Migrate into your new challenge listing like so:

cd truffle-project

Then, initialize Truffle:

truffle init

The above command will generate the next listing construction for Truffle:

contracts/, which comprises the good contract supply codes, written in Solidity. Inside this listing there’s an vital contract referred to as nftgamef.com.

migrations/. Deployment of good contracts is managed with the migration system. It’s used to observe modifications within the good contract.

check/ is the place check codes and information for the good contracts are stored. Checks can written in both Solidity or JavaScript.

nftgamef.com is the Truffle configuration file the place you possibly can outline your deployment community for deploying your good contracts.

Creating a wise contract

Within the contracts/ listing, create a brand new file referred to as nftgamef.com and add the next:

// SPDX-License-Identifier: MIT pragma solidity ^0.8.9; contract Buy { handle[20] public patrons; }

pragma solidity ^0.8.9; signifies the minimal model of Solidity required. The pragma command means “further data that solely the compiler cares about”, whereas the caret image (^) means “the model indicated or increased.”

When writing Solidity, knowledge sorts like array and strings have to be declared. Solidity offers a particular knowledge sort referred to as handle, which can also be an Ethereum handle saved as 20 byte values. This handle can be utilized to ship and obtain Ether.

Throughout the contract Buy{ scope, we outline a public variable referred to as borrower with an handle sort and a size of 20, which is an array of Ethereum addresses. All the time bear in mind so as to add a semicolon (;) on the finish of each assertion, to keep away from an error.

Now let’s create a operate that enables customers to make a borrow request. Under the variable declaration, add the next:

// shopping for a automobile operate purchase (uint carId) public returns (uint) { require(carId >= 0 && carId <= 19); purchaser[carId] = nftgamef.comer; return carId; }

In response to the code above, the operate accepts an integer (uint) parameter carId and is anticipated to return an integer because the output. The require() assertion is used to make sure that the carId is inside the purchaser array’s vary. As a result of arrays in Solidity are listed from zero, the carId worth have to be between zero and 19.

If the carId is inside the vary, the handle from which the decision was made is added to our purchaser array, which is denoted by nftgamef.comer. We then return the carId that was handed.

Subsequent we write a operate that get all of the patrons, like so:

// get all patrons operate getBuyers() public view returns (handle[20] reminiscence) { return patrons; }

We return the customer as a kind handle[20] reminiscence which comprises the variable’s knowledge location. view within the operate declaration signifies that the operate is not going to change the state of the contract.

Compiling the good contract

Following that, we should compile our Solidity code in order that the Ethereum Digital Machine (EVM) can perceive it.

Open the terminal in your challenge’s root listing and sort the next command:

truffle compile

The above command will compile all good contracts within the contracts folder and in addition to create a construct listing that comprises a contracts folder with artifacts.

Artifacts are .json information that function a JavaScript wrapper for interacting with the corresponding good contracts.

Migrating the good contract

Now that the contract has been efficiently compiled, it’s time emigrate it to the blockchain.

A migration is a deployment script that’s used to alter the state of your utility’s contracts, shifting them from one state to the subsequent.

To deploy the good contract over the blockchain, we are going to first create the migration configuration file. It is a JavaScript file that handles deployment of the good contracts on the blockchain.

Nevertheless, to be able to deploy the good contracts with migrations, we should first achieve entry to their artifacts, which had been generated because of the Truffle compile command. Contained in the migration listing is a default migration file nftgamef.com that handles deployment of the nftgamef.com file.

Lets create our personal migration configuration file, nftgamef.com, with the next:

const PurchaseContract = nftgamef.comire(‘Buy’); nftgamef.comrts = operate(deployer) { nftgamef.comoy(PurchaseContract); };

The nftgamef.comire() methodology is used to specify the good contract to make use of for interacting with the blockchain. As a result of a supply file might include a number of contracts, we specified the identify of the contract definition fairly than the identify of the .sol file. On this case we use Buy fairly than Auto.

The migrations are then exported as a operate with a parameter (deployer). The thing deployer is answerable for staging deployments. Subsequent, we deploy PurchaseContract.

Subsequent, we shall be utilizing one other blockchain testing device referred to as Ganache to supply an interface to deploy our good contracts and perform checks. You possibly can both obtain or use the command line npm i -g ganache-cli.

In case you are utilizing Ubuntu OS, you may discover it tough to run the Ganache utility. Merely right-click on the appliance file, go to properties, then to permissions and tick permit executing file as program. Then re-run the appliance.

I shall be utilizing the Ganache GUI for this tutorial. Run the appliance and choose quickstart. The next picture shall be displayed on the display:

ganache quickstart

Now let’s migrate our good contract:

truffle migrate

After a profitable migration, you will notice the next:

Compiling your contracts… =========================== > All the pieces is updated, there’s nothing to compile. Beginning migrations… ====================== > Community identify: ‘ganache’ > Community id: 5777 > Block gasoline restrict: 6721975 (0x6691b7) nftgamef.com ====================== Changing ‘Migrations’ – > transaction hash: 0x7f4ad90e465b3e6501e8a49f3af1692ba39df66cbb6e014061b9e7e592167c02 > Blocks: 0 Seconds: 0 > contract handle: 0x5A61A1989d92Fb349fAcd409Fdc8A4C640853fD9 > block quantity: 1 > block timestamp: 1636840180 > account: 0x3309Fa70a44a69eB7b7E87038Afa61a1C9dDB31b > steadiness: 99.99502316 > gasoline used: 248842 (0x3cc0a) > gasoline value: 20 gwei > worth despatched: 0 ETH > whole value: 0186212888 ETH > Saving migration to chain. > Saving artifacts – > Complete value: 0186212888 ETH nftgamef.com ==================== Changing ‘Buy’ – > transaction hash: 0x039224bded1eec1272e422d79ea146aa0026d13252fa7c495628829dbf7d5e42 > Blocks: 0 Seconds: 0 > contract handle: 0xA89fdCd07E195be4555E07025b8613224e312F97 > block quantity: 3 > block timestamp: 1636840182 > account: 0x3309Fa70a44a69eB7b7E87038Afa61a1C9dDB31b > steadiness: 99.98848808 > gasoline used: 284241 (0x45651) > gasoline value: 20 gwei > worth despatched: 0 ETH > whole value: 0186212888 ETH > Saving migration to chain. > Saving artifacts – > Complete value: 0186212888 ETH Abstract ======= > Complete deployments: 2 > Last value: 0186212888 ETH

Now return to your Ganache utility:

ganache migration

You’ll discover the present block, which was beforehand zero, is now 4. Additionally, the primary handle began with 100 Ether, now it has 99.99 Ether. That is because of the transaction prices of migration.

We have now efficiently created and deployed our good contract on our native blockchain. Now we will check our good contract to ensure it does what it’s imagined to.

Testing a wise contract

There are two strategies of testing good contracts in Truffle. The primary is by utilizing Solidity and the second is by utilizing JavaScript. For this tutorial, we shall be utilizing the JavaScript methodology.

Within the check listing, create a brand new file nftgamef.com and write the next:

const Buy = nftgamef.comire(“Buy”); contract(“Buy”, (accounts) => { let buy; let expectedBuyer; earlier than(async () => { buy = await nftgamef.comoyed(); }); describe(“get account addresses for each buy”, async () => { earlier than(“purchase a automobile utilizing accounts[0]”, async () => { await nftgamef.com(4, { from: accounts[0] }); expectedBuyer = accounts[0]; }); it(“can retrieve purchaser’s handle by automobile id”, async () => { const purchaser = await nftgamef.comrs(4); nftgamef.coml(purchaser, expectedBuyer, “Anticipated to return purchaser’s account.”); }); it(“can retrieve addresses of each patrons”, async () => { const patrons = await nftgamef.comuyers(); nftgamef.coml(patrons[4], expectedBuyer, “Purchaser must be included.”); }); }); });

First, we use the nftgamef.comire() methodology to import our Buy contract. Then we declare a contract and move in our Buy occasion as the primary argument, adopted by a callback operate because the second.

The parameter for the callback operate is accounts. The accounts offers the community’s obtainable accounts. We use earlier than to make sure that the bought automobile with ID 4 is assigned to the primary account.

We then run a check to see which handle bought the automobile with ID 4. To check the precise worth (purchaser) and the anticipated worth (expectedBuyer), we use the nftgamef.com methodology. If the check fails, the error message is printed to the console.

Lastly, we verify to see if it returns all the patrons’ addresses. Then we verify to see if the handle with UD 4 is among the many returned purchaser addresses.

Now we check our contract by working the next command:

truffle check

If the check passes, it’s best to see the same outcome under:

Utilizing community ‘check’. Compiling your contracts… =========================== > All the pieces is updated, there’s nothing to compile. Contract: Buy get account addresses for each buy ✓ can retrieve purchaser’s handle by automobile id (102ms) ✓ can retrieve addresses of each patrons (429ms) 2 passing (2s)


As blockchain adoption grows, the necessity to ship top quality merchandise can’t be met with out investing in blockchain and blockchain testing experience.

Blockchain testing ensures that each one parts within the system are working correctly and that each one purposes are interacting with it in a reliable method.

Related Posts