Birdseye View of ZeUnit
In the previous section Getting Started, we covered the basics of getting ZeUnit testing set up in your solution. Aside from some clever assertion syntax, ZeUnit has yet to offer any real value over the existing frameworks like XUnit, NUnit or MS Test.
Make ZeUnit Yours
The language features that are used as examples in this section are just a small subset of the features that ZeUnit has to offer. More importantly, ZeUnit is designed to be extended and customized to fit the needs of your project.
Simple Suite
public class SimpleSuite
{
public Fact GetExpectedValue()
{
var actual = GetSomeString();
return actual == "expected";
}
}
The example above in the class SimpleSuite, is the most basic form of a test in ZeUnit. There is no constructor injection or custom method bindings. As a result, it defaults to TransientSuite with a TransientLifecycleFactory for the class lifecycle.
The test itself, at a low level, boils down to a simple assertion: return value == "expected";. With a little help from implicit type operators magic, the bool is converted to a Fact<bool> allowing the code to compile and the test to report results.
A secondary implicit conversion operator is also created on Tuple<bool, string> to support custom messages to be returned along with the test's status.
public class SimpleSuite : TransientSuite
{
public Fact GetExpectedValueWithMessage()
{
return (actual == "expected", $"Expected value does not match {actual}!!");
}
}
Suite Lifecycle
The example above explicitly defines the lifecycle by inheriting from the TransientSuite class, this the default behavior for classes that don't inherit a Suite class. More on the lifecycle in the Test & Suite Lifecycle section.
Adding Assertions
While the shorthand bool and Tuple<bool, string> are useful for quick and dirty tests, a simple pass and fails to give enough details for troubleshooting, having to write out custom messages for every test would become very tedious. This is where the ZeUnit.Assertions namespace shines. The shouldly inspired collection of helpers supercharge the testing process.
public class AssertionSuite
{
public Fact GetExpectedValue()
{
var actual = GetSomeString();
return actual
.IsNotNull()
.IsType<string>()
.Is("expected");
}
}
A similar test could be written using the bool, but would require that it is broken into 3 separate tests or that the test returns the type IEnumerable<Fact> allowing for yield return syntax for multiple assertions.
The other added value in using the assertion library, over the shorthand bool tests, is rich reporting on the objects under the tests. This is something that is lost when implicitly converting from bool to Fact.
The Kitchen Sink
The TheKitchenSinkSuite class really showcases the power of ZeUnit to create many tests with very few lines of code. Out of the gate, there are two attributes that define the attributes which are used for class composition to fill the test class dependencies.
- The
[Only(typeof(SingletonCounter))]attribute registers a shared singleton instance for all tests that inject that type. You can also subclassOnlyAttribute<TType, TAttribute>to build strongly-typed singletons. - The
[LamarContainer(typeof(CalculatorRegistry))]attribute creates a Lamar DI container fromCalculatorRegistry, allowingICalculatorto be injected into the class constructor.
Class Composition
These attributes are just a taste of what can be done before a test executes by building custom attributes inheriting from ZeComposerAttribute<TComposer> and implementing IZeClassComposer. See Class Composers or Building Class Composers.
[Only(typeof(SingletonCounter))]
[LamarContainer(typeof(CalculatorRegistry))]
public class TheKitchenSinkSuite(ICalculator calculator, SingletonCounter counter)
{
[InlineData(new[] { 1d }, 1d)]
[InlineData(new[] { 1d, 2d }, 3d)]
[InlineData(new[] { 1d, 2d, 3d, 4d }, 10d)]
public Fact AdditionHarness(double[] values, double expected)
{
var actual = calculator.Apply<AddOperation>(values);
counter.Increment();
return actual.Is(expected);
}
}
InlineData is a familiar tool from existing unit testing frameworks, and ZeUnit makes custom method bindings just as easy to create via ZeBinderAttribute<TBinder>. More on this in the Method Binders section.
Back to the single instance of SingletonCounter, which might seem out of place in a transient test suite. However, because ZeUnit schedules tests atop reactive IObservable streams, declaring dependency chains between suites and methods is built in. While unit-test purists avoid shared state, as ZeUnit scales to integration and end-to-end tests, ordered state progression becomes a powerful tool.
[DependsOn("AddOneMoreFirst")]declares that a test method must wait untilAddOneMoreFirstcompletes before executing.[DependsOn(typeof(TheKitchenSinkSuite))](orIDependsOn<TheKitchenSinkSuite>) ensures all tests inTheKitchenSinkSuitefinish beforeNextKitchenSinkSuitebegins.
[DependsOn(typeof(TheKitchenSinkSuite))]
[Only(typeof(SingletonCounter))]
public class NextKitchenSinkSuite(SingletonCounter counter) : ReEntrySuite
{
public Fact AddOneMoreFirst()
{
counter.Increment();
return true;
}
[DependsOn("AddOneMoreFirst")]
public Fact CheckTotalValue()
{
return counter.Value.Is(4);
}
}
Understanding The Lifecycle
In the example above, TheKitchenSinkSuite uses the default TransientSuite lifecycle, running each InlineData binding on a fresh class instance while incrementing the shared SingletonCounter.
Once every test in TheKitchenSinkSuite completes, ZeDependencyScheduler unblocks NextKitchenSinkSuite. By inheriting ReEntrySuite (IZeLifecycle<SingletonLifecycleFactory>), NextKitchenSinkSuite reuses a single suite instance across its methods, and [DependsOn("AddOneMoreFirst")] guarantees AddOneMoreFirst runs before CheckTotalValue asserts the final counter value.
Next Section: Functional First