1. Engineering
  2. Computer Science
  3. run this example which illustrates unit testing using pythons unittest...

Question: run this example which illustrates unit testing using pythons unittest...

Question details

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 unittests TestCase def testUserName(self): self.assertEqual ),False) ),False) self.assertEqual (is ),False) def testOneAmpersand(self): self.assertEqual(is ValidEmail(jim.), False) self.assertEqual (isValidEmail(jim@), False) def testCompany(self) ), False) self.assertEqual(isValidEmail(jim.smith@abcStreel_com),False) def testGoodEmail(self): ),True) ), True) ),True) #main #unittest.ma.n() suite = TestLoader().loadTestsFromTestCase(TestvalidEmail) TextTestRunner(verbosity-2).run(suite) unittest
  

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution