Sobes.tech
Junior — Middle

How do you create test scenarios to verify code?

sobes.tech AI

Answer from AI

To create test scenarios, I usually use the unittest or pytest modules. First, I identify which functions or methods need to be tested, then write tests that verify their correctness with various input data, including boundary cases and possible errors.

Example with unittest:

import unittest

def add(a, b):
    return a + b

class TestAddFunction(unittest.TestCase):
    def test_positive_numbers(self):
        self.assertEqual(add(2, 3), 5)

    def test_negative_numbers(self):
        self.assertEqual(add(-1, -1), -2)

    def test_zero(self):
        self.assertEqual(add(0, 0), 0)

if __name__ == '__main__':
    unittest.main()

This approach helps ensure that the code works correctly and that changes do not break existing functionality.