Detailed explanation of unittest code in Python, pythonunittest

Source: Internet
Author: User

Detailed explanation of unittest code in Python, pythonunittest

Preface

When writing a function or class, you can also write a test for it. Through testing, you can determine that the code can work as required for various inputs.

This article describes how to use the unittest tool in the Python module to test the code.

Test Functions

First, we compile a simple function that accepts three parameters: surname, name, and intermediate name, and returns the complete name:

Names. py

Def get_fullname (firstname, lastname, middel = ''): ''' create full name ''' if middel: full_name = firstname + ''+ middel +'' + lastname return full_name.title () else: full_name = firstname + ''+ lastname return full_name.title ()

Then write the calling function program in the current directory.

Get_name.py

from names import get_fullnamemessage = "Please input 'q' to quit."print(message)while True:  first = input("Please input your firstname: ")  if first == 'q':    break  last = input("Please input your lastname: ")  if last == 'q':    break  middels = input("Please input your middel name or None: ")  if last == 'q':    break  formant_name = get_fullname(first,last,middels)  print("\tYour are fullname is: " + formant_name.title())

Call result:

Please input 'q' to quit.
Please input your firstname: xiao
Please input your lastname: peng
Please input your middel or None:
Your are fullname is: Xiao Peng
Please input your firstname: xiao
Please input your lastname: peng
Please input your middel or None: you
Your are fullname is: Xiao You Peng
Please input your firstname: q

Process ended. Exit code 0

Create a test program

It takes some time to get used to the syntax for creating a test case. However, after creating a test case, it is easy to test the unit of a function. Import the unittest module and the function to be tested, and create a class that inherits the unittest. TestCase function,

Compile a series of methods to test the function behavior.

The following describes how to test whether the names. py function can obtain the correct name:

Test_get_name.py

Import unittestfrom names import get_fullnameclass NamesTestCase (unittest. testCase): ''' defines the test class ''' def test_get_name2 (self): ''' test the name of two words ''' formatied_name2 = get_fullname ('xiao ', 'pengyou') self. assertEqual (formatied_name2, 'xiao pengyou') def test_get_name3 (self): ''' test the name of the three words ''' formatied_name3 = get_fullname ('xiao', 'peng ', middel = 'you') self. assertEqual (formatied_name3, 'xiao Peng you') if _ name _ = '_ init _': unittest. main ()

Test results:

Ran 2 tests in 0.034 s

OK

Two test units passed the test!

A test report is generated in the current large Directory, which can be opened in a browser.

The figure shows that the two tests passed and the test time is displayed !!!

Various assertions of unittest. TestCase

Unittest asserted Methods

Method Chinamoocs
AssertEqual (a, B) Verify a = B
AssertNotEqual (a, B) Verify! = B
AssertTrue (x) Verify that x is True
AssertFalse (x) Verify that x is False
AssertIn (item, list) Verify that item is in list
AssertNotIn (item, list) Verifying that item is not in list

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.