Let's consider a simple fixture with a unit test that must throw. In this case, we use the ExpectedExceptionAttribute to tell the framework that the method should throw:
ExpectedExceptionAttribute
[TestFixture] public class MyFixture { [Test] [ExpectedException(typeof(Exception))] public void ThisMethodThrows() { ... } }
ExpectedExceptionAttrribute is called a Decorator attribute because it decorates and alter the behavior of a test. Another commonly used decorator is IgnoreAttribute to ignore a test.
ExpectedExceptionAttrribute
IgnoreAttribute
In MbUnit, other decorators are available. Here I describe two of those: RepeatAttribute and ThreadedRepeatAttribute. RepeatAttribute will make the execution of the test repeated the desired number of times in the same thread while ThreadedRepeatAttribute will launch simultaneously a desired number of threads that will execute the method. RepeatAttribute create a sequence of execution, ThreadedRepeatAttribute create a parallel execution.
RepeatAttribute
ThreadedRepeatAttribute
[Test] [Repeat(10)] // this will make the RepeatedTest executed 10 times public void RepeatedTest() { ... } [Test] [ThreadedRepeat(10)] // this will make the RepeatedTest executed in 10 concurent threads public void AmIThreadSake() { ... }
Decorators are chained internally in the order that they are defined, therefore declaration order is important if you want to combine them. For example, the two following test methods have different signification:
RepeatAndThrow
ThrowWhileRepeating
[Test] [Repeat(10)] [ExpectedException(typeof(Exception))] public void RepeatAndThrow() { ... } [Test] [ExpectedException(typeof(Exception))] [Repeat(10)] public void ThrowWhileRepeating() { ... }
Page rendered at Monday, October 13, 2008 11:33:25 AM UTC
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.