- Example1
- Command-Line Interface
- Example2
Example1
สร้างไฟล์ test.py
import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) if __name__ == '__main__': unittest.main()
รัน unittest
> python -m unittest test.py ... ---------------------------------------------------------------------- Ran 3 tests in 0.000s OK
Command-Line Interface
The unittest module can be used from the command line to run tests from modules, classes or even individual test methods:
python -m unittest test_module1 test_module2 python -m unittest test_module.TestClass python -m unittest test_module.TestClass.test_method
You can pass in a list with any combination of module names, and fully qualified class or method names.
Test modules can be specified by file path as well:
python -m unittest tests/test_something.py
This allows you to use the shell filename completion to specify the test module. The file specified must still be importable as a module. The path is converted to a module name by removing the ‘.py’ and converting path separators into ‘.’. If you want to execute a test file that isn’t importable as a module you should execute the file directly instead.
You can run tests with more detail (higher verbosity) by passing in the -v flag:
python -m unittest -v test_module
When executed without arguments Test Discovery is started:
python -m unittest
For a list of all the command-line options:
python -m unittest -h
Changed in version 3.2: In earlier versions it was only possible to run individual test methods and not modules or classes.
The TestCase
class provides several assert methods to check for and report failures. The following table lists the most commonly used methods (see the tables below for more assert methods):
Method | Checks that | New in |
---|---|---|
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 | 3.1 |
assertIsNot(a, b) | a is not b | 3.1 |
assertIsNone(x) | x is None | 3.1 |
assertIsNotNone(x) | x is not None | 3.1 |
assertIn(a, b) | a in b | 3.1 |
assertNotIn(a, b) | a not in b | 3.1 |
assertIsInstance(a, b) | isinstance(a, b) | 3.2 |
assertN otIsInstance(a, b) | not isinstance(a, b) | 3.2 |
All the assert methods accept a msg argument that, if specified, is used as the error message on failure (see also longMessage
). Note that the msg keyword argument can be passed to assertRaises()
, assertRaisesRegex()
, assertWarns()
, assertWarnsRegex()
only when they are used as a context manager.
Example2
สร้างไฟล์ fib.py
def fib(n): if n <= 1: return n return fib(n - 1) + fib(n - 2) def main(): n = 9 result = fib(n) print(f'fib({n}) = {result}') main()
รันทดสอบ fib()
> python fib.py fib(9) = 34
สร้างไฟล์ test.py
import unittest import fib class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) def test_fib(self): self.assertEqual(fib.fib(9), 34) if __name__ == '__main__': unittest.main()
รัน unittest
> python -m unittest test.py fib(9) = 34 .... ---------------------------------------------------------------------- Ran 4 tests in 0.000s OK