0
0
Software Engineeringknowledge~30 mins

Test-driven development (TDD) in Software Engineering - Mini Project: Build & Apply

Choose your learning style9 modes available
Test-driven development (TDD)
📖 Scenario: You are working on a small software feature that calculates the area of a rectangle. To ensure your code works correctly, you will use Test-driven development (TDD). This means you will first write a test that defines the expected behavior, then write the code to pass the test, and finally complete the setup.
🎯 Goal: Build a simple TDD cycle by writing a test case for a rectangle area function, then write the function to pass the test, and finally complete the test setup.
📋 What You'll Learn
Create a test function named test_rectangle_area that checks if the area calculation is correct for a rectangle with width 5 and height 10.
Create a function named rectangle_area that takes width and height as parameters and returns the area.
Complete the test setup by calling the test function to run the test.
💡 Why This Matters
🌍 Real World
TDD is used in software development to ensure code quality and reduce bugs by writing tests before code.
💼 Career
Many software engineering roles require knowledge of TDD to write reliable and maintainable code.
Progress0 / 4 steps
1
Write the test function
Write a test function called test_rectangle_area that checks if calling rectangle_area(5, 10) returns 50. Use an assert statement inside the function.
Software Engineering
Hint

Remember, the test function should use assert to check the expected output.

2
Write the function to pass the test
Write a function called rectangle_area that takes two parameters: width and height. It should return the product of width and height.
Software Engineering
Hint

The function should multiply width and height and return the result.

3
Call the test function
Add a call to the test_rectangle_area() function at the end of the code to run the test.
Software Engineering
Hint

Simply call the test function by its name followed by parentheses.

4
Complete the TDD cycle
Add a print statement print("Test passed!") after calling test_rectangle_area() to confirm the test passed successfully.
Software Engineering
Hint

This message helps confirm that the test ran without errors.