Custom Test Lifecycle
Every test suite in ZeUnit runs with a class factory resolved from IZeLifecycle<TFactory>. If you do not inherit a lifecycle base class such as TransientSuite or ReEntrySuite, ZeUnit defaults to TransientLifecycleFactory.
You can define your own lifecycle when you want custom pooling, scoped per-batch instances, or specialized teardown logic.
1. Implement IZeClassFactory
IZeClassFactory has two responsibilities: return an instance of the test class from Get(), and release resources in Dispose().
using System.Reflection;
using ZeUnit;
public sealed class PooledClassFactory : IZeClassFactory
{
private readonly TypeInfo _typeInfo;
private readonly ComposerClassFactory _composers;
private object? _cached;
public PooledClassFactory(TypeInfo typeInfo, ComposerClassFactory composers)
{
_typeInfo = typeInfo;
_composers = composers;
}
public object Get() =>
_cached ??= TransientLifecycleFactory.CreateInstance(_typeInfo, _composers);
public void Dispose()
{
if (_cached is IDisposable disposable)
disposable.Dispose();
_composers.Dispose();
}
}
Factory Constructor Conventions
ZeDiscovery resolves your TFactory using the first matching constructor signature: (TypeInfo, ComposerClassFactory), (TypeInfo), or a parameterless constructor.
2. Expose an IZeLifecycle<TFactory> Base Class or Interface
public abstract class PooledSuite : IZeLifecycle<PooledClassFactory>
{
}
3. Apply the Lifecycle to a Test Class
public class ConnectionPoolTests : PooledSuite
{
private int _invocations;
public Fact FirstQuery() => (++_invocations).IsGreaterThanOrEqualTo(1);
public Fact SecondQuery() => (++_invocations).IsGreaterThanOrEqualTo(1);
}
When all tests belonging to the factory's group finish (including cancellation or errors), ZeDependencyScheduler disposes the IZeClassFactory cleanly.