📖 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
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
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
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
Hint
Use an if statement to check the function result and print the correct message.
Practice
(1/5)
1. What is the main purpose of writing test cases for blockchain smart contracts?
easy
A. To find and fix errors before deploying the contract
B. To make the contract run faster on the blockchain
C. To increase the contract's gas cost
D. To make the contract code longer
Solution
Step 1: Understand the role of test cases
Test cases are written to check if the code works correctly and to catch mistakes early.
Step 2: Relate to blockchain deployment
Since blockchain contracts are hard to change after deployment, testing helps avoid costly errors.
Final Answer:
To find and fix errors before deploying the contract -> Option A
Quick Check:
Test cases catch errors early = C [OK]
Hint: Test cases catch errors before deployment [OK]
Common Mistakes:
Thinking tests speed up contract execution
Believing tests increase gas cost intentionally
Assuming tests make code longer for no reason
2. Which of the following is the correct syntax to group tests in a blockchain testing framework like Mocha?
easy
A. itGroup('Token tests', () => { /* tests */ })
B. group('Token tests', () => { /* tests */ })
C. testGroup('Token tests', () => { /* tests */ })
D. describe('Token tests', () => { /* tests */ })
Solution
Step 1: Recall common test grouping syntax
In Mocha and similar frameworks, describe is used to group related tests.
Step 2: Identify correct function name
Only describe is the standard function for grouping; others are incorrect or do not exist.
3. Consider this test case snippet for a smart contract function that stores a value:
it('should store the value correctly', async () => {
await contract.store(42);
const result = await contract.retrieve();
assert.equal(result.toNumber(), 42);
});
What will be the output if the contract stores 42 correctly?
medium
A. Test passes with no errors
B. Test fails with assertion error
C. Syntax error in test code
D. Runtime error due to missing function
Solution
Step 1: Analyze the test steps
The test calls store(42) then retrieves the value and checks if it equals 42.
Step 2: Understand assertion behavior
If the contract works correctly, result.toNumber() will be 42, so assert.equal passes without error.
Final Answer:
Test passes with no errors -> Option A
Quick Check:
Correct value stored means test passes = A [OK]
Hint: If assert matches expected, test passes [OK]
Common Mistakes:
Assuming test fails even if values match
Confusing syntax errors with assertion failures
Ignoring async/await usage in tests
4. Identify the error in this test case for transferring tokens: