kopia lustrzana https://github.com/animator/learn-python
186 wiersze
4.2 KiB
Markdown
186 wiersze
4.2 KiB
Markdown
# Unittest in Python
|
|
|
|
The `unittest` module in Python is a powerful and flexible framework for writing and running tests. It provides a range of tools for constructing and running tests, making it easier to ensure your code behaves as expected.
|
|
|
|
### Writing Tests
|
|
|
|
A typical test case is created by subclassing `unittest.TestCase` and defining methods that start with `test`.
|
|
|
|
### Example: Basic Test Case
|
|
|
|
```python
|
|
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'])
|
|
with self.assertRaises(TypeError):
|
|
s.split(2)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|
|
```
|
|
|
|
#### Output
|
|
|
|
When you run this script, the output will be:
|
|
|
|
```python
|
|
...
|
|
----------------------------------------------------------------------
|
|
Ran 3 tests in 0.001s
|
|
|
|
OK
|
|
```
|
|
|
|
### Running Tests
|
|
|
|
You can run tests by calling the script directly. When executed, `unittest` will find all test cases and run them:
|
|
|
|
```bash
|
|
python test_script.py
|
|
```
|
|
|
|
Alternatively, you can use the command line to discover and run tests:
|
|
|
|
```bash
|
|
python -m unittest discover
|
|
```
|
|
|
|
### Test Fixtures
|
|
|
|
Test fixtures are used to set up a known state for tests and clean up afterward. You can use `setUp()` and `tearDown()` methods for this purpose.
|
|
|
|
### Example: Test Fixtures
|
|
|
|
```python
|
|
import unittest
|
|
|
|
class TestExample(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.number = 42
|
|
|
|
def tearDown(self):
|
|
del self.number
|
|
|
|
def test_addition(self):
|
|
self.assertEqual(self.number + 10, 52)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|
|
```
|
|
|
|
#### Output
|
|
|
|
```python
|
|
.
|
|
----------------------------------------------------------------------
|
|
Ran 1 test in 0.000s
|
|
|
|
OK
|
|
```
|
|
|
|
### Organizing Tests
|
|
|
|
Tests can be organized into test suites to run them collectively. This is useful for running related tests together.
|
|
|
|
### Example: Test Suites
|
|
|
|
```python
|
|
import unittest
|
|
|
|
class TestMath(unittest.TestCase):
|
|
|
|
def test_add(self):
|
|
self.assertEqual(1 + 1, 2)
|
|
|
|
def test_subtract(self):
|
|
self.assertEqual(5 - 3, 2)
|
|
|
|
class TestString(unittest.TestCase):
|
|
|
|
def test_upper(self):
|
|
self.assertEqual('foo'.upper(), 'FOO')
|
|
|
|
def test_isupper(self):
|
|
self.assertTrue('FOO'.isupper())
|
|
|
|
if __name__ == '__main__':
|
|
suite = unittest.TestSuite()
|
|
suite.addTests(unittest.makeSuite(TestMath))
|
|
suite.addTests(unittest.makeSuite(TestString))
|
|
runner = unittest.TextTestRunner()
|
|
runner.run(suite)
|
|
```
|
|
|
|
#### Output
|
|
|
|
```python
|
|
....
|
|
----------------------------------------------------------------------
|
|
Ran 4 tests in 0.001s
|
|
|
|
OK
|
|
```
|
|
|
|
### Assertions
|
|
|
|
`unittest` provides a wide range of assertion methods to verify conditions in tests:
|
|
|
|
- `assertEqual(a, b)`: Check `a == b`
|
|
- `assertNotEqual(a, b)`: Check `a != b`
|
|
- `assertTrue(x)`: Check `bool(x) is True`
|
|
- `assertFalse(x)`: Check `bool(x) is False`
|
|
- `assertIs(a, b)`: Check `a is b`
|
|
- `assertIsNot(a, b)`: Check `a is not b`
|
|
- `assertIsNone(x)`: Check `x is None`
|
|
- `assertIsNotNone(x)`: Check `x is not None`
|
|
- `assertIn(a, b)`: Check `a in b`
|
|
- `assertNotIn(a, b)`: Check `a not in b`
|
|
- `assertIsInstance(a, b)`: Check `isinstance(a, b)`
|
|
- `assertNotIsInstance(a, b)`: Check `not isinstance(a, b)`
|
|
|
|
### Example: Using Assertions
|
|
|
|
```python
|
|
import unittest
|
|
|
|
class TestAssertions(unittest.TestCase):
|
|
|
|
def test_assert_equal(self):
|
|
self.assertEqual(3 + 2, 5)
|
|
|
|
def test_assert_true(self):
|
|
self.assertTrue('FOO'.isupper())
|
|
|
|
def test_assert_in(self):
|
|
self.assertIn('h', 'hello')
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|
|
```
|
|
|
|
#### Output
|
|
|
|
```python
|
|
...
|
|
----------------------------------------------------------------------
|
|
Ran 3 tests in 0.001s
|
|
|
|
OK
|
|
```
|
|
|
|
|
|
The `unittest` module is a powerful framework that provides all the tools necessary to write comprehensive and maintainable tests.
|
|
By leveraging its features, you can ensure your code works correctly and prevent regressions, ultimately leading to more robust and reliable software.
|