Skip to content

파이썬 라이브러리 unittest #

Find similar titles

5회 업데이트 됨.

  • 최초 작성자
  • 최근 업데이트

Structured data


Introduction #

Python unit testing framework, sometimes called pyunit is a standard unit testing framework. 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.

Overview #

unittest module provides a rich set of tools for building and running tests for any cases.

How to use #

As unittest is a standard builtin Python library, we can use unittest with just importing it, like import unittest. Here is basic example how to write simple testcase using unittest.

Basic example #

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('Insilicogen'.upper(), 'INSILICOGEN')

    def test_isupper(self):

    def test_split(self):
        s = '안녕하세요 Sam 입니다'
        self.assertEqual(s.split(), ['안녕하세요', 'Sam', '입니다'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):

if __name__ == '__main__':

Command-line interface #

unittest module can be used from command-line to test modules or running different test cases.

$ python -m unittest test_module1 test_module2
$ python -m unittest test_module.TestClass
$ python -m unittest test_module.TestClass.test_method

Command-line options #

unittest supports some command-line options:

-b, --buffer #

The standard output and standard error streams are buffered during the test run. Output during a passing test is discarded. Output is echoed normally on test fail or error and is added to the failure messages.

-c, --catch #

Control-C during the test run waits for the current test to end and then reports all the results so far. A second Control-C raises the normal KeyboardInterrupt exception.

-f, --failfast #

Stop the test run on the first error or failure.

The command line can also be used for test discovery, for running all of the tests in a project or just a subset.

Test discovery #

unittest supports automatic discovering test modules if all files are importable from top of directory. The basic usage is:

cd project_directory
python -m unittest discover

Test cases #

class unittest.TestCase(methodName='runTest')

Instances of the TestCase class represent the smallest testable units in the unittest universe. This class is intended to be used as a base class, with specific tests being implemented by concrete subclasses.

Each instance of TestCase will run a single test method and also can be removed or added to TestSuite.

def suite():
    suite = unittest.TestSuite()
    return suite

Methods #

setUp() #

Method called to prepare the test fixture. This is called immediately before calling the test method.

tearDown() #

Method called immediately after the test method has been called and the result recorded.

setUpClass() #

A class method called before tests in an individual class run. Example:

def setUpClass(cls):

tearDownClass() #

A class method called after tests in an individual class have run. tearDownClass is called with the class as the only argument and must be decorated as a classmethod():

def tearDownClass(cls):

run(result=None) #

Run the test, collecting the result into the test result object passed as result.

skipTest(reason) #

Calling this during a test method or setUp() skips the current test.

debug() #

Run the test without collecting the result. This allows exceptions raised by the test to be propagated to the caller, and can be used to support running tests under a debugger.

References #


Suggested Pages #