Designing Clusters for Testing and Portability#

Unit Testing Code-Driven Clusters#

Unit tests should focus on behavioral correctness and spec compliance.

ClusterTester Helper Class#

The ClusterTester helper class (in src/app/server-cluster/testing/ClusterTester.h) is mandatory for testing code-driven clusters. It abstracts TLV handling, memory management for views, and fabric context.

The unit test instantiates the cluster, provides a mock delegate, and uses ClusterTester to interact with it.

Important tests to consider:#

  • Initialization: Initial attribute values are correct after Startup.

  • Range Checking: Errors (e.g., ConstraintError) for out-of-range values in setters and command handlers.

  • Boundary Cases: Behavior at minimum, maximum, and with null values for nullable attributes.

  • Spec Compliance: All spec-defined error conditions are handled correctly.

  • Side Effects: Event generation and dirty attribute marking for state changes.

  • Delegate Interaction: Proper calls out to the delegate and handling of delegate-reported errors.

  • Fabric Isolation: Correct behavior for fabric-scoped attributes and commands.

For detailed API usage and examples, please see the ClusterTester Helper Class Guide.

Unit testing existing clusters#

  • Option 1: Refactor

    • Refactor the cluster to use DefaultServerCluster and ClusterTester. This is the preferred path for long-term maintainability.

  • Option 2: Test at Interface Boundary

    • Instantiate the cluster and use ClusterTester to interact with it via the ServerClusterInterface boundary. This is useful for existing implementations that cannot yet be fully refactored.