Skip to content

feat: set up comprehensive Python testing infrastructure with Poetry #84

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,9 @@ dmypy.json

# Pyre type checker
.pyre/

# Claude settings
.claude/*

# Poetry lock file is NOT ignored (keep it in version control)
# poetry.lock
3,614 changes: 3,614 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
[tool.poetry]
name = "p3"
version = "0.1.0"
description = "Programming Puzzles and Code Competitions"
authors = ["P3 Contributors"]
readme = "README.md"
packages = [{include = "generators"}, {include = "solvers"}]

[tool.poetry.dependencies]
python = "^3.8"
# Core dependencies from ICLR2023/src/requirements.txt
tqdm = "*"
# orderedset = "*" # Has build issues, using built-in OrderedDict instead
numpy = "*"
astor = "*"
scikit-learn = "*"
fire = "*"
strictfire = "*"
pebble = "*"
# Other common dependencies found in the project
datasets = "*"
transformers = "^4.30.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
pytest-cov = "^4.1.0"
pytest-mock = "^3.11.1"

[tool.poetry.scripts]
test = "pytest:main"
tests = "pytest:main"

[tool.pytest.ini_options]
minversion = "7.0"
testpaths = ["tests"]
python_files = ["test_*.py", "*_test.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = [
"-ra",
"--strict-markers",
"--strict-config",
"--cov=generators",
"--cov=solvers",
"--cov-branch",
"--cov-report=term-missing:skip-covered",
"--cov-report=html:htmlcov",
"--cov-report=xml:coverage.xml",
# "--cov-fail-under=80", # Disabled for infrastructure testing
]
markers = [
"unit: Unit tests",
"integration: Integration tests",
"slow: Slow tests that should be run less frequently",
]

[tool.coverage.run]
source = ["generators", "solvers"]
omit = [
"*/tests/*",
"*/test_*.py",
"*/__pycache__/*",
"*/site-packages/*",
]

[tool.coverage.report]
precision = 2
show_missing = true
skip_covered = false
# fail_under = 80 # Disabled for infrastructure testing
exclude_lines = [
"pragma: no cover",
"def __repr__",
"raise AssertionError",
"raise NotImplementedError",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
"if typing.TYPE_CHECKING:",
]

[tool.coverage.html]
directory = "htmlcov"

[tool.coverage.xml]
output = "coverage.xml"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Test package initialization
151 changes: 151 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
"""Shared pytest fixtures and configuration for all tests."""
import os
import sys
import tempfile
from pathlib import Path
from typing import Generator, Dict, Any

import pytest

# Add project root to Python path
PROJECT_ROOT = Path(__file__).parent.parent
sys.path.insert(0, str(PROJECT_ROOT))


@pytest.fixture
def temp_dir() -> Generator[Path, None, None]:
"""Create a temporary directory for test files."""
with tempfile.TemporaryDirectory() as tmpdir:
yield Path(tmpdir)


@pytest.fixture
def temp_file(temp_dir: Path) -> Generator[Path, None, None]:
"""Create a temporary file for testing."""
temp_path = temp_dir / "test_file.txt"
temp_path.write_text("test content")
yield temp_path


@pytest.fixture
def mock_config() -> Dict[str, Any]:
"""Provide a mock configuration dictionary."""
return {
"debug": True,
"max_iterations": 100,
"timeout": 30,
"output_dir": "/tmp/test_output",
"model_name": "test_model",
}


@pytest.fixture
def sample_puzzle_data() -> Dict[str, Any]:
"""Provide sample puzzle data for testing."""
return {
"name": "test_puzzle",
"description": "A test puzzle for unit testing",
"input": [1, 2, 3, 4, 5],
"expected_output": 15,
"difficulty": "easy",
"tags": ["math", "sum"],
}


@pytest.fixture
def sample_code_snippet() -> str:
"""Provide a sample code snippet for testing."""
return """
def solve(input_data):
'''Solve the puzzle by summing all numbers.'''
return sum(input_data)
"""


@pytest.fixture
def mock_environment_variables(monkeypatch):
"""Mock environment variables for testing."""
test_env = {
"TEST_MODE": "true",
"LOG_LEVEL": "DEBUG",
"OUTPUT_FORMAT": "json",
}
for key, value in test_env.items():
monkeypatch.setenv(key, value)
return test_env


@pytest.fixture(scope="session")
def project_root() -> Path:
"""Return the project root directory."""
return PROJECT_ROOT


@pytest.fixture
def generators_path(project_root: Path) -> Path:
"""Return the path to the generators module."""
return project_root / "generators"


@pytest.fixture
def solvers_path(project_root: Path) -> Path:
"""Return the path to the solvers module."""
return project_root / "solvers"


@pytest.fixture
def clean_imports():
"""Clean up imports to ensure test isolation."""
modules_to_remove = []
for module in sys.modules:
if module.startswith(("generators", "solvers")):
modules_to_remove.append(module)

yield

for module in modules_to_remove:
if module in sys.modules:
del sys.modules[module]


@pytest.fixture(autouse=True)
def reset_random_seed():
"""Reset random seed for reproducible tests."""
import random
import numpy as np

random.seed(42)
np.random.seed(42)

yield

# Reset after test
random.seed()
np.random.seed()


def pytest_configure(config):
"""Configure pytest with custom settings."""
config.addinivalue_line(
"markers", "unit: Unit tests"
)
config.addinivalue_line(
"markers", "integration: Integration tests"
)
config.addinivalue_line(
"markers", "slow: Slow tests that should be run less frequently"
)


def pytest_collection_modifyitems(config, items):
"""Modify test collection to add markers based on test location."""
for item in items:
# Add markers based on test file location
if "unit" in str(item.fspath):
item.add_marker(pytest.mark.unit)
elif "integration" in str(item.fspath):
item.add_marker(pytest.mark.integration)

# Add slow marker to tests with "slow" in their name
if "slow" in item.nodeid.lower():
item.add_marker(pytest.mark.slow)
1 change: 1 addition & 0 deletions tests/integration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Integration test package initialization
Loading