Skip to content
Illegitimis edited this page May 28, 2017 · 6 revisions

Properties of a good unit test

properties of a good unit test
atomic should target a small piece of functionality, hard to maintain otherwise
deterministic pass or fail, never inconclusive
repeatable consistent with dependent objects changing, time passing and between runs
order independent & isolated should not run be forced to run in a specific order, other tests or dependencies should not prevent test passing
fast order of ms
easy to setup should not be cumbersome

NUnit

NUnit Assert.That
object type and properties Is.TypeOf<T>(), Is.InstanceOf<T>(), Has.Property("PropertyName")
exceptions Throws.Exception Throws.TypeOf() .With.Matches(predicate)
strings Is.EqualTo("expected") Is.EqualTo("eXpeCTed").IgnoreCase Is.Not.EqualTo("notExpected")
Numerical values Is.EqualTo(int) Is.EqualTo(float).Within(tolerance) Is.EqualTo(val).Within(p).Percent Is.Positive / Is.Negative / Is.NaN
DateTime Is.EqualTo(dt).Within(ts) Is.EqualTo(dt).Within(v).Milliseconds
Ranges Is.GreaterThan(v) / Is.LessThan(v) Is.GreaterThanOrEqualTo(v) Is.InRange(lo,hi)
Collections Is.All.Empty Contains.Item(item) Has.Some.ContainsSubstring("sub_str") Has.Exactly(m).EndsWith("suffix") Is.Unique Has.None.EqualTo(val) Is.EquivalentTo(actualCollection) Is.Ordered
References Is.SameAs Is.Not.SameAs
Nulls & booleans Is.Not.Empty, Is.Null, Is.True

<<

Clone this wiki locally