0% found this document useful (0 votes)
3 views5 pages

51 Python Testing

The document discusses the importance of testing code to prevent bugs, crashes, and poor user experiences, comparing manual testing, automated test cases, and exception handling. It provides examples of how to implement exception handling and unit testing in Python, emphasizing the benefits of automated tests for catching regressions. The author, Gowtham SB, is a Data Engineering expert who shares insights and resources for learning data engineering.

Uploaded by

joanantoranjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

51 Python Testing

The document discusses the importance of testing code to prevent bugs, crashes, and poor user experiences, comparing manual testing, automated test cases, and exception handling. It provides examples of how to implement exception handling and unit testing in Python, emphasizing the benefits of automated tests for catching regressions. The author, Gowtham SB, is a Data Engineering expert who shares insights and resources for learning data engineering.

Uploaded by

joanantoranjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Gowtham SB

www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

🧪 Python Testing ZGuide: Manual Test vs


Test Case vs Exception Handling

1 WHY TEST YOUR CODE?


1️⃣
To avoid:

● Bugs in production

● App crashes

● Wrong output

● Poor user experience

2 2️⃣
2️⃣ ⚔️Manual Testing vs Automated Test Cases
Feature Manual Testing Test Case (Automated Testing)

How it works Run code manually and check Write assert statements to auto-
output check

Fast for small ✅ Yes ⚠️Overhead at first, but pays off


code later

Repeatable ❌ You must test again ✅ Run all tests with


manually pytest/unittest

Catch regressions ❌ Hard to track ✅ Alerts when old bug comes


back

Example print(divide(10, 2)) assert divide(10, 2) == 5

3️⃣ Exception Handling vs Test Case


Feature Exception Handling Test Case (unittest / pytest)
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

Purpose Avoid crashes Validate that logic is correct

When used In production app During development

Handles invalid input? ✅ Yes ✅ Yes (you can test invalid input
too)

Detects wrong logic? ❌ No ✅ Yes

Example try: a/b assert divide(6, 2) == 3


except:

4️⃣ Sample Code + Manual Testing


def divide(a, b):
return a / b

print(divide(10, 2)) # ✅ OK
print(divide(10, 0)) # ❌ ZeroDivisionError (crash)

5️⃣ Improved Code With Exception Handling


def divide(a, b):
try:
return a / b
except ZeroDivisionError:
return "Cannot divide by zero ❌"

Manual test:

print(divide(10, 0)) # ✅ "Cannot divide by zero ❌"

6️⃣ Add Test Cases with unittest


import unittest

def divide(a, b):


try:
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

return a / b
except ZeroDivisionError:
return "Cannot divide by zero ❌"

class TestDivideFunction(unittest.TestCase):
def test_divide_normal(self):
self.assertEqual(divide(10, 2), 5.0)

def test_divide_zero(self):
self.assertEqual(divide(10, 0), "Cannot divide by zero ❌")

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

Run with:

python test_divide.py

7️⃣ Same with pytest (Simpler)


# test_divide.py
from your_module import divide

def test_divide_normal():
assert divide(10, 2) == 5.0

def test_divide_zero():
assert divide(10, 0) == "Cannot divide by zero ❌"

Run with:

pytest test_divide.py

8️⃣ What Happens if You Introduce a Bug?


If you change this:

return a * b # ❌ by mistake
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

You will still get:

FAILED test_divide_normal - assert 20 == 5.0

🎯 So your test case instantly catches bugs.

🔚 Final Summary
Concept What It Helps With

Manual Testing Quick checks during development

Exception Handling Graceful crash protection in real time

Unit Test / Pytest Automated correctness check, regression


safety
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil

About the Author


Gowtham SB is a Data Engineering expert, educator, and content creator with a
passion for big data technologies, as well as cloud and Gen AI . With years of
experience in the field, he has worked extensively with cloud platforms, distributed
systems, and data pipelines, helping professionals and aspiring engineers master the
art of data engineering.

Beyond his technical expertise, Gowtham is a renowned mentor and speaker, sharing
his insights through engaging content on YouTube and LinkedIn. He has built one of
the largest Tamil Data Engineering communities, guiding thousands of learners to
excel in their careers.

Through his deep industry knowledge and hands-on approach, Gowtham continues to
bridge the gap between learning and real-world implementation, empowering
individuals to build scalable, high-performance data solutions.

𝐒𝐨𝐜𝐢𝐚𝐥𝐬

𝐘𝐨𝐮𝐓𝐮𝐛𝐞 - https://www.youtube.com/@dataengineeringvideos

𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - https://instagram.com/dataengineeringtamil

𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - https://instagram.com/thedatatech.in

𝐂𝐨𝐧𝐧𝐞𝐜𝐭 𝐟𝐨𝐫 𝟏:𝟏 - https://topmate.io/dataengineering/

𝐋𝐢𝐧𝐤𝐞𝐝𝐈𝐧 - https://www.linkedin.com/in/sbgowtham/

𝐖𝐞𝐛𝐬𝐢𝐭𝐞 - https://codewithgowtham.blogspot.com

𝐆𝐢𝐭𝐇𝐮𝐛 - http://github.com/Gowthamdataengineer

𝐖𝐡𝐚𝐭𝐬 𝐀𝐩𝐩 - https://lnkd.in/g5JrHw8q

𝐄𝐦𝐚𝐢𝐥 - atozknowledge.com@gmail.com

𝐀𝐥𝐥 𝐌𝐲 𝐒𝐨𝐜𝐢𝐚𝐥𝐬 - https://lnkd.in/gf8k3aCH

You might also like