Back to Repositories

Implementing Qt Test Runner Infrastructure in OpenPilot

This test runner implementation serves as the main entry point for Qt-based unit tests in the OpenPilot project. It sets up the QApplication environment and handles language translations for test execution.

Test Coverage Overview

The test runner provides essential infrastructure for executing Qt-based unit tests in OpenPilot. It covers:

  • QApplication initialization for Qt test environment
  • Language translation loading and configuration
  • Test session execution via Catch2 framework
  • Exit code handling and normalization

Implementation Analysis

The testing approach utilizes Catch2 as the primary testing framework integrated with Qt infrastructure. The implementation follows a modular pattern, separating concerns between application setup, translation management, and test execution.

Key technical aspects include QTranslator configuration, path resolution for translation files, and standardized exit code handling.

Technical Details

Testing tools and configuration:

  • Catch2 testing framework with CATCH_CONFIG_RUNNER
  • Qt framework components (QApplication, QTranslator)
  • Translation file path configuration
  • Debug output for translation loading status
  • Exit code normalization (0xFF maximum)

Best Practices Demonstrated

The test runner exemplifies several testing best practices including proper resource initialization, error handling, and clean shutdown procedures.

  • Structured application setup and teardown
  • Clear error reporting for translation loading
  • Proper path handling for resource files
  • Standardized exit code management

commaai/openpilot

selfdrive/ui/tests/test_runner.cc

            
#define CATCH_CONFIG_RUNNER
#include "catch2/catch.hpp"

#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QTranslator>

int main(int argc, char **argv) {
  // unit tests for Qt
  QApplication app(argc, argv);

  QString language_file = "main_test_en";
  // FIXME: pytest-cpp considers this print as a test case
  qDebug() << "Loading language:" << language_file;

  QTranslator translator;
  QString translationsPath = QDir::cleanPath(qApp->applicationDirPath() + "/../translations");
  if (!translator.load(language_file, translationsPath)) {
    qDebug() << "Failed to load translation file!";
  }
  app.installTranslator(&translator);

  const int res = Catch::Session().run(argc, argv);
  return (res < 0xff ? res : 0xff);
}