Python Unit Test Framework unittest how to use it

Source: Internet
Author: User
Overview

1. Testing the Scaffold (test fixture)

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

2. Testing cases (test case)

The smallest test unit.

3. Testing Suite (test suite)

A collection of test cases.

4. Test Runner (runner)

The component that tests the execution.

Command line interface

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

The code is 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

The code is as follows:


Python-m unittest-v Test_module


test Case Auto Search

The unittest supports a simple test discovery. After the command line passes in discovery, the framework automatically searches the current directory for the cases to be tested and executes. The search directory must be a package or a module. Basic use is as follows:

The code is as follows:


CD Project_directory
Python-m UnitTest Discover


The sub-options are as follows:
-v,–verbose
Verbose level of output information

-s,–start-directory Directory
Start Search directory (default to current directory)

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

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

Create test code

1. Mode One

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

The code is as follows:


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

Run

2. Mode II

Write a method that starts with test

The code is 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


build a test suite

Way One

The code is as follows:


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

Way Two (recommended)

The code is as follows:


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


Way three (recommended)

The code is 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

The code is as follows:


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


Mode Five

UnitTest's Testloader provides a default test suite to build

The code is as follows:


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

Ignore test Cases (Python2.7 support)

Can be unconditionally ignored and conditional ignored, through the adorner implementation

The code is 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 for 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


test classes can also be ignored

The code is 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.