Python-unittest (8)

Source: Internet
Author: User

Break down obscure tests into simple tests

The purpose of this article is to explain that obscure tests are not easy to understand. Then, we use an example to convert obscure test cases into simple test cases, so that the test results are clear at a glance.

Breaking down obscure tests into simple ones.

Unittest provides the means to test the Code through a series of assertions. I have often felt
The temptation to exercise extends aspects of a particle piece of code within a single test
Method. If any part fails, it becomes obscured as to which Part failed. It is preferable to split
Things up into several smaller test methods, so that when some part of the code under test
Fails, it is obvious.

1. Create a new file named recipe8.py in which to put our application code for this
Recipe.

2. Pick a class to test. In this case, we will use an alternative version of the Roman
Numeral converter, which converts both ways.

3. Create a new file called recipe8_obscure.py in which to put some longer
Test methods.

4. Create some test methods that combine several test assertions.

5. Run the obscure tests. Why did it fail? Where is the bug? It reports that II is not
Equal to I, so something appears to be off. If this the only bug?

6. Create another file called recipe8_clear.py to create a more fine-grained set
Test methods.

7. Split up the assertions into separate test methods to give a higher fidelity of output.

8. Run the clearer test suite. Is it a bit clearer where the bug is? What did we trade in
Get this higher degree of test failure? Was it worth the effort?

Test code 1:

Code# !usr/bin/env python 2.7# coding: utf-8# filename: recipe8.pyclass RomanNumeralConverter(object):    def __init__(self):        self.digit_map = {"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1}    def convert_to_decimal(self, roman_numeral):        val = 0        for char in roman_numeral:            val += self.digit_map[char]        return val    def convert_to_roman(self, decimal):        val = ""        while decimal > 1000:            val += "M"            decimal -= 1000        while decimal > 500:            val += "D"            decimal -= 500        while decimal > 100:            val += "C"            decimal -= 100        while decimal > 50:            val += "L"            decimal -= 50        while decimal > 10:            val += "X"            decimal -= 10        while decimal > 5:            val += "V"            decimal -= 5        while decimal > 1:            val += "I"            decimal -= 1        return val

 

Code# !usr/bin/env python 2.7# coding: utf-8# filename: recipe8_obscure.pyimport unittestfrom recipe8 import *class RomanNumeralTest(unittest.TestCase):    def setUp(self):        self.cvt = RomanNumeralConverter()    def test_convert_to_decimal(self):        self.assertEquals(0, self.cvt.convert_to_decimal(""))        self.assertEquals(1, self.cvt.convert_to_decimal("I"))        self.assertEquals(2010, \                          self.cvt.convert_to_decimal("MMX"))        self.assertEquals(4000, \                          self.cvt.convert_to_decimal("MMMM"))    def test_convert_to_roman(self):        self.assertEquals("", self.cvt.convert_to_roman(0))        self.assertEquals("II", self.cvt.convert_to_roman(2))        self.assertEquals("V", self.cvt.convert_to_roman(5))        self.assertEquals("XII", \                          self.cvt.convert_to_roman(12))        self.assertEquals("MMX", \                          self.cvt.convert_to_roman(2010))        self.assertEquals("MMMM", \                          self.cvt.convert_to_roman(4000))if __name__ == "__main__":    unittest.main()

 

Result output: (this result makes it hard to figure out which use cases have errors. It is not as clear as the following use cases)

. F
========================================================== ====================================
Fail: test_convert_to_roman (_ main _. romannumeraltest)
----------------------------------------------------------------------
Traceback (most recent call last ):
File "E: \ study \ Python \ 4668_code \ Chapter 1 \ 01 \ recipe8_obscure.py", line 22, in test_convert_to_roman
Self. assertequals ("II", self. CVT. convert_to_roman (2 ))
Assertionerror: 'II '! = 'I'

----------------------------------------------------------------------
Ran 2 tests in 0.001 s

Failed (failures = 1)
Process terminated with an exit code of 1

 

Test code 2:

Code# !usr/bin/env python 2.7# coding: utf-8# filename: recipe8_clear.pyimport unittestfrom recipe8 import *class RomanNumeralTest(unittest.TestCase):    def setUp(self):        self.cvt = RomanNumeralConverter()    def test_to_decimal1(self):        self.assertEquals(0, self.cvt.convert_to_decimal(""))    def test_to_decimal2(self):        self.assertEquals(1, self.cvt.convert_to_decimal("I"))    def test_to_decimal3(self):        self.assertEquals(2010, \                          self.cvt.convert_to_decimal("MMX"))    def test_to_decimal4(self):        self.assertEquals(4000, \                          self.cvt.convert_to_decimal("MMMM"))    def test_convert_to_roman1(self):        self.assertEquals("", self.cvt.convert_to_roman(0))    def test_convert_to_roman2(self):        self.assertEquals("II", self.cvt.convert_to_roman(2))    def test_convert_to_roman3(self):        self.assertEquals("V", self.cvt.convert_to_roman(5))    def test_convert_to_roman4(self):        self.assertEquals("XII", \                          self.cvt.convert_to_roman(12))    def test_convert_to_roman5(self):        self.assertEquals("MMX", \                          self.cvt.convert_to_roman(2010))    def test_convert_to_roman6(self):        self.assertEquals("MMMM", \                          self.cvt.convert_to_roman(4000))if __name__ == "__main__":    unittest.main()

Result output:

. Fffff ....
========================================================== ====================================
Fail: test_convert_to_roman2 (_ main _. romannumeraltest)
----------------------------------------------------------------------
Traceback (most recent call last ):
File "E: \ study \ Python \ 4668_code \ Chapter 1 \ 01 \ recipe8_clear.py", line 30, in test_convert_to_roman2
Self. assertequals ("II", self. CVT. convert_to_roman (2 ))
Assertionerror: 'II '! = 'I'

========================================================== ====================================
Fail: test_convert_to_roman3 (_ main _. romannumeraltest)
----------------------------------------------------------------------
Traceback (most recent call last ):
File "E: \ study \ Python \ 4668_code \ Chapter 1 \ 01 \ recipe8_clear.py", Line 33, in test_convert_to_roman3
Self. assertequals ("V", self. CVT. convert_to_roman (5 ))
Assertionerror: 'V '! = 'Iii'

========================================================== ====================================
Fail: test_convert_to_roman4 (_ main _. romannumeraltest)
----------------------------------------------------------------------
Traceback (most recent call last ):
File "E: \ study \ Python \ 4668_code \ Chapter 1 \ 01 \ recipe8_clear.py", line 37, in test_convert_to_roman4
Self. CVT. convert_to_roman (12 ))
Assertionerror: 'xii '! = 'Xi'

========================================================== ====================================
Fail: test_convert_to_roman5 (_ main _. romannumeraltest)
----------------------------------------------------------------------
Traceback (most recent call last ):
File "E: \ study \ Python \ 4668_code \ Chapter 1 \ 01 \ recipe8_clear.py", line 41, in test_convert_to_roman5
Self. CVT. convert_to_roman (2010 ))
Assertionerror: 'mmx '! = 'Mmviii'

========================================================== ====================================
Fail: test_convert_to_roman6 (_ main _. romannumeraltest)
----------------------------------------------------------------------
Traceback (most recent call last ):
File "E: \ study \ Python \ 4668_code \ Chapter 1 \ 01 \ recipe8_clear.py", line 45, in test_convert_to_roman6
Self. CVT. convert_to_roman (4000 ))
Assertionerror: 'mmmm '! = 'Mmmdcccclxxxxviii'

----------------------------------------------------------------------
Ran 10 tests in 0.001 s

Failed (failures = 5)
Process terminated with an exit code of 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.