Testing
Testing strategies and best practices for Morphir .NET
Overview
Morphir .NET supports multiple testing approaches to ensure code quality and correctness.
Unit Testing with TUnit
TUnit is the primary unit testing framework:
using TUnit.Assertions;
using TUnit.Core;
public class TypeExprTests
{
[Test]
public void TInt_Should_Be_Equal()
{
var type1 = new TypeExpr.TInt();
var type2 = new TypeExpr.TInt();
Assert.That(type1).IsEqualTo(type2);
}
}
Behavior-Driven Development with Reqnroll
Reqnroll enables BDD-style testing:
Feature: Type Expression Creation
Scenario: Create an integer type
Given I want to create a type expression
When I create a TInt
Then it should be a valid type expression
Property-Based Testing
Use property-based testing for invariant validation:
[Property]
public bool RoundtripSerialization(TypeExpr typeExpr)
{
var json = JsonSerializer.Serialize(typeExpr);
var deserialized = JsonSerializer.Deserialize<TypeExpr>(json);
return typeExpr.Equals(deserialized);
}
Contract Testing
Test compatibility with Morphir IR format:
[Test]
public void Should_Roundtrip_With_Morphir_Elm()
{
// Load canonical IR sample
var json = File.ReadAllText("samples/canonical.json");
var ir = JsonSerializer.Deserialize<IR>(json);
// Serialize back
var roundtrip = JsonSerializer.Serialize(ir);
// Verify compatibility
Assert.That(roundtrip).IsValidJson();
}
Best Practices
- Exhaustive Testing: Test all ADT cases
- Edge Cases: Test boundary conditions
- Roundtrip Tests: Always test serialization roundtrips
- Property Tests: Use property-based testing for invariants
- Coverage: Maintain >= 80% code coverage
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.