0x07 Python - Test-Driven Development
0x07 Python - Test-Driven Development
0x07 Python -
Test-driven
development
python- Programming
TABLE OF CONTENTS
01 02
Overview Learning
topics Objectives
03 04
Quiz hands on lab
questions practice
01
OVERVIEW topics
Topics
https://t.me/alx_2023
python
Programming
Topics
Slides On Telegram
alx_2023ch
02
Learning Objectives
What’s an interactive test
interactive testing in Python, particularly with doctest, allows you to create self-
contained documentation that includes code examples and tests, enhancing both
documentation and code validation. It is a valuable tool for ensuring that your
code examples in documentation remain accurate and functional as your code
evolves.
Each function has a docstring that includes code examples and their expected
outputs.
The >>> prompts indicate that these are interactive sessions, and users can
execute the code within these sessions.
Ex:
This function takes two numbers, 'x' and 'y', and returns their sum.
Usage:
>>> add(2, 3)
5
>>> add(-1, 1)
0
"""
return x + y
This function takes two numbers, 'x' and 'y', and returns their sum.
Usage:
>>> add(2, 3)
5
>>> add(-1, 1)
0
"""
return x + y
This function takes two numbers, 'x' and 'y', and returns their difference.
Usage:
>>> subtract(5, 3)
2
>>> subtract(10, 7)
3
"""
return x - y
This function takes two numbers, 'x' and 'y', and returns their product.
Usage:
>>> multiply(2, 4)
8
>>> multiply(3, -2)
-6
"""
return x * y
Doctest with txt file
"""Module for say_my_name method."""
Args:
first_name: first name string.
last_name: last name string.
Raises:
TypeError: If first_name or last_name are not strings.
"""
if not isinstance(first_name, str):
raise TypeError("first_name must be a string")
if __name__ == "__main__":
import doctest
doctest.testfile("tests/3-say_my_name.txt")
Doctest with txt file
The ``3-say_my_name`` module
============================
Using ``3-say_my_name``
---------------------
Import module:
>>> say_my_name = __import__('3-say_my_name').say_my_name
Test basic:
>>> say_my_name("hello", "there")
My name is hello there
Test firstname:
>>> say_my_name("hello")
My name is hello
Missing 2 args:
>>> say_my_name()
Traceback (most recent call last):
...
TypeError: say_my_name() missing 1 required positional argument: 'first_name
Use unittest module
import unittest
add = __import__('code').add
class TestAdd(unittest.TestCase):
def test_basic(self):
self.assertEqual(add(5, 10), 15)
if __name__ == '__main__':
unittest.main()
04
Hands on lab Practice
Have a Question
Leave a Comment!
Subscribe
To stay updated with latest
videos
Share
To let the others know more
Thanks