unittest-skill
Generates Python unittest tests. Built-in testing framework with TestCase, setUp/tearDown, and assertion methods. Use when user mentions "unittest", "TestCase", "self.assertEqual", "Python unittest". Triggers on: "unittest", "TestCase", "self.assertEqual", "Python unittest" (not pytest).
What this skill does
# Python unittest Skill
## Core Patterns
### Basic Test
```python
import unittest
class TestCalculator(unittest.TestCase):
def setUp(self):
self.calc = Calculator()
def test_add(self):
self.assertEqual(self.calc.add(2, 3), 5)
def test_divide_by_zero(self):
with self.assertRaises(ZeroDivisionError):
self.calc.divide(10, 0)
def test_multiple_assertions(self):
self.assertEqual(self.calc.add(2, 2), 4)
self.assertEqual(self.calc.subtract(5, 3), 2)
self.assertAlmostEqual(self.calc.divide(10, 3), 3.333, places=3)
def tearDown(self):
pass # cleanup
if __name__ == '__main__':
unittest.main()
```
### Assertions
```python
self.assertEqual(a, b)
self.assertNotEqual(a, b)
self.assertTrue(condition)
self.assertFalse(condition)
self.assertIsNone(obj)
self.assertIsNotNone(obj)
self.assertIs(a, b) # same object
self.assertIn(item, collection)
self.assertNotIn(item, collection)
self.assertIsInstance(obj, cls)
self.assertAlmostEqual(a, b, places=5)
self.assertGreater(a, b)
self.assertLess(a, b)
self.assertRegex(str, r'\d+')
self.assertCountEqual(a, b) # same elements, any order
# Exception
with self.assertRaises(ValueError) as ctx:
raise ValueError("bad")
self.assertIn("bad", str(ctx.exception))
# Warning
with self.assertWarns(DeprecationWarning):
deprecated_function()
```
### SubTest (Parameterized)
```python
def test_add_multiple(self):
test_cases = [(2, 3, 5), (-1, 1, 0), (0, 0, 0)]
for a, b, expected in test_cases:
with self.subTest(a=a, b=b):
self.assertEqual(self.calc.add(a, b), expected)
```
### Mocking
```python
from unittest.mock import patch, MagicMock, Mock
class TestUserService(unittest.TestCase):
@patch('myapp.service.UserRepository')
@patch('myapp.service.EmailService')
def test_create_user(self, MockEmail, MockRepo):
mock_repo = MockRepo.return_value
mock_repo.save.return_value = User(1, 'Alice')
service = UserService()
result = service.create_user('[email protected]', 'Alice')
self.assertEqual(result.id, 1)
mock_repo.save.assert_called_once()
MockEmail.return_value.send_welcome.assert_called_with('[email protected]')
```
### Lifecycle
```
setUpClass() → Once before all tests (classmethod)
setUp() → Before each test
test_method() → Test
tearDown() → After each test
tearDownClass() → Once after all tests (classmethod)
```
## Run: `python -m unittest` or `python -m unittest test_module.TestClass.test_method`
## Discover: `python -m unittest discover -s tests -p "test_*.py"`
## Deep Patterns
For advanced patterns, debugging guides, CI/CD integration, and best practices,
see `reference/playbook.md`.
Related in unit-testing
phpunit-skill
IncludedGenerates PHPUnit tests in PHP. Covers assertions, data providers, mocking, and test doubles. Use when user mentions "PHPUnit", "TestCase", "assertEquals", "PHP test". Triggers on: "PHPUnit", "TestCase PHP", "assertEquals PHP", "PHP unit test".
karma-skill
IncludedGenerates Karma test runner configurations for browser-based JavaScript testing. Works with Jasmine, Mocha, or QUnit. Use when user mentions "Karma", "karma.conf.js", "browser test runner". Triggers on: "Karma", "karma.conf", "karma test runner", "browser-based JS test".
testng-skill
IncludedGenerates TestNG tests in Java with groups, data providers, parallel execution, XML suite configuration, and listeners. Use when user mentions "TestNG", "@DataProvider", "testng.xml", "groups". Triggers on: "TestNG", "@DataProvider", "testng.xml", "TestNG suite", "parallel tests Java".
jest-skill
IncludedGenerates Jest unit and integration tests in JavaScript or TypeScript. Covers mocking, snapshots, async testing, and React component testing. Use when user mentions "Jest", "describe/it/expect", "jest.mock", "toMatchSnapshot". Triggers on: "Jest", "expect().toBe()", "jest.mock", "snapshot test", "JS test", "React test".
junit-5-skill
IncludedGenerates production-grade JUnit 5 unit and integration tests in Java. Covers assertions, parameterized tests, lifecycle hooks, mocking with Mockito, and nested tests. Use when user mentions "JUnit", "JUnit 5", "@Test", "assertEquals", "Assertions", "Java unit test". Triggers on: "JUnit", "@Test", "assertEquals", "Java test", "unit test Java".
mstest-skill
IncludedGenerates MSTest tests in C#. Microsoft's built-in testing framework for .NET. Use when user mentions "MSTest", "[TestMethod]", "[TestClass]", "Assert.AreEqual". Triggers on: "MSTest", "[TestMethod]", "[TestClass]", "Microsoft test framework".