[Python2.7] Summary of how to use unittest

manual

25.3. Unittest — Unittest Framework — Python 2.7.x Documentation

template

Some people have already created a template, so I think you should refer to the following. python unittest template-Qiita

The following is an example of a minimum configuration test.

Minimum configuration test example


import unittest

class UnitTest(unittest.TestCase):
    def test1(self):
        self.assertEqual(1, 1)

if __name__ == '__main__':
    unittest.main()

Execution example


.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

※important point The test to be executed must start with test in the method name.

assert method

Method Evaluation contents
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
assertNotIsInstance(a, b) not isinstance(a, b)

Please refer to the following for details. There are many others. 25.3. Unittest — Unittest Framework — Python 2.7.x Documentation-

Executes specific processing at initialization / termination

At initialization / termination of the entire Test

Executed when the entire Test is initialized


def setUpModule():
    pass

Run at the end of the entire Test


def tearDownModule():
    pass

TestCase initialization / termination

Executed at test case initialization


class HogeTest(unittest.TestCase):
#...
    @classmethod
    def setUpClass(cls):
        pass

Execute at the end of TestCase


class HogeTest(unittest.TestCase):
#...
    @classmethod
    def tearDownClass(cls):
        pass

Test Fixture (Each test in TastCase) At initialization / end

Executed at test case initialization


class HogeTest(unittest.TestCase):
#...
    def setUp(self):
        pass

Execute at the end of TestCase


class HogeTest(unittest.TestCase):
#...
    def tearDown(self):
        pass

Execution order

Here is the log when the following test is executed.

Execution order confirmation sample of each function (excerpt)


def setUpModule():
    print '## setUpModule!'

def tearDownModule():
    print '## tearDownModule!'
    print unittest.TestResult

class HogeTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print '## setUpClass!'
        pass

    @classmethod
    def tearDownClass(cls):
        print '## tearDownClass!'

    def setUp(self):
        print '## setUp!'

    def tearDown(self):
        print '## tearDown!'

    def test1(self):
        expected = 1
        actual = 1
        self.assertEqual(expected, actual)

Execution result


.....
----------------------------------------------------------------------
Ran 1 tests in 0.000s

OK
## setUpModule!
## setUpClass!
## setUp!
## tearDown!
## tearDownClass!
## tearDownModule!

Get test results

You can get it with TestResult class. In the frequently used ʻunittest.main () `, implement as follows.

unittest.main()When getting in


def main()
    #By default sys.exit()Will be called, so exit=Specify False
    test_program = unittest.main(exit=False)
    test_result = test_program.result

Please refer to the following for the contents that can be obtained by TestResult.

25.3. Unittest — Unit test framework — Python 2.7.x documentation

Get the execution time of each test as a variable

python - How to know time spent on each test when using unittest? - Stack Overflow

reference

python unittest template-Qiita Library: unittest --Life with Python python - Unittest causing sys.exit() - Stack Overflow When writing a test with python unittest, use doCleanups for setUp that may fail --Qiita

Recommended Posts

[Python2.7] Summary of how to use unittest
[Python] Summary of how to use pandas
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
Summary of how to use MNIST in Python
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
Summary of how to use csvkit
[Question] How to use plot_surface of python
[Python] Summary of how to use split and join functions
[Python] How to use two types of type ()
python3: How to use bottle (2)
Summary of how to import files in Python 3
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
Summary of studying Python to use AWS Lambda
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
I tried to summarize how to use matplotlib of python
How to use Python Kivy ① ~ Basics of Kv Language ~
[Python] Summary of how to specify the color of the figure
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
[Python] How to use Typetalk API
[python] Summary of how to retrieve lists and dictionary elements
Comparison of how to use higher-order functions in Python 2 and 3
[Introduction to Python] How to use class in Python?
scikit-learn How to use summary (machine learning)
How to install and use pandas_datareader [Python]
[python] How to use __command__, function explanation
How to calculate Use% of df command
[Python] How to use import sys sys.argv
[Python] Organizing how to use for statements
python: How to use locals () and globals ()
How to use __slots__ in Python class
Jupyter Notebook Basics of how to use
How to use "deque" for Python data
Basics of PyTorch (1) -How to use Tensor-
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
How to use regular expressions in Python
How to use is and == in Python
[Blender x Python] How to use modifiers
Summary of how to write AWS Lambda
How to use Python Kivy (reference) -I translated Kivy Language of API reference-