Danh mục

Dive Into Python-Chapter 14. Test-First Programming

Số trang: 53      Loại file: pdf      Dung lượng: 0.00 B      Lượt xem: 8      Lượt tải: 0    
Hoai.2512

Hỗ trợ phí lưu trữ khi tải xuống: 40,000 VND Tải xuống file đầy đủ (53 trang) 0
Xem trước 6 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu dive into python-chapter 14. test-first programming, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Dive Into Python-Chapter 14. Test-First Programming Chapter 14. Test-First Programming14.1. roman.py, stage 1Now that the unit tests are complete, its time to start writing the code thatthe test cases are attempting to test. Youre going to do this in stages, so youcan see all the unit tests fail, then watch them pass one by one as you fill inthe gaps in roman.py.Example 14.1. roman1.pyThis file is available in py/roman/stage1/ in the examples directory.If you have not already done so, you can download this and other examplesused in this book.Convert to and from Roman numerals#Define exceptionsclass RomanError(Exception): pass 1class OutOfRangeError(RomanError): pass 2class NotIntegerError(RomanError): passclass InvalidRomanNumeralError(RomanError): pass 3def toRoman(n): convert integer to Roman numeral pass 4def fromRoman(s): convert Roman numeral to integer pass1 This is how you define your own custom exceptions in Python.Exceptions are classes, and you create your own by subclassing existingexceptions. It is strongly recommended (but not required) that you subclassException, which is the base class that all built-in exceptions inherit from.Here I am defining RomanError (inherited from Exception) to act as the baseclass for all my other custom exceptions to follow. This is a matter of style; Icould just as easily have inherited each individual exception from theException class directly.2 The OutOfRangeError and NotIntegerError exceptions will eventuallybe used by toRoman to flag various forms of invalid input, as specified inToRomanBadInput.3 The InvalidRomanNumeralError exception will eventually be used byfromRoman to flag invalid input, as specified in FromRomanBadInput.4 At this stage, you want to define the API of each of your functions,but you dont want to code them yet, so you stub them out using the Pythonreserved word pass.Now for the big moment (drum roll please): youre finally going to run theunit test against this stubby little module. At this point, every test caseshould fail. In fact, if any test case passes in stage 1, you should go back toromantest.py and re-evaluate why you coded a test so useless that it passeswith do-nothing functions.Run romantest1.py with the -v command-line option, which will give moreverbose output so you can see exactly whats going on as each test case runs.With any luck, your output should look like this:Example 14.2. Output of romantest1.py against roman1.pyfromRoman should only accept uppercase input ... ERRORtoRoman should always return uppercase ... ERRORfromRoman should fail with malformed antecedents ... FAILfromRoman should fail with repeated pairs of numerals ... FAILfromRoman should fail with too many repeated numerals ... FAILfromRoman should give known result with known input ... FAILtoRoman should give known result with known input ... FAILfromRoman(toRoman(n))==n for all n ... FAILtoRoman should fail with non-integer input ... FAILtoRoman should fail with negative input ... FAILtoRoman should fail with large input ... FAILtoRoman should fail with 0 input ... FAIL======================================================================ERROR: fromRoman should only accept uppercase input----------------------------------------------------------------------Traceback (most recent call last): File C:docbookdippy omanstage1 omantest1.py, line 154, intestFromRomanCase roman1.fromRoman(numeral.upper())AttributeError: None object has no attribute upper======================================================================ERROR: toRoman should always return uppercase----------------------------------------------------------------------Traceback (most recent call last): File C:docbookdippy omanstage1 omantest1.py, line 148, intestToRomanCase self.assertEqual(numeral, numeral.upper())AttributeError: None object has no attribute upper======================================================================FAIL: fromRoman should fail with malformed antecedents----------------------------------------------------------------------Traceback (most recent call last): File C:docbookdippy omanstage1 omantest1.py, line 133, intestMalformedAntecedent self.assertRaises(roman1.InvalidRomanNumeralError,roman1.fromRoman, s) File c:python21libunittest.py, line 266, in failUnlessRaises raise self.failureException, excNameAssertionError: InvalidRomanNumeralError======================================================================FAIL: fromRoman should fail with repeated pairs of numerals----------------------------------------------------------------------Traceback (most recent call last): File C:docbookdippy omanstage1 omantest1.py, lin ...

Tài liệu được xem nhiều: