ca-django-tests
npx skills add https://github.com/ramonsaraiva/yadm --skill ca-django-tests
Agent 安装分布
Skill 文档
CA Django Testing Skill
This skill helps write and run Django unit tests in Consumer Affairs repositories with a focus on achieving 100% code coverage.
When to Use This Skill
AUTOMATIC ACTIVATION TRIGGERS – Activate this skill immediately when:
- â User mentions ANY coverage-related terms: “coverage”, “codecov”, “test coverage”, “partial coverage”, “branch coverage”, “uncovered lines”, “missing coverage”, “BrPart”, “line X is not covered”, “partially covered”
- â User asks to write tests for CA repository code
- â User wants to add test coverage for existing functions
- â User needs to run tests in CA repos
- â User mentions fixing test failures or improving coverage
- â User references specific test files or test paths in CA repos
- â User says “use the test skill” or similar phrases
PROACTIVE USE – Also use this skill proactively (without being asked) when:
- After implementing new features in CA repos that need tests
- After fixing bugs in CA repos that need test verification
- When analyzing code that lacks proper test coverage
Writing Tests
Test File Location Strategy
CRITICAL: Always prioritize using existing test modules unless creating a new code module:
- Check for existing test files first before creating new ones
- Add tests to existing test modules that are related to the code being tested
- Only create new test files when:
- Creating a completely new code module
- The existing test file would become too large (>1000 lines)
- The new functionality is logically separate from existing tests
Test Structure Requirements
All tests MUST follow these principles:
1. Test base class
Some repositories might have their own baseclass extending Django’s TestCase class.
- Look for other test modules and make sure you’re using the same base class as they
- If you don’t find test modules examples, try finding if we’re extending Django’s base
TestClasswith our own
2. Unit Test Each Function Separately
- Each function/method should have its own test class
- Test one function at a time in isolation
- Use mocking for dependencies and external calls
3. Cover Every Code Path
THIS IS CRITICAL: You must write tests for:
- â Happy path (expected inputs)
- â Edge cases (empty inputs, None values, boundary conditions)
- â Error conditions (exceptions, invalid inputs)
- â Each branch in if/else statements
- â Each iteration scenario in loops
- â Each return statement path
- â Early returns and guard clauses
Goal: 100% code coverage – codecov must pass in CI
4. Test Naming Convention
def test_<function_name>_<scenario>_<expected_outcome>(self):
"""Clear description of what is being tested"""
Examples:
test_calculate_total_with_valid_items_returns_sum()test_calculate_total_with_empty_list_returns_zero()test_calculate_total_with_negative_values_raises_error()
Testing Best Practices
Use Mocking Appropriately
from unittest.mock import patch, Mock, MagicMock
class MyServiceTestCase(TestCase):
@patch('app.module.external_api_call')
def test_service_with_mocked_api(self, mock_api):
mock_api.return_value = {'status': 'success'}
# Test your code
Use Fixtures and Setup
class MyTestCase(TestCase):
def setUp(self):
"""Set up test fixtures that are reused across tests"""
self.user = User.objects.create(username='testuser')
def tearDown(self):
"""Clean up after tests if needed"""
pass
Test Database Interactions
from django.test import TestCase
class ModelTestCase(TestCase):
def test_model_creation(self):
instance = MyModel.objects.create(field='value')
self.assertEqual(instance.field, 'value')
self.assertIsNotNone(instance.pk)
Running Tests
Priority Order for Running Tests
Use the ca helper script with the following priority:
1. First Try: ca magictest (PREFERRED)
ca magictest path.to.the.tests.folder.or.module
Examples:
ca magictest app.api.tests.test_viewsca magictest app.api.testsca magictest .(run all tests)
This is the preferred method – it uses containers that are already running.
2. If magictest fails: ca pytest
ca pytest path/to/the/tests/folder/or/module
Use this if the repo has pytest support:
ca pytest app/api/tests/test_views.pyca pytest app/api/tests/
3. If pytest not available: ca test
ca test path.to.the.tests.folder.or.module
Fallback to Django’s test runner:
ca test app.api.tests.test_views
IMPORTANT: Container Usage
- DO NOT use
docker compose run– this starts a separate container - DO USE the
cascript – it uses already running containers - The containers should already be running before executing tests
Running Specific Tests
To run a single test method:
ca magictest app.api.tests.test_views.MyTestCase.test_specific_method
Workflow Example
When user asks to “write tests for the authenticate function”:
-
Locate the function to understand its code paths
-
Find existing test file for that module (e.g., if function is in
app/api/auth.py, look forapp/api/tests/test_auth.py) -
Analyze all code paths in the function:
- Note all branches (if/else, try/except)
- Note all return statements
- Note edge cases
-
Write comprehensive tests covering each path:
- Create test class for the function
- Write individual test methods for each code path
- Use mocking for external dependencies
-
Run tests using
ca magictest:ca magictest app.api.tests.test_auth -
Verify coverage – ensure all lines are covered
-
Fix any failures and re-run
Coverage Requirements
- Target: 100% code coverage
- Every line of code must be executed by at least one test
- Every branch must be tested
- Codecov CI check must pass
- Use coverage reports to identify untested code paths
Common Patterns in CA Repos
Testing API Views
from django.test import TestCase, RequestFactory
from unittest.mock import patch
class ViewTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
def test_view_returns_200(self):
request = self.factory.get('/api/endpoint/')
response = my_view(request)
self.assertEqual(response.status_code, 200)
Testing with Authentication
from django.contrib.auth.models import User
class AuthenticatedTestCase(TestCase):
def setUp(self):
self.user = User.objects.create_user(
username='testuser',
password='testpass'
)
self.client.login(username='testuser', password='testpass')
Testing Async Code
from django.test import TestCase
import asyncio
class AsyncTestCase(TestCase):
def test_async_function(self):
result = asyncio.run(my_async_function())
self.assertEqual(result, expected_value)
Remember
â
Check for existing test files first
â
Cover every single code path
â
Use ca magictest for running tests
â
Aim for 100% coverage
â
Mock external dependencies
â
Test error conditions, not just happy paths
â
Use descriptive test names
â
Keep tests focused and isolated