Skip to main content

テストの自動化

単体テストを記述するためのファイル固有の手順。

メモ

  • このライブラリの例はインスピレーションを得るためのものです。プロジェクト、言語、チーム プロセスに合わせて具体的に調整することをお勧めします。
  • 特定の言語とシナリオ向けのカスタム指示のコミュニティに投稿された例については、Awesome GitHub Copilot Customizations リポジトリを参照してください。
  • カスタム指示は、作成するプラットフォームまたは IDE に応じて、さまざまなスコープにわたって適用できます。 詳しくは、「GitHub Copilot Chat の回答のカスタマイズについて」を参照してください。

この例では、applyTo フィールドを使用してリポジトリ内の Python テスト ファイルにのみ適用される、パス固有の python-tests.instructions.md ファイルを示します。 パス固有の手順ファイルの詳細については、「GitHub Copilot のリポジトリ カスタム命令を追加する」を参照してください。

Text
---
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": "[email protected]"})
```