0
0
Testing Fundamentalstesting~10 mins

Test-driven development (TDD) concept in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates the Test-driven development (TDD) approach by first writing a test for a simple function that adds two numbers, then implementing the function to pass the test.

Test Code - unittest
Testing Fundamentals
import unittest

# Step 1: Write the test first
class TestAddFunction(unittest.TestCase):
    def test_add_two_numbers(self):
        result = add(2, 3)
        self.assertEqual(result, 5)

# Step 2: Implement the function to pass the test
def add(a, b):
    return a + b

if __name__ == '__main__':
    unittest.main()
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test runner starts and discovers test_add_two_numbersTest framework ready to run tests-PASS
2test_add_two_numbers calls add(2, 3)Function add is called with inputs 2 and 3-PASS
3add function returns 5Function returns the sum of inputs-PASS
4test_add_two_numbers asserts result == 5Test compares actual result with expected value 5assertEqual(result, 5)PASS
5Test runner reports test_add_two_numbers PASSEDTest completed successfully-PASS
Failure Scenario
Failing Condition: If the add function returns incorrect result, e.g., returns a - b instead of a + b
Execution Trace Quiz - 3 Questions
Test your understanding
What is the first step in Test-driven development (TDD) shown in this test?
ARun the program without tests
BWrite the test before implementing the function
CImplement the function before writing the test
DRefactor the code without tests
Key Result
In TDD, writing the test first helps clarify requirements and ensures the code meets expectations before implementation.