Using Python unittest to achieve a simple unit test example elaboration

  • 2020-05-19 05:08:32
  • OfStack

preface

The importance of unit testing is not much said, the thing is Python too many unit testing frameworks and tools, in what unittest, testtools, subunit, coverage, testrepository, nose, mox, mock, fixtures, discover, plus setuptools, distutils etc. Of these, regardless of how to write unit tests, just how to run the unit tests has a variety of methods, N again because it is not functional testing, is a lot of people are not interested to touch something. However, as an excellent programmer, you should not only write good functional code, but also write good test code to show your strength. So many frameworks and tools, it is easy to get confused, confused because they do not understand its basic principles, if 1 some basic concepts are not clear, how can you write clear thinking test code?

Today's topic is unittest, as a module in the standard python, is the foundation of other frameworks and tools, and resources is the official document: http: / / docs python. org / 2.7 / library/unittest html and source code, the document has been written very well, in this paper, we give a instance, very simple, see under 1.

Instance as follows

Firstly, an Python module to be tested is given. The code is as follows:

Procedure to be tested: date_service.pyPython


# coding:utf8
'''
 Common date class 
 
@author: www.crazyant.net
'''
 
def get_date_year_month(pm_date):
 """ To obtain parameters pm_date The corresponding year and month 
 """
 if not pm_date:
  raise Exception("get_curr_year_month: pm_date can not be None")
 
 # get date's yyyymmddHHMMSS pattern
 str_date = str(pm_date).replace("-", "").replace(" ", "").replace(":", "")
 
 year = str_date[:4]
 month = str_date[4:6]
 return year, month

You can then write a test script that looks like this:

Test procedure: test_date_service.pyPython


# coding: utf8
 
"""
 test date_service.py
 
@author: peishuaishuai
"""
 
import unittest
 
from service import date_service
 
class DateServiceTest(unittest.TestCase):
 """
 test clean_tb_async_src_acct.py
 """
 
 def setup(self):
  """ Do the initialization of the resource here  """
  pass
 
 def tearDown(self):
  """ Do the release of resources here  """
  pass
 
 def test_get_date_year_month_1(self):
  """  The test method 1 , the test method should be test_ At the beginning  """
  
  pm_date = "2015-11-25 14:40:52"
  year, month = date_service.get_date_year_month(pm_date)
  self.assertEqual(year, "2015", "year not equal")
  self.assertEqual(month, "11", "month not equal")
 
 def test_get_date_year_month_2(self):
  """  The test method 1 , the test method should be test_ At the beginning  """
  pm_date = "20161225144052"
  year, month = date_service.get_date_year_month(pm_date)
  self.assertEqual(year, "2016", "year not equal")
  self.assertEqual(month, "12", "month not equal")
 
 
# test main
if __name__ == "__main__":
 unittest.main()

Run this test_date_service.py, and the following message will be printed:

Run test results


..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
 
OK

Each point here represents a successful run of one test, and it will give you the total number of successful runs and the duration of the test.

1 straight before a lot of time, I don't know to write what's the use of a single measurement, because single measurement is the written application for 1 times, did not create a new logic, my in the mind wondering "I have the program written according to my idea, it will run according to my design, why want to use a single measurement to go 1 times?" After debugging for a long time, I found that the problem was "obja.equals (objb)", because obja and objb1 are Long1 and Integer, so even if the values are the same, they will not be the same.

From that moment on, I found that what the single test does is, in fact, "verify whether the program is running as I want it to run", which is the ultimate goal of it. However, this is the key thing. The design is usually correct, but the code often does not run as we want it to run.

Monotone is to verify that the code is running as we expect it to, and that's what monotone is all about.

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: