TEST DRIVEN DEVELOPMENT (TDD) IN BLOCKCHAIN USING TRUFFLE

in #blockchain7 years ago

Test-Driven development is a process of developing and running automated test before actual development of the application. Hence, TDD sometimes also called as Test First Development.

3 thumb rules of TDD

You are not allowed to write any production code unless it is to make a failing unit test pass.Meaning you should first test case before any production code and that test should fail.

You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.Meaning you should stop writing any further test case if your test case fail. So the moment your test case fails, you should stop writing the more test case, and start working on production code. And also, even if compiling of test case fails, stop wrting more test cases.

You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

How to perform TDD Test :

Add a test.
Run all tests and see if any new test fails.
Write some code.
Run tests and Refactor code.
Repeat

Flow Diagram :

0_2IzDBzOHdL7G8c3k.png

Note: Truffle comes with built in Mocha test framework and Chai assertion library.

Steps to follow to start using TDD in Truffle
In truffle console :

create test Ballot

Add this code

const Ballot = artifacts.require("./Ballot.sol");

contract('Ballot', function(accounts) {
let BallotInst;
let chairman = accounts[0];
before("getting instance before all Test Cases", async function(){
BallotInst = await Ballot.new();
});
//First test case to test chairman is set or not.
it('chairman is assigned', async function(){
assert.equal(await BallotInst.chairman(), chairman, 'Test Failed. chairman is not assigned.');
})
});

test ./test/ballot.js

It will fail since we haven't written code.

create contract Ballot

It will create a contract under contract folder.

create migration deploy_contracts

It will create a migration file under migrations folder.
Now add code in Ballot.sol

pragma solidity ^0.4.4;
contract Ballot {
address public chairman;
//constructor
function Ballot() public{
chairman = msg.sender;
}
}

test ./test/ballot.js

Now your test case will pass.
Keep adding test cases first and then contract codes...


Output:
output001.png

Ballot test and contract files
https://github.com/Vishwas1/tdd_truffle_bc/blob/master/test/ballot.js
https://github.com/Vishwas1/tdd_truffle_bc/blob/master/contracts/Ballot.sol

Summary:
1.Test driven development is a process of modifying code in order to pass a test designed previously.
2.It more emphasis on production code rather than test case design.
3.It is sometimes known as “Test First Development.”
4.TDD includes refactoring a code i.e. changing/adding some amount of code to the existing code without affecting the
behavior of the code.
5.TDD when used, the code becomes clearer and simple to understand.

Few use full links
https://drive.google.com/file/d/18B5xorMOd6AYDxGVlGBYH7uR7y_ZHJPx/view?usp=sharinghttps://www.codeproject.com/Articles/683498/DTDT https://michalzalecki.com/ethereum-test-driven-introduction-to-solidity/~~~ embed:qkblc5WRn-U youtube ~~~
https://github.com/Vishwas1/tdd_truffle_bc

Sort:  

Hi, vishwasbanand, welcome in steemit. You must and need write a post and introduce yourself. Something like: Hi, friends, my name is..., I live in..., I like..., I hope about steemit..., and I'll write about...
Then you'll use only two tags: introduceyourself and intruducemyself

Oh I see! I am new to Steemit so...Thank you for your suggestion. Will do that.