Question: Run this example which illustrates unit testing using python's unittest module. Correct the problems with the code. Suppose that valid email addresses need to the
Run this example which illustrates unit testing using python's unittest module. Correct the problems with the code.
Suppose that valid email addresses need to the follow these rules:
- the address must contain one and only one @ character
- the string following the @ character MUST be abcSteel.com
- the string preceding the @ character must contain ONLY letters and optionally one period. It must be at least one letter.
filename: modValidateEmail.py
def isValidEmail(email): if email.count('@') != 1: #email address MUST aheve exactly one @ character return False tmpList = email.split('@') #create a list of 2 strings(username and mandated company name if len(tmpList) == 2: #there must be a username AND a company name return False
userName = tmpList[0] for c in userName: if not c.isalpha() and c!='.': return False inst=tmpList[1] if inst !='abcSteel.com': return False
#filename: UnitTestvalidEmail.py from unittest import * from modValidateEmail import* class TestvalidEmail(TestCase): #create a subclass of unittest's TestCase def testUserName(self): self.assertEqual (isValidEmail('joe4@abcSteel.com'),False) self.assertEqual(isValidEmail('joe$smith@abcSteel.com'),False) self.assertEqual (is ValidEmail('joe.allen.smith@abcSteel.com'),False) def testOneAmpersand(self): self.assertEqual(is ValidEmail('jim.'), False) self.assertEqual (isValidEmail(jim@smith@abcSteel.com'), False) def testCompany(self) self.assertEqual(isValidEmail('jim.smith@mnstate.com'), False) self.assertEqual(isValidEmail(jim.smith@abcStreel_com'),False) def testGoodEmail(self): self.assertEqual(isValidEmail('jesse.jones@abcSteel.com'),True) self.assertEqual(isValidEmail('jesse@abcSteel.com'), True) self.assertEqual(isValidEmail('jim.@abcSteel.com'),True) #main #unittest.ma.n() suite = TestLoader().loadTestsFromTestCase(TestvalidEmail) TextTestRunner(verbosity-2).run(suite) unittest
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
