Remarque
- Les exemples de cette bibliothèque sont destinés à vous inspirer. Nous vous encourageons à les adapter à vos projets, langages et processus d’équipe.
- Pour des exemples d’instructions personnalisées pour des langages et scénarios spécifiques fournis par la communauté, consultez le référentiel Personnalisations remarquables de GitHub Copilot.
- Vous pouvez appliquer des instructions personnalisées à différents niveaux, en fonction de la plateforme ou de l’IDE sur lequel vous les créez. Pour plus d’informations, consultez « À propos de la personnalisation des réponses de GitHub Copilot Chat ».
Cet exemple montre un fichier python-tests.instructions.md
spécifique au chemin d’accès qui s’applique uniquement aux fichiers de test Python de votre référentiel, à l’aide du champ applyTo
. Pour plus d’informations sur les fichiers d’instructions spécifiques au chemin d’accès, consultez Ajout d’instructions personnalisées du référentiel pour GitHub Copilot.
--- applyTo: "tests/**/*.py" --- When writing Python tests: ## Test Structure Essentials - Use pytest as the primary testing framework - Follow AAA pattern: Arrange, Act, Assert - Write descriptive test names that explain the behavior being tested - Keep tests focused on one specific behavior ## Key Testing Practices - Use pytest fixtures for setup and teardown - Mock external dependencies (databases, APIs, file operations) - Use parameterized tests for testing multiple similar scenarios - Test edge cases and error conditions, not just happy paths ## Example Test Pattern ```python import pytest from unittest.mock import Mock, patch class TestUserService: @pytest.fixture def user_service(self): return UserService() @pytest.mark.parametrize("invalid_email", ["", "invalid", "@test.com"]) def test_should_reject_invalid_emails(self, user_service, invalid_email): with pytest.raises(ValueError, match="Invalid email"): user_service.create_user({"email": invalid_email}) @patch('src.user_service.email_validator') def test_should_handle_validation_failure(self, mock_validator, user_service): mock_validator.validate.side_effect = ConnectionError() with pytest.raises(ConnectionError): user_service.create_user({"email": "test@example.com"}) ```
---
applyTo: "tests/**/*.py"
---
When writing Python tests:
## Test Structure Essentials
- Use pytest as the primary testing framework
- Follow AAA pattern: Arrange, Act, Assert
- Write descriptive test names that explain the behavior being tested
- Keep tests focused on one specific behavior
## Key Testing Practices
- Use pytest fixtures for setup and teardown
- Mock external dependencies (databases, APIs, file operations)
- Use parameterized tests for testing multiple similar scenarios
- Test edge cases and error conditions, not just happy paths
## Example Test Pattern
```python
import pytest
from unittest.mock import Mock, patch
class TestUserService:
@pytest.fixture
def user_service(self):
return UserService()
@pytest.mark.parametrize("invalid_email", ["", "invalid", "@test.com"])
def test_should_reject_invalid_emails(self, user_service, invalid_email):
with pytest.raises(ValueError, match="Invalid email"):
user_service.create_user({"email": invalid_email})
@patch('src.user_service.email_validator')
def test_should_handle_validation_failure(self, mock_validator, user_service):
mock_validator.validate.side_effect = ConnectionError()
with pytest.raises(ConnectionError):
user_service.create_user({"email": "test@example.com"})
```