1 Replies - 261 Views - Last Post: 06 February 2014 - 07:20 AM Rate Topic: -----

#1 apathybear7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-June 13

unit test failure (book example is passing)

Posted 05 February 2014 - 07:55 PM

I am learning from Dive into python 3 and am currently stuck on the chapter on unit testing.

Here is my unit called roman1.py
#test with romantest1

roman_numeral_map = (('M',  1000),
                     ('CM', 900),
                     ('D',  500),
                     ('CD', 400),
                     ('C',  100),
                     ('XC', 90),
                     ('L',  50),
                     ('XL', 40),
                     ('X',  10),
                     ('IX', 9),
                     ('V',  5),
                     ('IV', 4),
                     ('I',  1)) 


def to_roman(n):
	'''convert integer to Roman numeral'''
	
	if n > 3999:
		raise OutOfRangeError('number out of range ..')


	result = ''


	for numeral, integer in roman_numeral_map:

		while n >= integer:
			result = result + numeral
			n = n - integer

	return result   #result is a roman numeral



#test with romantest2
class OutOfRangeError(ValueError):
	pass



and here is one of my unit tests called unittest2.py
import unittest
import roman1



class ToRomanBadInput(unittest.TestCase):
	def test_too_large(self):
		'''to_roman should fail with large input'''
		self.assertRaises(roman1.OutOfRangeError, roman1.to_roman, 4000)

if __name__ == '__main__':
	unittest.main() 




According to the book, this test should be passing. For me, this test is raising a failure. What is it im doing wrong? This is the link to the book: http://www.diveintop...html#romantest2

any help is much appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: unit test failure (book example is passing)

#2 DblAAssassin  Icon User is offline

  • D.I.C Regular

Reputation: 32
  • View blog
  • Posts: 253
  • Joined: 11-May 13

Re: unit test failure (book example is passing)

Posted 06 February 2014 - 07:20 AM

What is the Exact Error, you are getting?

My guess, is that it is saying that __name__ & __main__ are undefined.

Try removing the if statement, and see if it works, as the if statement is not in the book.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1