0
0
Blockchain / Solidityprogramming~30 mins

Writing test cases in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Writing Test Cases for a Simple Blockchain
📖 Scenario: You are working on a simple blockchain project. To make sure your blockchain works correctly, you need to write test cases that check its basic functions.
🎯 Goal: Build test cases to verify the blockchain's ability to add blocks and validate the chain.
📋 What You'll Learn
Create a blockchain list with initial data
Set a variable for the expected chain length
Write a test function to check adding a block
Print the test results
💡 Why This Matters
🌍 Real World
Writing test cases helps ensure that blockchain code works correctly before it is used in real applications.
💼 Career
Blockchain developers must write tests to verify their code, which is critical for security and reliability in blockchain projects.
Progress0 / 4 steps
1
Create the initial blockchain data
Create a list called blockchain with these exact dictionaries as blocks: {'index': 1, 'data': 'Genesis Block'} and {'index': 2, 'data': 'Second Block'}
Blockchain / Solidity
Need a hint?

Use a list with two dictionaries exactly as shown.

2
Set the expected chain length
Create a variable called expected_length and set it to 3
Blockchain / Solidity
Need a hint?

Just assign the number 3 to expected_length.

3
Write a test function to add a block
Write a function called test_add_block() that adds a new block {'index': 3, 'data': 'Third Block'} to blockchain and returns len(blockchain) == expected_length
Blockchain / Solidity
Need a hint?

Define the function exactly as named and append the new block inside it.

4
Print the test result
Write a print statement that calls test_add_block() and prints 'Test passed' if it returns true, otherwise prints 'Test failed'
Blockchain / Solidity
Need a hint?

Use an if statement to check the function result and print the correct message.