tinycodinglm / code /code202.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame
1.95 kB
deepseek-harness trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How do I write good tests with pytest?

Assistant

Basic Structure:

python
import pytest

def test_addition():
    assert 1 + 1 == 2

class TestCalculator:
    def test_divide(self):
        assert 10 / 2 == 5

Fixtures:

python
@pytest.fixture
def sample_data():
    return {"users": [{"id": 1, "name": "Alice"}]}

def test_users(sample_data):
    assert len(sample_data["users"]) == 1

Parametrization:

python
@pytest.mark.parametrize("a,b,expected", [
    (1, 2, 3),
    (0, 0, 0),
    (-1, 1, 0),
])
def test_add(a, b, expected):
    assert a + b == expected

Mocking:

python
from unittest.mock import Mock, patch

@patch("my_module.external_api")
def test_with_mock(mock_api):
    mock_api.return_value = {"status": "ok"}
    result = my_module.call_api()
    assert result["status"] == "ok"

Async Testing:

python
@pytest.mark.asyncio
async def test_async():
    result = await async_function()
    assert result == "expected"

Coverage:

bash
pytest --cov=my_package --cov-report=html

Tips:

  • One assertion per test
  • Descriptive test names
  • Use fixtures for setup/teardown
  • Mock external dependencies
  • Run tests in CI/CD