Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Why and What to Test

Why and What to Test

In this lesson — part of Testing Fundamentals — you'll learn why and what to test in Python and why it matters in real work.

Why it matters

Tests prove your code works and keep it working as you change it.

Key ideas

  • What to test
  • Arrange-Act-Assert
  • Running a test suite
  • Good vs. brittle tests

In practice

Here's how it looks in idiomatic Python:

import unittest

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

class TestAdd(unittest.TestCase):
    def test_add(self):
        self.assertEqual(add(2, 3), 5)

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

Python note: The stdlib unittest framework discovers methods prefixed with test_ on TestCase subclasses; pytest is the popular third-party alternative.

Try it yourself

Exercise: In Python, write three tests for a function that reverses a string.

Recap

You now understand why and what to test and can apply it in Python. Mark this lesson complete and continue to the next one.