This commit is contained in:
Yavuz Sava 2025-01-27 15:52:53 +03:00
parent b1ea020901
commit 2b36dffae3

View File

@ -751,6 +751,149 @@
"- To help detect problems earlier in development, rather than later when some other operation fails. Problems that arent addressed until later in the development process can turn out to be more time-intensive and costly to fix.\n",
"- To provide a form of documentation for other developers reading the code."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def OrganizeList(myList):\n",
" for item in myList:\n",
" assert type(item) == int, \"Error: List items must be integers.\"\n",
" myList.sort()\n",
" return myList\n",
"\n",
"print(OrganizeList(my_new_list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Terms and definitions from module 5**\n",
"\n",
"**Automatic testing:** A process where software checks itself for errors and confirms that it works correctly\n",
"**Black-box tests:** A test where there is an awareness of what the program is supposed to do but not how it does it\n",
"**Edge cases:** Inputs to code that produce unexpected results, found at the extreme ends of the ranges of input\n",
"**Pytest:** A powerful Python testing tool that assists programmers in writing more effective and stable programs\n",
"**Software testing:** A process of evaluating computer code to determine whether or not it does what is expected\n",
"**Test case:** This is the individual unit of testing that looks for a specific response to a set of inputs\n",
"**Test fixture:** This prepared to perform one or more tests\n",
"**Test suite:** This is used to compile tests that should be executed together\n",
"**Test runner:** This runs the test and provides developers with the outcomes data\n",
"**unittest:** A set of Python tools to construct and run unit tests\n",
"**Unit tests:** A test to verify that small isolated parts of a program work correctly\n",
"**White-box test:** A test where test creator knows how the code works and can write test cases that use the understanding to make sure it performs as expected"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exemplar: Implementing unit testing"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# emails. py\n",
"# student@14119fc213a3 :~/scripts$ cat emails. py\n",
"#!/usr/bin/env python3\n",
"import sys\n",
"import csv\n",
"def populate_dictionary(filename):\n",
" \"\"\"Populate a dictionary with name/email pairs for easy lookup.\"\"\"\n",
" email_dict = {}\n",
" with open(filename) as csvfile:\n",
" lines = csv.reader(csvfile, delimiter = ',')\n",
" for row in lines:\n",
" name = str(row[0].lower())\n",
" email_dict[name] = row[l]\n",
" return email_dict\n",
"def find_email(argv):\n",
" \"\"\"Return an email address based on the username given.\"\"\"\n",
" # Create the username based on the command line input .\n",
" fullname = str(argv[1] + \" \" + argv[2])\n",
" # Preprocess the data\n",
" email_dict = populate_dictionary('/home/student/data/user_emails.csv')\n",
" # Find and print the email\n",
" return email_dict.get(fullname. lower())\n",
"def main():\n",
" print(find_email(sys.argv))\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n",
"\n",
"# python3 emails.py Blossom Gill\n",
"# blossom@abc.edu"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"import unittest\n",
"from emails import find_email\n",
"class EmailsTest(unittest.TestCase):\n",
" def test_basic(self):\n",
" testcase = [None, \"Bree\", \"Campbell\"]\n",
" expected = \"breee@abc.edu\"\n",
" self.assertEqual(find_email(testcase), expected)\n",
"if __name__ == '__main__':\n",
" unittest.main()\n",
"\n",
"# Output:\n",
"# .\n",
"# ----------------------------------------------------------------------\n",
"# Ran 1 test in 0.001s\n",
"# OK"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Test Case 1: Missing parameters**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Traceback (most recent call last):\n",
" File \"emails.py\", line 30, in <module>\n",
" main()\n",
" File \"emails.py\", line 27, in main\n",
" print(find_email(sys.argv))\n",
" File \"emails.py\", line 20, in find_email\n",
" fullname = str(argv[1] + \" \" + argv[2])\n",
"IndexError: list index out of range\n",
"\n",
"\n",
"#!/usr/bin/env python3\n",
"import unittest\n",
"from emails import find_email\n",
"class TestFile(unittest.TestCase):\n",
" def test_basic(self):\n",
" testcase = [None, \"Bree\", \"Campbell\"]\n",
" expected = \"breee@abc.edu\"\n",
" self.assertEqual(find_email(testcase), expected)\n",
" def test_one_name(self):\n",
" testcase = [None, \"John\"]\n",
" expected = \"Missing parameters\"\n",
" self.assertEqual(find_email(testcase), expected)\n",
"if __name__ == '__main__':\n",
" unittest.main()\n"
]
}
],
"metadata": {