Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 17, 2025

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the Velodyne TD3 reinforcement learning project. It introduces Poetry as the package manager and configures pytest with coverage reporting, making it easy for developers to write and run tests immediately.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with Poetry as the package manager
  • Dependencies: Added production dependencies (numpy, torch, tensorboard, squaternion)
  • Development Dependencies: Added testing tools (pytest, pytest-cov, pytest-mock)

Testing Configuration

  • pytest Settings: Configured in pyproject.toml with:
    • Strict markers and configuration
    • Coverage reporting (HTML, XML, terminal)
    • Custom markers for test organization (unit, integration, slow)
    • Test discovery patterns
  • Coverage Settings: Configured source paths and exclusion patterns

Directory Structure

/workspace/
├── pyproject.toml          # Poetry and testing configuration
├── .gitignore             # Updated with testing entries
├── tests/
│   ├── __init__.py
│   ├── conftest.py        # Shared pytest fixtures
│   ├── test_setup_validation.py  # Infrastructure validation tests
│   ├── unit/
│   │   └── __init__.py
│   └── integration/
│       └── __init__.py

Pytest Fixtures (in conftest.py)

  • temp_dir: Temporary directory for test files
  • mock_config: Mock configuration dictionary
  • sample_state, sample_action, sample_batch: Test data fixtures
  • mock_ros_node, mock_gazebo_services, mock_publishers, mock_subscribers: ROS-specific mocks
  • device: PyTorch device fixture
  • reset_random_seeds: Automatic seed reset for reproducibility

Other Improvements

  • .gitignore: Comprehensive exclusions for Python, testing, ROS, and PyTorch artifacts
  • Validation Tests: Created tests to verify the infrastructure works correctly

How to Use

Install Dependencies

poetry install

Run Tests

# Run all tests with coverage
poetry run pytest

# Run tests without coverage
poetry run pytest --no-cov

# Run specific test file
poetry run pytest tests/test_setup_validation.py

# Run tests with specific markers
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m "not slow"

# Alternative commands (both work)
poetry run test
poetry run tests

Writing Tests

  1. Create test files in tests/unit/ or tests/integration/
  2. Use the provided fixtures from conftest.py
  3. Mark tests appropriately (@pytest.mark.unit, @pytest.mark.integration, etc.)

Example Test

import pytest
from TD3.replay_buffer import ReplayBuffer

class TestReplayBuffer:
    @pytest.mark.unit
    def test_buffer_initialization(self):
        buffer = ReplayBuffer(state_dim=24, action_dim=2, max_size=1000)
        assert buffer.size == 0
        assert buffer.max_size == 1000
    
    @pytest.mark.unit
    def test_add_experience(self, sample_state, sample_action):
        buffer = ReplayBuffer(state_dim=24, action_dim=2)
        buffer.add(sample_state, sample_action, 1.0, sample_state, False)
        assert buffer.size == 1

Notes

  • ROS dependencies are mocked in fixtures to allow testing without a full ROS installation
  • Coverage threshold is currently disabled for initial setup but can be enabled by adding --cov-fail-under=80 back to pytest options
  • The infrastructure supports both unit and integration testing patterns
  • All standard pytest features and plugins are available

Next Steps

Developers can now immediately start writing tests for the existing codebase. The infrastructure is fully configured and ready to use.

- Add Poetry package manager configuration with pyproject.toml
- Configure pytest with coverage reporting and custom markers
- Create testing directory structure (tests/unit, tests/integration)
- Add comprehensive pytest fixtures in conftest.py for common testing needs
- Include ROS-specific mock fixtures for testing robot code
- Set up .gitignore with testing and Python-specific exclusions
- Add validation tests to verify infrastructure works correctly
- Configure Poetry scripts for running tests (poetry run test/tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant