# thomhurst/tunit architecture

> Architecture diagram of the GitHub repository thomhurst/tunit, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thomhurst/tunit
- Repository: https://github.com/thomhurst/tunit

## Overview

##### Main Components
- Core Runtime and Test Infrastructure – Represented by projects such as TUnit.Core and TUnit.Engine. These contain the logic for test discovery, registration, execution, orchestration of hooks (before/after, assembly, class, test), and management of test contexts/events.
- Assertion Library – Shown as TUnit.Assertions (and its variants) which provides an asynchronous, fluent API for making assertions throughout tests.
- Analyzers and Code Fix Providers – The file tree shows a range of analyzer projects (TUnit.Analyzers.*, TUnit.Assertions.Analyzers.CodeFixers, etc.) that enforce best practices and help the developer catch errors during compile time.
- Source Generation Infrastructure – A key architectural piece that “finds” tests at compile time without using reflection, registering them to be executed by the engine. This component is implicit in the design and helps power the “source generated tests” model.
- Templates and Scaffolding – TUnit.Templates along with the Example and Playground projects, which help users to install and bootstrap their test projects.
- Integration with External Test Platforms – Components like TUnit.Playwright indicate that there is support for UI/Browser testing; and the documentation points to integrations with Visual Studio, Rider, VS Code, and the CLI.
- Build and Pipeline Support – TUnit.Pipeline and the existence of GitHub CI/CD files, actions, and release configurations indicate a decoupled pipeline that builds, packages, performs analysis and runs tests.
- Additional Supporting Tools – There are several “tools” directories (e.g. for speed comparisons, nuget tester, and benchmark projects) that enrich the development and performance testing of the framework.

##### Out Relationships and Data Flow
- The Core Testing Engine (TUnit.Core and TUnit.Engine) is the heart and orchestrates test discovery via source generators. It interacts with the Assertion Library to validate test results.
- The analyzers (from TUnit.Analyzers and TUnit.Assertions.Analyzers) act as “companion” tools – they hook into the build process to provide diagnostics. They analyze code structure before runtime.
- The Templates provide a starting point for consumers, and depend on the Core, Assertions, and Engine packages.
- External test runners (via IDE adapters, CLI commands, and the Microsoft.Testing.Platform integration) pass commands to the Engine, which then uses events and hooks to manage test lifecycles.
- The Pipeline modules are responsible for continuous integration tasks (packaging, version generation, running tests, etc.) and interface with the code repositories and deployment systems.

##### Architectural Patterns and Principles
- Layered Architecture – Each set of assemblies (Core, Engine, Assertions, Analyzers) are separated by responsibilities.
- Dependency Injection and Hook/Event Patterns – Test and lifecycle hooks clearly indicate use of dependency injection patterns, property injection, and event subscription to manage resource lifecycles.
- Extensibility – The use of source generators and customizable data sources (attributes like [MatrixDataSource], [ClassDataSource<T>], etc.) means the system is built to be extended by users.
- Build/Pipeline Separation – The build scripts and CI/CD pipeline modules are separated as independent modules under TUnit.Pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Build and Pipeline Support"
        Pipeline["TUnit.Pipeline"]:::pipeline
        GithubWorkflows["GitHub Workflows"]:::pipeline
        GithubActions["GitHub Actions"]:::pipeline
    end

    subgraph "Core Test Engine Layer"
        TUnitCore["TUnit.Core"]:::core
        SourceGen["Source Generation"]:::core
        TUnitEngine["TUnit.Engine"]:::core
    end

    subgraph "Assertion Library"
        TUnitAssertions["TUnit.Assertions"]:::assertions
    end

    subgraph "Analyzers & Code Fixers"
        TUnitAnalyzers["TUnit.Analyzers"]:::analyzers
        TUnitAnalyzersCodeFixers["TUnit.Analyzers.CodeFixers"]:::analyzers
        TUnitAssertionsAnalyzers["TUnit.Assertions.Analyzers"]:::analyzers
    end

    subgraph "Templates & Scaffolding"
        TUnitTemplates["TUnit.Templates"]:::templates
        TUnitExampleAsp["TUnit.Example.Asp.Net"]:::templates
    end

    subgraph "Integration"
        TUnitPlaywright["TUnit.Playwright"]:::integration
        IDEAdapters["IDE/CLI Adapters"]:::integration
    end

    subgraph "Supporting Tools"
        Tools["Tools"]:::tools
    end

    Pipeline -->|"triggers"| TUnitEngine
    TUnitCore -.->|"source_gen"| SourceGen
    SourceGen -.->|"registers_tests"| TUnitEngine
    TUnitEngine -->|"executes_assertions"| TUnitAssertions
    TUnitAnalyzers -.->|"analyzes"| TUnitCore
    TUnitAnalyzersCodeFixers -.->|"fixes"| TUnitCore
    TUnitAssertionsAnalyzers -.->|"analyzes"| TUnitCore
    TUnitTemplates -->|"depends_on"| TUnitCore
    TUnitTemplates -->|"depends_on"| TUnitEngine
    TUnitTemplates -->|"depends_on"| TUnitAssertions
    TUnitExampleAsp -->|"scaffolds"| TUnitCore
    TUnitExampleAsp -->|"scaffolds"| TUnitEngine
    TUnitExampleAsp -->|"scaffolds"| TUnitAssertions
    IDEAdapters -->|"sends_commands"| TUnitEngine
    TUnitPlaywright -->|"executes_UI_tests"| TUnitEngine
    Tools -->|"supports"| Pipeline

    click TUnitCore "https://github.com/thomhurst/tunit/blob/main/TUnit.Core"
    click TUnitEngine "https://github.com/thomhurst/tunit/blob/main/TUnit.Engine"
    click SourceGen "https://github.com/thomhurst/tunit/tree/main/TUnit.Core/Interfaces/SourceGenerator"
    click TUnitAssertions "https://github.com/thomhurst/tunit/blob/main/TUnit.Assertions"
    click TUnitAnalyzers "https://github.com/thomhurst/tunit/blob/main/TUnit.Analyzers"
    click TUnitAnalyzersCodeFixers "https://github.com/thomhurst/tunit/blob/main/TUnit.Analyzers.CodeFixers"
    click TUnitAssertionsAnalyzers "https://github.com/thomhurst/tunit/blob/main/TUnit.Assertions.Analyzers"
    click TUnitTemplates "https://github.com/thomhurst/tunit/blob/main/TUnit.Templates"
    click TUnitExampleAsp "https://github.com/thomhurst/tunit/blob/main/TUnit.Example.Asp.Net"
    click TUnitPlaywright "https://github.com/thomhurst/tunit/blob/main/TUnit.Playwright"
    click Pipeline "https://github.com/thomhurst/tunit/blob/main/TUnit.Pipeline"
    click GithubWorkflows "https://github.com/thomhurst/tunit/tree/main/.github/workflows"
    click GithubActions "https://github.com/thomhurst/tunit/tree/main/.github/actions"
    click Tools "https://github.com/thomhurst/tunit/tree/main/tools"

    classDef core fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef assertions fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef analyzers fill:#FFD700,stroke:#000,stroke-width:2px;
    classDef templates fill:#98FB98,stroke:#000,stroke-width:2px;
    classDef integration fill:#D3D3D3,stroke:#000,stroke-width:2px;
    classDef pipeline fill:#DA70D6,stroke:#000,stroke-width:2px;
    classDef tools fill:#F0E68C,stroke:#000,stroke-width:2px;
```
