Python unit test (2)

Source: Internet
Author: User

Python unit test (2)
Title: Python unit test (2) date: 2015-03-04 19: 08: 20 categories: Pythontags: [Python, unit test] --- in Python unit test (1, we talked about the concept of unit test and a simple unit test example. In this example, there are only three functions, so you can print the output results of each function to the screen, and then check whether the results meet expectations with the naked eye. However, suppose there is a program with 20 classes, each class has dozens of functions, and some functions have dozens of output results. In this case, how can we see it with the naked eye? Of course, you can use if to determine if output result = expected result: return Trueelse: print U' not equal '. In this case, you find that the program has several functions, repeat the last three lines several times. Based on the concise code principle, you can write this judgment process to a function: def isequal (output, right_output): if output = right_output: return Trueelse: print U' not equal '. Congratulations, you have entered the regular stage. However, you have already done all this for you. Welcome to the unittest module. Unittest supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework. the unittest module provides classes that make it easy to support these qualities for a set of tests. the official Python documentation says that unittest supports automated testing, installation, sharing, and code disabling ...... In a word, unittest is very useful. Use readandadd once. py to demonstrate the basic usage of unittest. first, we will introduce a function of unittest, assertEqual (first, second). This function is used to check whether the value of first is equal to that of second, if not, an error is thrown. Create utest first. in The py file, enter the following code and run: #-*-coding: UTF-8-*-import unittestimport readandadd class basictest (unittest. testCase): # The class name can be set to def testread (self): # each function must start with "test". output = readandadd.read('1.txt ') self. assertEqual (output, '2, 3') def testgetnum (self): output = readandadd. getnum ('2, 3') self. assertEqual (output, ['2', '3']) def testaddnum (self): output = readandadd. addnum ([2, 3]) self. assertEqual (output, 5) if _ Name _ = '_ main _': unittest. the running result of main () is as follows :... ---------------------------------------------------------------------- Ran 3 tests in 0.001 s OK, you may say that it is just an OK and there is nothing. Then I will first put the self under the testread () function. assertEqual (output, '2, 3') is changed to self. assertEqual (output, '2, 4') is running utest. py to see how the output result is :.. F ============================================== ================================== FAIL: testread (_ main __. basictest) ---------------------------------------------------------------------- Traceback (most recent call last): File "E:/mystuff/unitest/utest. py ", line 8, in testread self. assertEqual (output, '2, 4 ') AssertionError: '2, 3 '! = '2, 4' -------------------------------------------------------------------- Ran 3 tests in 0.000 s FAILED (failures = 1) here the error location and specific error content are exactly found. Note that, at the top, there is a. F guess that it may be a location indicating an error. Keep the error of testread unchanged, and then put the following content in the testgetnum () function self. assertEqual (output, ['2', '3']) is changed to self. assertEqual (output, ['2', '6']) and then run utest. py program, the output result is as follows :. FF =================================================== ================================== FAIL: testgetnum (_ main __. basictest) ---------------------------------------------------------------------- Traceback (most recent call last): File "E:/mystuff/unitest/utest. py ", line 12, in test Getnum self. assertEqual (output, ['2', '6']) AssertionError: Lists differ: ['2', '3']! = ['2', '6'] First differing element-['2', '3']? ^ + ['2', '6']? ^ ======================================================== ================================== FAIL: testread (_ main __. basictest) ---------------------------------------------------------------------- Traceback (most recent call last): File "E:/mystuff/unitest/utest. py ", line 8, in testread self. assertEqual (output, '2, 4') AssertionError: '2, 3 '! = '2, 4' -------------------------------------------------------------------- Ran 3 tests in 0.001 s FAILED (failures = 2) can be seen, two errors are displayed here. The first line is changed to. FF. Therefore, the content of the first line should be read from right to left, which indicates the relative position of the error function in all functions. Now let's change testread () and testgetnum () back to see all the correct output :... ---------------------------------------------------------------------- Ran 3 tests in 0.000 s OK confirms that no message is the best message. This article introduces the basic usage of the unit test module unittest assertEqual. The next article will introduce the unittest module more comprehensively.

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.