Revolutionizing QA: AI-Powered Unit Test Generation with DeepSeek-R1 and pytest Framework | Step-by-Step Guide

In modern software development, comprehensive testing remains a critical challenge. This tutorial demonstrates how to combine DeepSeek's intelligence with pytest's testing framework to generate robust unit tests automatically.

Automated Unit Test Generation with DeepSeek-R1 and pytest

Environment Configuration

Set up your testing environment with these essential packages:

# Install core dependencies
!pip install pytest deepseek-ai transformers pytest-cov mock

# Verify installation
import pytest
print(f"pytest version: {pytest.__version__}")
Pro Tip: Create a dedicated virtual environment for testing: python -m venv testenv && source testenv/bin/activate

Basic Test Generation Workflow

Create a test generator module:

# test_generator.py
from transformers import AutoTokenizer, AutoModelForCausalLM

class TestGenerator:
    def __init__(self):
        self.tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/deepseek-r1")
        self.model = AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-r1")
    
    def generate_test(self, function_code: str) -> str:
        prompt = f"""Generate pytest unit tests for this Python function:
{function_code}

Follow these requirements:
1. Use parameterized testing
2. Include edge cases
3. Add type annotations
4. Include descriptive docstrings"""

        inputs = self.tokenizer(prompt, return_tensors="pt")
        outputs = self.model.generate(**inputs, max_length=1024)
        return self.tokenizer.decode(outputs[0], skip_special_tokens=True)

Testing Sample Application Code

Create a simple calculator module to test:

# calculator.py
def add(a: float, b: float) -> float:
    return a + b

def subtract(a: float, b: float) -> float:
    return a - b

Generating and Executing Tests

Automate the test lifecycle:

# Generate tests
generator = TestGenerator()
with open("calculator.py") as f:
    tests = generator.generate_test(f.read())

# Save generated tests
with open("test_calculator.py", "w") as f:
    f.write(tests)

# Run tests with pytest
!pytest test_calculator.py -v --cov=calculator

Advanced Test Patterns

Implement mock testing with AI-generated scenarios:

# test_database.py
from unittest.mock import Mock
import pytest

def test_db_connection():
    # AI-generated test scenario
    mock_db = Mock()
    mock_db.connect.return_value = True
    assert mock_db.connect("localhost:5432") is True
    mock_db.connect.assert_called_once_with("localhost:5432")

CI/CD Integration

Add automated testing to GitHub Actions:

# .github/workflows/tests.yml
name: AI-Powered Tests

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.10'
    - name: Install dependencies
      run: |
        pip install -r requirements.txt
    - name: Generate tests
      run: python generate_tests.py
    - name: Run tests
      run: pytest --cov=src/ --cov-report=xml

Best Practices for AI-Generated Tests

  • Validate generated tests with human review
  • Maintain 80%+ code coverage threshold
  • Update test patterns with code changes
  • Combine AI tests with manual regression suites
Pattern Recognition: DeepSeek-R1 excels at identifying common test patterns:
  • Boundary value analysis
  • Equivalence partitioning
  • State transition testing
  • Error condition simulation

Handling Complex Dependencies

Test a Flask API endpoint with generated tests:

# test_api.py
import pytest
from myapp import create_app

@pytest.fixture
def client():
    app = create_app()
    with app.test_client() as client:
        yield client

def test_homepage(client):
    """Test homepage response (AI-generated)"""
    response = client.get('/')
    assert response.status_code == 200
    assert b"Welcome" in response.data

By integrating DeepSeek-R1 with pytest, developers can achieve comprehensive test coverage while reducing manual effort. Remember that AI-generated tests should complement - not replace - human testing expertise. Regular review and refinement of generated tests ensures they remain effective as your codebase evolves.


Category: deepseek

Latest Articles