Back to Repositories

Testing Car Documentation Generation System in OpenPilot

This test suite validates the documentation generation and maintenance for car compatibility in the OpenPilot system. It ensures accurate documentation generation, verifies consistency between generated and current documentation, and tests the car documentation diff functionality.

Test Coverage Overview

The test suite provides comprehensive coverage of OpenPilot’s car documentation system.

Key areas tested include:
  • Documentation generation for all supported car models
  • Consistency validation between generated and existing documentation
  • Car documentation diff functionality
  • File handling and cleanup operations

Implementation Analysis

The testing approach uses pytest fixtures and class-based organization to efficiently test documentation components.

Key implementation patterns include:
  • Class-level setup for car documentation retrieval
  • File comparison testing for documentation consistency
  • Temporary file management for diff testing
  • Integration with OpenPilot’s documentation templates

Technical Details

Testing infrastructure includes:
  • pytest framework for test organization
  • Custom documentation generators and comparators
  • File system operations for documentation management
  • Integration with OpenPilot’s basedir configuration
  • Template-based documentation generation system

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Separation of setup and test logic
  • Proper resource cleanup after testing
  • Clear assertion messages for debugging
  • Efficient use of class-level fixtures
  • Integration with project-wide documentation standards

commaai/openpilot

selfdrive/car/tests/test_docs.py

            
import os

from openpilot.common.basedir import BASEDIR
from opendbc.car.docs import generate_cars_md, get_all_car_docs
from openpilot.selfdrive.debug.dump_car_docs import dump_car_docs
from openpilot.selfdrive.debug.print_docs_diff import print_car_docs_diff
from openpilot.selfdrive.car.docs import CARS_MD_OUT, CARS_MD_TEMPLATE


class TestCarDocs:
  @classmethod
  def setup_class(cls):
    cls.all_cars = get_all_car_docs()

  def test_generator(self):
    generated_cars_md = generate_cars_md(self.all_cars, CARS_MD_TEMPLATE)
    with open(CARS_MD_OUT) as f:
      current_cars_md = f.read()

    assert generated_cars_md == current_cars_md, "Run selfdrive/car/docs.py to update the compatibility documentation"

  def test_docs_diff(self):
    dump_path = os.path.join(BASEDIR, "selfdrive", "car", "tests", "cars_dump")
    dump_car_docs(dump_path)
    print_car_docs_diff(dump_path)
    os.remove(dump_path)