One step past Assert.Throws<>

If you’re not using xUnit (and you should be), at least consider adopting the practices they have for dealing with exceptions in unit tests. From the very first release, they moved us from this:[ExpectedException(typeof(InvalidOperationException))] public void WithdrawingMoreThanBalanceThrows() {     Account account = new Account();     account.Deposit(100);     account.Widthraw(200); } to the far-better: public void WithdrawingMoreThanBalanceThrows()…

2

ReSharper and xUnit.net

Those of you who have switched over to xUnit.Net may have a received a nasty surprise with the ‘type members layout’ feature of ReSharper: it doesn’t respect method attributes. Namely, you can tell it to not reorder methods in an NUnit fixture like this:  <Pattern>   <Match>       <HasAttribute CLRName=”NUnit.Framework.TestFixture”/>     </Match>   </Pattern> But you cannot…

5

Weaving with xUnit.net

There has been mixed reaction to the removal of [SetUp] and [TearDown] in xUnit.net. Personally, I think it’s great as it helps to raise unit test ‘smells’, particularly around how classes interact with each another. Here’s a small example of how we can use the BeforeAfterTestAttribute in xUnit to remove duplicate code. (It’s based on…


Faking out [PrincipalPermission]

Depending on how your unit tests are setup, you may not be running as the proper user and won’t have the necessary permissions to invoke a method. If this happens, you can easily alter the principle thread’s identity with the following code. For instance, if you user needs to belong to a certain role (or…


xUnit.net goes live!

NUnit is dead! Long live xUnit.net! There are several cool features, including aspect-like extensibility for tests, but the two changes that will immediately make a difference in my day-to-day coding are: [ExpectedException] is gone. In its place is Assert.Throws<T>( delegate … ). Not only much easier to read, but no longer can a test throw in…