Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
68 views

0x07 Python - Test-Driven Development

Uploaded by

ELOTHMANI93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

0x07 Python - Test-Driven Development

Uploaded by

ELOTHMANI93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ALX LESSON

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

What’s an interactive test

Why tests are important

python How to write Docstrings to create tests

How to write documentation for each module and


Programming function
Topics
What are the basic option flags to create tests

How to find edge cases


Slides On Telegram

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.

The expected outputs are specified after the >>> prompts.


What’s an interactive test

Ex:

def add(x, y):


"""
Add two numbers.

This function takes two numbers, 'x' and 'y', and returns their sum.

Usage:
>>> add(2, 3)
5
>>> add(-1, 1)
0
"""
return x + y

python3 -m doctest code.py


What’s an interactive test
# file name : math_functions.py

def add(x, y):


"""
Add two numbers.

This function takes two numbers, 'x' and 'y', and returns their sum.

Usage:
>>> add(2, 3)
5
>>> add(-1, 1)
0
"""
return x + y

def subtract(x, y):


"""
Subtract two numbers.

This function takes two numbers, 'x' and 'y', and returns their difference.

Usage:
>>> subtract(5, 3)
2
>>> subtract(10, 7)
3
"""
return x - y

def multiply(x, y):


"""
Multiply two numbers.

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."""

def say_my_name(first_name, last_name=""):


"""Method for printing first and last name.

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 not isinstance(last_nam e, str):


raise TypeError("last_name must be a string")

print("My name is {:s} {:s}".format(first_name, last_name))

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

Test lastname empty:


>>> say_my_name("hello", "")
My name is hello

Test not str arg1:


>>> say_my_name(33, "hello")
Traceback (most recent call last):
...
TypeError: first_name must be a string

Test not str arg2:


>>> say_my_name("hello", 98)
Traceback (most recent call last):
...
TypeError: last_name must be a string

Test not str arg1 arg2:


>>> say_my_name(91, 98)
Traceback (most recent call last):
...
TypeError: first_name must be a string

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

You might also like