Python Unit Test Framework UnitTest use method to explain _python

Source: Internet
Author: User

Overview

1. Testing Scaffold (Test fixture)

The work to be done before the test is prepared and the work to be done after the test is completed. Includes setup () and teardown ().

2. Testing cases (test case)

The smallest test unit.

3. Testing Suites (test suite)

A collection of test cases.

4. Test Runner

The component that the test executes.

Command-line interface

You can run test modules, test classes, and test methods using the command line.

Copy Code code as follows:

Python-m unittest Test_module1 Test_module2
Python-m unittest Test_module. TestClass
Python-m unittest Test_module. Testclass.test_method

Can add-V print details
Copy Code code as follows:

Python-m unittest-v Test_module

test Case Auto Search

UnitTest supports a simple test discovery. After the command line is passed in discovery, the framework automatically searches the current directory for the case to test and executes it. The search directory must be a package or module. The basic use is as follows:

Copy Code code as follows:

CD Project_directory
Python-m UnitTest Discover

Sub options are as follows:
-v,–verbose
Detail level of output information

-s,–start-directory Directory
Start the search directory (default is the current directory)

-p,–pattern pattern
Matching file name (default is test*.py)

-t,–top-level-directory Directory
Top-level directory for search (default is start directory)

Creating test Code

1. Mode One

Create subclass Inheritance UnitTest. TestCase, and then rewrite the following methods

Copy Code code as follows:

Class Widgettestcase (UnitTest. TestCase):
def setUp (self):
Pass
def runtest (self):
Pass
def teardown (self):
Pass

Run

2. Mode Two

Write a method that starts with test

Copy Code code as follows:

Class Widgettestcase (UnitTest. TestCase):
def setUp (self):
Pass

def test_xx1 (self)
def test_xx2 (self)
...
def test_xxn (self)

def teardown (self):
Pass


Building test Suites

Way One

Copy Code code as follows:

Widgettestsuite = UnitTest. TestSuite ()
Widgettestsuite.addtest (widgettestcase (' test_default_size '))
Widgettestsuite.addtest (widgettestcase (' test_resize '))

Mode two (recommended)

Copy Code code as follows:

Def Suite ():
Suite = UnitTest. TestSuite ()
Suite.addtest (widgettestcase (' test_default_size '))
Suite.addtest (widgettestcase (' test_resize '))
Return Suite

Mode III (recommended)
Copy Code code as follows:

Def Suite ():
Tests = [' test_default_size ', ' test_resize ']
Return unittest. TestSuite (Map (widgettestcase, tests))

Mode Four

Multiple test suites built into larger test suites

Copy Code code as follows:

Suite1 = Module1. Thetestsuite ()
Suite2 = Module2. Thetestsuite ()
alltests = UnitTest. TestSuite ([Suite1, Suite2])

Mode Five

UnitTest's Testloader provides a default test suite

Copy Code code as follows:

Suite = UnitTest. Testloader (). Loadtestsfromtestcase (Widgettestcase)

Ignore test Cases (Python2.7 support)

Can be divided unconditionally ignore and conditional ignore, through the adorner implementation

Copy Code code as follows:

Class Mytestcase (UnitTest. TestCase):

@unittest. Skip ("Demonstrating skipping")
def test_nothing (self):
Self.fail ("shouldn ' t happen")

@unittest. SkipIf (Mylib.__version__ < (1, 3),
"Not supported in this library version")
def test_format (self):
# Tests that work to only a certain version of the library.
Pass

@unittest. Skipunless (Sys.platform.startswith ("Win"), "requires Windows")
def test_windows_support (self):
# Windows Specific testing code
Pass


The test class can also ignore
Copy Code code as follows:

@unittest. Skip ("Showing class skipping")
Class Myskippedtestcase (UnitTest. TestCase):
def test_not_run (self):
Pass

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.