![]() ![]() If you write unit tests, it is important to write them early and to keep them updated as code and requirements change. Unit testing is an important part of an overall testing-centric development strategy. Python has a framework for unit testing, the appropriately-named unittest module. The set of two conversion functions - to_roman(), and later from_roman() - can be written and tested as a unit, separate from any larger program that imports them. This is called test-driven development, or TDD. You read that right: you’re going to write code that tests code that you haven’t written yet. Now let’s do something a little unexpected: write a test case that checks whether the to_roman() function does what you want it to. The to_roman() function should take an integer from 1 to 3999 and return the Roman numeral representation as a string… It will have two main functions, to_roman() and from_roman(). Let’s start mapping out what a roman.py module should do. There is no way to represent fractions or non-integer numbers in Roman numerals.There is no way to represent negative numbers in Roman numerals.There is no way to represent 0 in Roman numerals.For the purposes of this chapter, let’s stipulate that Roman numerals go from 1 to 3999. The Romans did have several ways of expressing larger numbers, for instance by having a bar over a numeral to represent that its normal value should be multiplied by 1000. There is a limited range of numbers that can be expressed as Roman numerals, specifically 1 through 3999.The converse is also true: if a string of characters is a valid Roman numeral, it represents only one number (that is, it can only be interpreted one way).There is only one correct way to represent a particular number as a Roman numeral.The rules for Roman numerals lead to a number of interesting observations: Now step back and consider what it would take to expand that into a two-way utility. You saw the mechanics of constructing and validating Roman numerals in “Case study: roman numerals”. In this chapter, you’re going to write and debug a set of utility functions to convert to and from Roman numerals. ![]() Discipline, I say! We had to write programs by hand, on paper, and feed them to the computer on punchcards. Write first, ship second, debug third (if ever). So spoiled by these fast computers and fancy “dynamic” languages. We have been cocksure of many things that were not so. ❝ Certitude is not the test of certainty. You are here: Home ‣ Dive Into Python 3 ‣ ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |