Use unittest in Python to implement simple unit test instance details, pythonunittest

Source: Internet
Author: User

Use unittest in Python to implement simple unit test instance details, pythonunittest

Preface

The importance of unit testing is not much said. The hateful thing is that there are too many unit testing frameworks and tools in Python, what are unittest, testtools, subunit, coverage, testrepository, nose, mox, mock, fixtures, discover, coupled with setuptools, distutils, and so on, doesn't talk about how to write unit tests first. Just how to run unit tests has multiple N methods, and because it is a test rather than a function, it is something that many people are not interested in. But as a good programmer, you must not only write functional code, but also test code to demonstrate your strength. So many frameworks and tools are easy to confuse. The reason for confusion is that they do not understand its basic principles. If some basic concepts are unclear, how can I write test code with clear thinking?

Today's topic is unittest. As a module in standard python, it is the basis of other frameworks and tools. Its reference is its official documentation: development.

The instance is as follows:

First, a Python module to be tested is provided. The Code is as follows:

Program to be tested: date_service.pyPython

# Coding: utf8''' common date class @ author: www.crazyant.net ''' def get_date_year_month (pm_date ): "obtain the year and month corresponding to the pm_date parameter" 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

Then you can write a test script with the following code:

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): "initialize resources here" pass def tearDown (self ): "release resources here" "pass def test_get_date_year_month_1 (self):" "test method 1, the test method should start with test _ "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): "" test method 1, the test method should start with test _ "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 mainif _ name _ = "_ main _": unittest. main ()

Run test_date_service.py to print the following information:

Test result

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

Each point here indicates that a test is successfully run, and the number of tests and the testing time are displayed.

For many times, I have never known the purpose of writing a single test, because a single test only runs the program once and does not create a new logic, I was wondering, "I have already written the program according to my ideas, and it will run according to my design. Why should I use a single test to repeat it? ", After that, I encountered a BUG in the Code. After debugging for a long time, I found out that the problem was "obja. equals (objb) ", because obja and objb are Long and Integer, they are not equal even if the values are the same.

From that moment on, I found that the single test is actually "Whether the verification program is running according to my ideas", which is the ultimate goal,, this is a key thing. The design is often correct, but the code written is often not run as we think.

A single test is to verify whether the code is running as we think, which is also the significance of this technology.

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

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.