Skip to main content
skill CX Codex

unittest-skill — Python unittest skill for Claude Code

Generates Python unittest tests with TestCase, setUp/tearDown, and assertion methods. Triggers when you mention unittest, TestCase, or self.assertEqual.

MIT 235 installs ★ 393 updated 2026-10-13

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add LambdaTest/agent-skills --skill unittest-skill -a claude-code
Open on GitHub

What unittest-skill does

The skill provides patterns for writing tests: basic TestCase with lifecycle hooks, a full set of assertion methods (assertEqual, assertTrue, assertRaises, and more), parameterized tests via subTest, and mocking dependencies. Examples cover function and class testing, exception handling, mocking external services, and test lifecycle setup.

You need Python and the built-in unittest library, which comes with every Python installation. The skill requires no additional dependencies. Run tests with python -m unittest or auto-discover with discover.

What it helps with

  • write a unit test for a Python function or class
  • check edge cases and exceptions in unittest
  • set up setUp and tearDown for test data preparation
  • mock external dependencies using unittest.mock
  • parameterize tests with multiple input values

Example prompts for the agent

  • Write a unittest for a calculate_tax function with basic cases and division-by-zero error
  • Create a TestCase that mocks an API call and verifies method calls
  • Write a parameterized test using subTest to check add with different number pairs

How to install unittest-skill

1Skills CLI (Vercel)

npx skills add LambdaTest/agent-skills --skill unittest-skill -a claude-code

2Manually: put the files in place

~/.claude/skills/unittest-skill/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/unittest-skill/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about unittest-skill

How do I install unittest-skill in Claude Code?

Run npx skills add LambdaTest/agent-skills --skill unittest-skill -a claude-code in your terminal, then check the config at ~/.claude/skills/unittest-skill/SKILL.md.

What does unittest-skill do?

Generates Python unittest test patterns: TestCase structure, assertion methods (assertEqual, assertRaises, etc.), dependency mocking, and parameterized tests using subTest.

Which platforms does the skill support?

Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, and OpenCode. Each platform has its own installation command.

Source text

SKILL.md · MIT
---
name: unittest-skill
description: >
  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).
languages:
  - Python
category: unit-testing
license: MIT
metadata:
  author: TestMu AI
  version: "1.0"
---

# 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`.

© TestMu AI · MIT · LambdaTest/agent-skills

Similar skills

All skills for Testing and QA →

From the same set47

The whole set →
flutter-testing-skill flutter-testing-skill generates widget tests, integration tests, and golden tests for Flutter in Dart. Runs locally or on real devices via TestMu AI cloud. CX Codex +1 242installs smartui-skill This skill prepares SmartUI visual regression tests for screenshot comparison. Use it to check pages and components across browsers and viewports. CX Codex +1 237installs jasmine-skill Generates test specs in Jasmine for JavaScript and TypeScript. Includes BDD patterns, spy and mock syntax, async support — the code editor writes describe, it, expect, and spy calls automatically. CX Codex +1 237installs mocha-skill Creates Mocha tests for JavaScript and TypeScript with Chai assertions and Sinon mocks. Useful when a project needs unit-test scaffolding. CX Codex +1 235installs espresso-skill Generate Espresso UI tests for Android apps written in Kotlin or Java. Use the skill to exercise screen interactions and check what the interface displays afterward. CX Codex +1 234installs laravel-dusk-skill Generates E2E tests with Laravel Dusk. The agent writes PHP code with $browser->visit, form checks, and Page Objects. CX Codex +1 231installs test-framework-migration-skill test-framework-migration-skill converts E2E test scripts between Selenium, Playwright, Puppeteer, and Cypress. It rewrites selectors, assertions, and lifecycle methods automatically. CX Codex +1 229installs behave-skill Generate Python BDD tests with Gherkin feature files and Behave step implementations. Useful when you need to turn user flows into runnable scenarios. CX Codex +1 227installs

Project sponsors

Companies that keep this analytics open