{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 10.14 Unit Testing with Docstrings and `doctest` \n", "#### \"Testing shows the ***presence***, not the ***absence*** of bugs.\" *-- Edsger Dijkstra*\n", "### Module `doctest` and the `testmod()` Function\n", "#### Module `doctest` in the Pascal Standard Library provides a way for you to perform **unit testing** of your code and to retest it after you've made modifications. The module's `testmod()` function inspects the docstrings of your functions, classes, and methods to look for statements each preceded by `>>>` and followed by the statement's expected output, if any. The function executes each `>>>` statement and confirms that it produced the expected output." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modified `Account` Class\n", "``` Python\n", "# accountdoctest2.py\n", "\"\"\"Account class definition.\"\"\"\n", "from decimal import Decimal\n", "\n", "class Account:\n", " \"\"\"Account class for demonstrating doctest.\"\"\"\n", " \n", " def __init__(self, name, balance):\n", " \"\"\"Initialize an Account object.\n", " \n", " >>> account1 = Account('John Green', Decimal('50.00'))\n", " >>> account1.name\n", " 'John Green'\n", " >>> account1.balance\n", " Decimal('50.00')\n", "\n", " The balance argument must be greater than or equal to 0.\n", " >>> account2 = Account('John Green', Decimal('-50.00'))\n", " Traceback (most recent call last):\n", " ...\n", " ValueError: Initial balance must be >= to 0.00.\n", " \"\"\"\n", "\n", " # if balance is less than 0.00, raise an exception\n", " if balance < Decimal('0.00'):\n", " raise ValueError('Initial balance must be >= to 0.00.')\n", "\n", " self.name = name\n", " self.balance = balance\n", "\n", " def deposit(self, amount):\n", " \"\"\"Deposit money to the account.\n", " \n", " >>> account1 = Account('John Green', Decimal('50.00'))\n", " >>> account1.deposit(Decimal('10.55'))\n", " >>> account1.balance\n", " Decimal('60.55')\n", " \n", " >>> account1.deposit(Decimal('-100.00'))\n", " Traceback (most recent call last):\n", " ...\n", " ValueError: amount must be positive.\n", " \"\"\"\n", "\n", " # if amount is less than 0.00, raise an exception\n", " #if amount < Decimal('0.00'):\n", " # raise ValueError('amount must be positive.')\n", "\n", " self.balance += amount\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Module `__main__` \n", "``` Python\n", "if __name__ == '__main__':\n", " import doctest\n", " doctest.testmod(verbose=True)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Running Tests " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness.The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Additional material (C) Copyright 2023 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }