0
0
Testing Fundamentalstesting~10 mins

Why systematic techniques improve coverage in Testing Fundamentals - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test simulates applying a systematic testing technique to ensure all parts of a simple calculator app are tested. It verifies that each function (add, subtract, multiply, divide) is covered by at least one test case, improving overall test coverage.

Test Code - unittest
Testing Fundamentals
import unittest

class Calculator:
    def add(self, a, b):
        return a + b
    def subtract(self, a, b):
        return a - b
    def multiply(self, a, b):
        return a * b
    def divide(self, a, b):
        if b == 0:
            raise ValueError("Cannot divide by zero")
        return a / b

class TestCalculator(unittest.TestCase):
    def setUp(self):
        self.calc = Calculator()

    def test_add(self):
        self.assertEqual(self.calc.add(2, 3), 5)

    def test_subtract(self):
        self.assertEqual(self.calc.subtract(5, 3), 2)

    def test_multiply(self):
        self.assertEqual(self.calc.multiply(4, 3), 12)

    def test_divide(self):
        self.assertEqual(self.calc.divide(10, 2), 5)

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initializes the Calculator and TestCalculator classes-PASS
2Runs test_add methodCalculator instance ready, inputs 2 and 3Check if add(2, 3) == 5PASS
3Runs test_subtract methodCalculator instance ready, inputs 5 and 3Check if subtract(5, 3) == 2PASS
4Runs test_multiply methodCalculator instance ready, inputs 4 and 3Check if multiply(4, 3) == 12PASS
5Runs test_divide methodCalculator instance ready, inputs 10 and 2Check if divide(10, 2) == 5PASS
6Test ends with all methods coveredAll calculator functions tested onceVerify all functions have test coveragePASS
Failure Scenario
Failing Condition: If any calculator function is not tested, e.g., test_divide method missing
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test_add method verify?
AThat subtracting 2 from 3 returns 1
BThat adding 2 and 3 returns 5
CThat multiplying 2 and 3 returns 6
DThat dividing 2 by 3 returns 0.66
Key Result
Using systematic techniques like writing separate tests for each function ensures all parts of the software are checked, improving test coverage and reducing the chance of missing bugs.