Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Raspberry Build HAT: Unit Testing Strategies with .NET

Raspberry Build HAT: Unit Testing Strategies with .NET
July 30, 2025 6 min read BEN ABT
.NET Raspberry Buildhat Lego

If you go beyond hobby tinkering and aim for reliable, maintainable software, you need tests. Hardware projects are no exception. In fact, the closer you get to IO, timing and device state, the more you benefit from having a fast feedback loop and repeatable verification.

This post builds on the previous article where we controlled LEGO motors from .NET using the Raspberry Pi Build HAT: Raspberry Build HAT: Controlling LEGO Engines with .NET . Here, we focus on testability and show a pragmatic way to introduce unit tests without needing the physical hardware for every run.

Why not just unit test everything? Direct calls into Build HAT are by nature integration tests: they cross process and machine boundaries (serial/I2C), depend on timing and require the hardware to be connected. In addition:

  • The Build HAT stack is hardware-dependent; without a Pi and the HAT attached, calls will fail by design.
  • APIs and firmware can vary, leading to occasional flakiness when testing “for real”.
  • Behavior differs between Windows (dev box) and Linux (the Pi), so portability issues can creep in.

The solution is to define a small abstraction for the device and test your application logic against it. Use a real implementation for production and a virtual or mocked implementation for unit tests. Keep end-to-end tests (that hit the actual HAT) as integration tests executed on a Raspberry Pi.

Implementation

Concrete device class for physical access:

C#
 1public class BrickHatEngineDevice : IEngineDevice
 2{
 3    private readonly Lock _lock = new();
 4    private readonly EngineDeviceOptions _options;
 5    private Brick? _brick;
 6
 7    public string SerialPort => _options.SerialPort;
 8
 9    public BrickHatEngineDevice(IOptions<EngineDeviceOptions> optionsAccessor)
10    {
11        _options = optionsAccessor.Value;
12        _brick = new Brick(_options.SerialPort);
13    }
14
15    public void Dispose()
16    {
17        _brick?.Dispose();
18        _brick = null;
19    }
20
21    public void SendRawCommand(string command)
22    {
23        lock (_lock)
24        {
25            _brick?.SendRawCommand(command);
26        }
27    }
28}

About locking: the lock ensures only one thread interacts with the underlying Brick at a time. This prevents accidental concurrent access (for example while initializing or disposing) and avoids interleaving commands on the serial connection.

Virtual device class to run without hardware, enabling fast and deterministic unit tests:

C#
 1public class VirtualEngineDevice : IEngineDevice
 2{
 3    private readonly Lock _lock = new();
 4    private bool _disposed;
 5    private readonly EngineDeviceOptions _options;
 6    private const int MaxCommands = 500;
 7
 8    public VirtualEngineDevice(IOptions<EngineDeviceOptions> options)
 9    {
10        _options = options.Value;
11    }
12
13    public IReadOnlyList<string> Commands
14    {
15        get
16        {
17            lock (_lock)
18            {
19                return _commands.ToList();
20            }
21        }
22    }
23
24    public string SerialPort => _options.SerialPort;
25
26    private readonly Queue<string> _commands = new();
27
28    public void SendRawCommand(string command)
29    {
30        if (_disposed)
31        {
32            throw new ObjectDisposedException(nameof(VirtualEngineDevice));
33        }
34
35        if (string.IsNullOrWhiteSpace(command))
36        {
37            return; // ignore invalid / empty commands in virtual implementation
38        }
39
40        lock (_lock)
41        {
42            _commands.Enqueue(command);
43            if (_commands.Count > MaxCommands)
44            {
45                _commands.Dequeue(); // drop oldest
46            }
47        }
48    }
49
50    public void Dispose()
51    {
52        lock (_lock)
53        {
54            _disposed = true;
55            _commands.Clear();
56        }
57        GC.SuppressFinalize(this);
58    }
59}

Shared device interface to keep your higher-level code independent from hardware specifics:

C#
1public interface IEngineDevice : IDisposable
2{
3    string SerialPort { get; }
4    void SendRawCommand(string command);
5}

Options class to hold configuration (injected via .NET Options pattern):

C#
1public class EngineDeviceOptions
2{
3    [Required]
4    public string SerialPort { get; set; } = null!;
5}

A small factory that tries to create the physical device; if that fails, it falls back to the virtual one. This gives you a graceful runtime behavior (with logs) while keeping tests simple:

C#
 1public interface IEngineDeviceFactory
 2{
 3    IEngineDevice Create();
 4}
 5
 6public class EngineDeviceFactory : IEngineDeviceFactory
 7{
 8    private readonly IServiceProvider _serviceProvider;
 9    private readonly ILogger<EngineDeviceFactory> _log;
10
11    public EngineDeviceFactory(IServiceProvider serviceProvider, ILogger<EngineDeviceFactory> log)
12    {
13        _serviceProvider = serviceProvider;
14        _log = log;
15    }
16
17    public IEngineDevice Create()
18    {
19        IOptions<EngineDeviceOptions> options = _serviceProvider.GetRequiredService<IOptions<EngineDeviceOptions>>();
20
21        IEngineDevice device;
22        try
23        {
24            device = new BrickHatEngineDevice(options);
25            _log.LogInformation("Physical engine device initialized in port {SerialPort}", device.SerialPort);
26        }
27        catch(Exception ex)
28        {
29            _log.LogError(ex, "Failed to initialize physical engine device in port {SerialPort}", options.Value.SerialPort);
30            device = new VirtualEngineDevice(options);
31            _log.LogWarning("Virtual engine device initialized in port {SerialPort}", device.SerialPort);
32        }
33
34        return device;
35    }
36}

Unit testing with a virtual device or a mock

With the abstraction in place, unit tests can run on any machine without the HAT attached. You can either:

  • Use the VirtualEngineDevice for realistic, append-only command capture, or
  • Mock IEngineDevice with your favorite library (e.g., NSubstitute, Moq) to verify interactions and error paths.

Example with NSubstitute (verifying delegation and error handling):

C#
 1  [Fact]
 2  public void SendRawCommand_ShouldDelegateToDevice()
 3  {
 4      IEngineDevice engine = Substitute.For<IEngineDevice>();
 5      InMemoryLogger<BrickCommandProvider> logger = new();
 6      BrickCommandProvider provider = new(engine, logger);
 7
 8      provider.SendRawCommand("FORWARD");
 9
10      engine.Received(1).SendRawCommand("FORWARD");
11  }
12
13[Fact]
14public void SendRawCommand_WhenDeviceThrows_ShouldLogAndRethrow()
15{
16    IEngineDevice engine = Substitute.For<IEngineDevice>();
17    engine.When(e => e.SendRawCommand(Arg.Any<string>())).Do(_ => throw new InvalidOperationException("boom"));
18    InMemoryLogger<BrickCommandProvider> logger = new();
19    BrickCommandProvider provider = new(engine, logger);
20
21    InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => provider.SendRawCommand("ERR"));
22    Assert.Equal("boom", ex.Message);
23}

You can also unit-test the behavior of the virtual implementation itself. For example, ignoring empty commands and honoring the ring buffer limit:

C#
 1[Fact]
 2public void VirtualDevice_ShouldIgnoreEmptyAndKeepCapacity()
 3{
 4    IOptions<EngineDeviceOptions> options = Substitute.For<IOptions<EngineDeviceOptions>>();
 5    options.Value.Returns(new EngineDeviceOptions { SerialPort = "/dev/serial0" });
 6
 7    VirtualEngineDevice device = new (options);
 8
 9    device.SendRawCommand("");          // ignored
10    device.SendRawCommand(" ");         // ignored
11    device.SendRawCommand("FORWARD");   // kept
12
13    Assert.Single(device.Commands);
14    Assert.Equal("FORWARD", device.Commands[0]);
15}

Tips:

  • Treat tests that talk to the real HAT as integration tests. Mark/skip them on CI unless a Pi runner is available.
  • Test concurrency: ensure your orchestration code doesn’t interleave commands. The lock in the physical device helps here.
  • Test disposal and error paths: verify your component logs and surfaces exceptions appropriately.

Conclusion

By introducing a tiny abstraction, you can keep all your orchestration logic unit-testable while still supporting the real device at runtime. The virtual implementation enables fast and deterministic tests and the factory provides a graceful fallback when hardware isn’t present.

It’s only a few extra lines compared to “spaghetti” code that talks to the HAT directly–but it pays off immediately with confidence, safety during refactors and easier collaboration. From here, consider adding richer simulations (timing, failures), categorizing integration tests and wiring this into your CI so both fast unit tests and slower device tests have their place.

Related articles

Raspberry Build HAT: Controlling LEGO Engines with .NET

Jul 24, 2025 - 5 min read

Raspberry Build HAT: Controlling LEGO Engines with .NET

In this beginner-friendly guide, you’ll learn how to control LEGO motors connected to a Raspberry Pi via the official Raspberry Pi …

Raspberry Pi WS2812B Lightstrip Unit Testing Strategies with .NET

Aug 14, 2025 - 7 min read

Raspberry Pi WS2812B Lightstrip Unit Testing Strategies with .NET

Testing hardware-driven projects pays off quickly. Compared to driving a single LEGO motor, controlling a WS2812B lightstrip involves timing …

Raspberry Pi WS2812B Lightstrip Control with .NET

Aug 5, 2025 - 7 min read

Raspberry Pi WS2812B Lightstrip Control with .NET

In this beginner-friendly guide, you’ll learn how to control a WS2812B (aka “NeoPixel”) LED light strip with a Raspberry …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact