Python Module unittest: Difference between revisions
(41 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
* [[Python_Language#unittest|Python Language]] | * [[Python_Language#unittest|Python Language]] | ||
* [[Python_Testing#unittest|Python Testing]] | * [[Python_Testing#unittest|Python Testing]] | ||
* [[Mocking in Python Testing]] | |||
=Overview= | =Overview= | ||
unittest mimics [[JUnit]]. | |||
=<tt>unittest</tt> vs. <tt>pytest</tt>= | =<tt>unittest</tt> vs. <tt>pytest</tt>= | ||
Line 11: | Line 13: | ||
=Concepts= | =Concepts= | ||
==Test Fixture== | ==Test Fixture== | ||
A test fixture represents the code needed to prepare the context for running one or more tests (create temporary databases, directories, etc.), and any associated cleanup actions. | A test fixture represents the code needed to prepare the context for running one or more tests (create temporary databases, directories, etc.), and any associated cleanup actions. Also see [[#Class_and_Module_Fixtures|Class and Module Fixtures]] section below. | ||
==Test Case== | ==Test Case== | ||
A test case is the individual unit of testing. In unittest, the test cases inherit from the base class <code>[[#TestCase|TestCase]]</code>. | A test case is the individual unit of testing. In unittest, the test cases inherit from the base class <code>[[#TestCase|TestCase]]</code>. | ||
==Test Suite== | ==Test Suite== | ||
A collection of test cases, test suites or both. | A collection of test cases, test suites or both. | ||
===Grouping Tests=== | |||
<font color=darkkhaki>TO PROCESS: https://docs.python.org/3/library/unittest.html#grouping-tests</font> | |||
==Test Runner== | ==Test Runner== | ||
The test runner is the component that orchestrates the execution of tests and provides the outcomes to the user. | The test runner is the component that orchestrates the execution of tests and provides the outcomes to the user. | ||
===Loading and Running Tests=== | |||
<font color=darkkhaki>TO PROCESS: https://docs.python.org/3/library/unittest.html#loading-and-running-tests</font> | |||
==<tt>unittest.mock</tt>== | ==<tt>unittest.mock</tt>== | ||
{{Internal|Python Mocking with unitest.mock|Mocking with <tt>unitest.mock</tt>}} | |||
==Organizing Test Code== | |||
<font color=darkkhaki>TO PROCESS: https://docs.python.org/3/library/unittest.html#organizing-test-code</font> | |||
=API= | =API= | ||
==<tt>TestCase</tt>== | ==<tt>TestCase</tt>== | ||
A simple [[#Test_Case|test case]] can be implemented by subclassing <code>TestCase</code> as | A simple [[#Test_Case|test case]] can be implemented by subclassing <code>TestCase</code> as shown below. The individual test methods are have names that start with <code>test</code>. This is a naming convention that informs the test runner about which methods to invoke as tests. | ||
<syntaxhighlight lang='py'> | |||
from unittest import TestCase | |||
from my_code_to_test import some_function | |||
class TestMyCode(TestCase): | |||
def test_empty(self): | |||
self.assertFalse(some_function('')) | |||
def test_lower_case(self): | |||
self.assertEqual('A', some_function('a')) | |||
def test_upper_case(self): | |||
self.assertEqual('A', some_function('A')) | |||
def test_exception(self): | |||
with self.assertRaises(ValueError): | |||
some_function('special') | |||
</syntaxhighlight> | |||
===Setup and Teardown=== | |||
<code>setUp()</code> and <code>tearDown()</code> define instructions to be executed before and after each test method execution. | |||
====<tt>setUp()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.setUp}} | |||
====<tt>tearDown()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.tearDown}} | |||
====<tt>setUpClass()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.setUpClass}} | |||
====<tt>tearDownClass()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.tearDownClass}} | |||
===Assertion Methods=== | ===Assertion Methods=== | ||
<font color=darkkhaki>These methods are used instead of the <code>assert</code> statement so the test runner can accumulate all test results and produce a report. Really? pytest can produce reports and it uses <code>assert</code>.</font> | |||
====<tt>assertEquals()</tt>==== | ====<tt>assertEquals()</tt>==== | ||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertEqual}} | |||
====<tt>assertNotEqual()</tt>==== | |||
====<tt>assertTrue()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertTrue}} | |||
====<tt>assertFalse()</tt>==== | ====<tt>assertFalse()</tt>==== | ||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertFalse}} | |||
====<tt>assertRaises()</tt>==== | ====<tt>assertRaises()</tt>==== | ||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises}} | |||
====<tt>assertIs()</tt>, <tt>assertIsNot()</tt>==== | |||
====<tt>assertIsNone()</tt>, <tt>assertIsNotNone()</tt>==== | |||
====<tt>assertIn()</tt>, <tt>assertNotIn()</tt>==== | |||
====<tt>assertIsInstance()</tt>, <tt>assertNotIsInstance()</tt>==== | |||
====<tt>assertMultiLineEqual()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertMultiLineEqual}} | |||
====<tt>assertSequenceEqual()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertSequenceEqual}} | |||
====<tt>assertListEqual()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertListEqual}} | |||
====<tt>assertTupleEqual()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertTupleEqual}} | |||
====<tt>assertSetEqual()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertSetEqual}} | |||
====<tt>assertDictEqual()</tt>==== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertDictEqual}} | |||
==<tt>TestResult</tt>== | |||
{{External|https://docs.python.org/3/library/unittest.html#unittest.TestResult}} | |||
=Operations= | |||
==Command Line Syntax== | |||
Running tests from command line: | |||
<syntaxhighlight lang='bash'> | |||
python -m unittest test_module_1 test_module_2 | |||
python -m unittest test_module.TestClass | |||
python -m unittest test_module.TestClass.test_method | |||
python -m unittest tests/test_something.py # The file must be still importable as a module. | |||
</syntaxhighlight> | |||
More verbosity: command line parameter <code>-v</code>. | |||
If the <code>unittest</code> module is executed without arguments, the module invokes [[#Test_Discovery|test discovery]]. | |||
===Command Line Options=== | |||
{{External|https://docs.python.org/3/library/unittest.html#command-line-options}} | |||
==Test Discovery== | |||
{{External|https://docs.python.org/3/library/unittest.html#test-discovery}} | |||
Run the <code>unittest</code> module without arguments: | |||
<syntaxhighlight lang='bash'> | |||
python -m unittest | |||
</syntaxhighlight> | |||
<font color=darkkhaki>TO PROCESS: https://docs.python.org/3/library/unittest.html#test-discovery</font> | |||
=Other Subjects= | |||
==Skipping Tests and Expected Failures== | |||
<font color=darkkhaki>TO PROCESS: https://docs.python.org/3/library/unittest.html#skipping-tests-and-expected-failures</font> | |||
==Distinguishing Test Iterations using Subtests== | |||
<font color=darkkhaki>TO PROCESS: https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests</font> | |||
==Class and Module Fixtures== | |||
<font color=darkkhaki>TO PROCESS: https://docs.python.org/3/library/unittest.html#class-and-module-fixtures</font> |
Latest revision as of 00:04, 28 August 2022
External
Internal
Overview
unittest mimics JUnit.
unittest vs. pytest
TODO: https://www.pythonpool.com/python-unittest-vs-pytest/
Concepts
Test Fixture
A test fixture represents the code needed to prepare the context for running one or more tests (create temporary databases, directories, etc.), and any associated cleanup actions. Also see Class and Module Fixtures section below.
Test Case
A test case is the individual unit of testing. In unittest, the test cases inherit from the base class TestCase
.
Test Suite
A collection of test cases, test suites or both.
Grouping Tests
TO PROCESS: https://docs.python.org/3/library/unittest.html#grouping-tests
Test Runner
The test runner is the component that orchestrates the execution of tests and provides the outcomes to the user.
Loading and Running Tests
TO PROCESS: https://docs.python.org/3/library/unittest.html#loading-and-running-tests
unittest.mock
Organizing Test Code
TO PROCESS: https://docs.python.org/3/library/unittest.html#organizing-test-code
API
TestCase
A simple test case can be implemented by subclassing TestCase
as shown below. The individual test methods are have names that start with test
. This is a naming convention that informs the test runner about which methods to invoke as tests.
from unittest import TestCase
from my_code_to_test import some_function
class TestMyCode(TestCase):
def test_empty(self):
self.assertFalse(some_function(''))
def test_lower_case(self):
self.assertEqual('A', some_function('a'))
def test_upper_case(self):
self.assertEqual('A', some_function('A'))
def test_exception(self):
with self.assertRaises(ValueError):
some_function('special')
Setup and Teardown
setUp()
and tearDown()
define instructions to be executed before and after each test method execution.
setUp()
tearDown()
setUpClass()
tearDownClass()
Assertion Methods
These methods are used instead of the assert
statement so the test runner can accumulate all test results and produce a report. Really? pytest can produce reports and it uses assert
.
assertEquals()
assertNotEqual()
assertTrue()
assertFalse()
assertRaises()
assertIs(), assertIsNot()
assertIsNone(), assertIsNotNone()
assertIn(), assertNotIn()
assertIsInstance(), assertNotIsInstance()
assertMultiLineEqual()
assertSequenceEqual()
assertListEqual()
assertTupleEqual()
assertSetEqual()
assertDictEqual()
TestResult
Operations
Command Line Syntax
Running tests from command line:
python -m unittest test_module_1 test_module_2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
python -m unittest tests/test_something.py # The file must be still importable as a module.
More verbosity: command line parameter -v
.
If the unittest
module is executed without arguments, the module invokes test discovery.
Command Line Options
Test Discovery
Run the unittest
module without arguments:
python -m unittest
TO PROCESS: https://docs.python.org/3/library/unittest.html#test-discovery
Other Subjects
Skipping Tests and Expected Failures
TO PROCESS: https://docs.python.org/3/library/unittest.html#skipping-tests-and-expected-failures
Distinguishing Test Iterations using Subtests
TO PROCESS: https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests
Class and Module Fixtures
TO PROCESS: https://docs.python.org/3/library/unittest.html#class-and-module-fixtures