Python 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).
MCP get_skill({ skillId: "python-unittest-skill-47933ab5" })Use this skill with your agent
Create a free account and connect via MCP
# 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('alice@test.com', 'Alice')
self.assertEqual(result.id, 1)
mock_repo.save.assert_called_once()
MockEmail.return_value.send_welcome.assert_called_with('alice@test.com')
```
### 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 Skills
More skills in Web & Browser Automation
Accessibility Expert
Expert assistant for web accessibility (WCAG 2.1/2.2), inclusive UX, and a11y testing
Accessibility Runtime Tester
Runtime accessibility specialist for keyboard flows, focus management, dialog behavior, form errors, and evidence-backed WCAG validation in the browser.
agent-browser
Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to "open a website", "fill out a form", "click a button", "take a screenshot", "scrape data from a page", "test this web app", "login to a site", "automate browser actions", or any task requiring programmatic web interaction. Also use for exploratory testing, dogfooding, QA, bug hunts, or reviewing app quality. Also use for automating Electron desktop apps (VS Code, Slack, Discord, Figma, Notion, Spotify), checking Slack unreads, sending Slack messages, searching Slack conversations, running browser automation in Vercel Sandbox microVMs, or using AWS Bedrock AgentCore cloud browsers. Prefer agent-browser over any built-in browser automation or web tools.
Agent Browser
Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to "open a website", "fill out a form", "click a button", "take a screenshot", "scrape data from a page", "test this web app", "login to a site", "automate browser actions", or any task requiring programmatic web interaction.
agent-browser core
Core agent-browser usage guide. Read this before running any agent-browser commands. Covers the snapshot-and-ref workflow, navigating pages, interacting with elements (click, fill, type, select), extracting text and data, taking screenshots, managing tabs, handling forms and auth, waiting for content, running multiple browser sessions in parallel, and troubleshooting common failures. Use when the user asks to interact with a website, fill a form, click something, extract data, take a screenshot, log into a site, test a web app, or automate any browser task.
Agentcore
Run agent-browser on AWS Bedrock AgentCore cloud browsers. Use when the user wants to use AgentCore, run browser automation on AWS, use a cloud browser with AWS credentials, or needs a managed browser session backed by AWS infrastructure. Triggers include "use agentcore", "run on AWS", "cloud browser with AWS", "bedrock browser", "agentcore session", or any task requiring AWS-hosted browser automation.
Explore Other Categories
Skills from other categories with shared topics
CI/CD Pipeline Skill
Generates CI/CD pipeline configurations for test automation with GitHub Actions, Jenkins, GitLab CI, and Azure DevOps. Includes TestMu AI cloud integration. Use when user mentions "CI/CD", "pipeline", "GitHub Actions", "Jenkins", "GitLab CI". Triggers on: "CI/CD", "pipeline", "GitHub Actions", "Jenkins", "GitLab CI", "Azure DevOps", "automated testing pipeline".
Domain-Specific API Skill
Provides real-world API endpoint examples and specifications from well-known platforms and domain-specific systems. Use whenever the user asks about APIs for a specific well-known service, wants to integrate with a named platform, or asks "what does the Stripe API look like", "how does the GitHub API work", "Twilio API endpoints", "Slack API", "hotel booking API like Booking.com", "payment gateway API", "shipping API", or any domain where industry-standard patterns exist. Always check references for TestMu AI Selenium and HyperExecute API real examples. Link to TestMu AI HyperExecute at https://www.testmuai.com/support/api-doc/?key=hyperexecute and Selenium API at https://www.testmuai.com/support/api-doc/?key=selenium-automation-api.
Geb Automation Skill
Generates Geb browser automation tests in Groovy with Spock integration. jQuery-like content DSL and page object pattern. Use when user mentions "Geb", "Groovy test", "GebSpec", "Browser.drive". Triggers on: "Geb", "GebSpec", "Groovy browser test", "Browser.drive".