How to add RowTest support to MSTest


Wednesday, January 26, 2011

I’ve blogged (read: complained) about the shortcoming of Microsoft’s unit testing framework in the past – lack of required features such checking exception messages and plain nuisances like its notorious habit of spawning new vsmdi files without any cause. On the other side MSTest has a good integration with Visual Studio that makes it worth while.
One of the features I miss when working with MSTest is Row Test or TestCase for you NUnit crowd. At times I want to be able to run the same test with different parameters:
[Row(0, 1, 1)]
public void TestRowAdd(int number1, int number2, int result)
Assert.AreEqual(result, number1 + number2);

I know this sample is somewhat trivial but bare with me – I’m trying to make a point…

Of course I could have written the test this way:

public void TestRowAdd()
Assert.AreEqual(3, 1 + 2);
Assert.AreEqual(1, 1 + 0);
Assert.AreEqual(0, -1 + 1);
Assert.AreEqual(-2, -1 + -1);

The problem with this solution is that the first Assert that fails throws the exception and so if the 2nd Assert fails I have no idea what is the end result of the remaining Asserts.

Another solution is to split the test into several tests (one per assert) but it get tedious real quick and then you start to create methods inside your tests that hide the actual implementation:

public void TestRowAdd1()
TestRowAdd(1, 2, 3);

public void TestRowAdd2()
TestRowAdd(1, 1, 0);

The only solution that comes close to real live Row test is to use Data-Driven Test but the downside is that the tests inputs are out of site in an external file.

I’ve been searching for a simple way to add row testing support to MSTest until a good friend pointed out that Microsoft Testing framework has extensibility built in – the good thing is that in VS2010 it just got easier to extend existing functionality.

The the good people of MS Team Test group have provided two tutorials one on how to add the ability to run tests with different privileges and the 2nd tutorial is all about adding RowTest support – and the source code to both tutorials can be found in the MSDN code gallery. Using the code makes the test in the beginning of this post possible and it’s easy to create and customize. The only question I have is why wasn’t RowTest released as part of VS2010 – but until it is part of the “official” MSTest family just create (or copy) your own.


Related Links:

Extending the Visual Studio Unit Test Type, part 1 Extending the Visual Studio Unit Test Type, part 2 Unit Test Extensibility Sample (source code for the tutorials above)

Book review: C# in Depth, Second Edition

1 comment

Tuesday, January 11, 2011

2735041When I first picked up C# in Depth I was expecting to be bored. Although I wanted to read it I thought that most of the book will be introductory level C# programming such as you might expect from a book that teaches a single language. I was wrong!

So if you’re looking for a book to learn C# from scratch – look elsewhere on the other hand if you already have some C experience under your belt and you want to learn more – this is the book for you. The book doesn’t waste any time on teaching the basics of C# (i.e. this is a for loop…) instead it takes you through the C# language and explains about how they cam to pass along with a few gotchas.

The book is divided into four parts each focuses on a different version of C# – the 1st part is all about C# 1.x, the 2nd part is about C# 2.0 – you get the point. in each part Jon Skeet helps understand the design decisions that created the current feature set and how each version was built on to of it’s predecessor.

Part1 - Preparing for the journey

The 1st part is made up from two parts, the first is about preparing for the book – C# specifications, using the snippet compiler and so on. the second part is about the beginning of C#delegates, the C# type system, reference and value types and how C# 2 is going to make it all better.

Part 2 - C# 2: solving the issues of C# 1

The 2nd part is where things are getting interesting – Jon shows how the second coming of C# made things easier for developers. I forgot how much syntax I needed to write in order to listen to an event – and now I know why I no longer needs it. Needless the say the big feature of C# 2.0 – generics has most of the focus of this part  -along with clear and thorough explanation about how they work. Other feature of C# 2.0 you can find in this part are partial classes (and when to use them), nullable types, static classes to name a few.

Part 3 - C# 3: revolutionizing how we code

The chapters in this part are all about C# 3.0 and 3.5, beginning with the syntactic sugar of auto-properties, object initializers, var and anonymous types going to lambdas and expression trees to extension methods finishing with the reason for most of these features – Linq. I’ve known and used all of these features in the past but this chapter helped me understand them better.

Part 4 - C# 4: playing nicely with others

The new kid on the block – all of the new features for the latest C# to date – optional and names parameters and why you need them (hint: COM). Generics covariance and contravariance that would make you head spin. And other changes

Part 5 – The appendixes

I usually don’t read the appendixes and use them for ad-hoc reference but in this case I kept on reading because they felt like an integral part of the book. There are three of them –

  • LINQ standard query operators
  • Generic collections in .NET (including the new concurrent collections)
  • Version summaries (ok that one you can pass)

To summarize - I enjoyed reading C# in depth, it’s a good book that a c# developer can benefit from and I can’t wait till the third edition comes out – hopefully shortly after C# 5 is released.

Don’t avoid difficult tasks

No comments

Wednesday, January 05, 2011

I was late to work yesterday.

Everything was going well – I had my year old daughter, her bag, my bag, my car keys Keysand was ready to leave I got to the door and I couldn’t find the house keys…

I knew I had them in my hand a minute ago but for the life of me I could not remember where I put it. I gently put my daughter on the floor where she played with one of her toys and started searching for the missing keys – I’ve emptied my pockets, searched all the rooms top to bottom but couldn’t find the keys. Usually when I loose an item I try to retrace my steps: I remembered taking the keys, grabbing the kid, throwing the trash… Because I was a bit tired this morning I assumed that the keys might be at the bottom of my trash bag but I preferred to re-check every other location before I “dive” into my trash and try to find it. After a few more minutes of search and no keys I’ve decided to open the garbage bag and look for the keys and there they were – at the bottom of a very unpleasant pile of waste.

A header into turqouise waterThe reason I’m telling you this story is that I could have saved about 20 minutes if I would have done an unpleasant task that I knew I might need to do instead of trying to avoid it – by looking several times everywhere else possible. This is very similar to situation that we face everyday at work – we have a difficult or unpleasant task that needs to be done and we put it away or try to avoid it by doing something else. More often than not the unpleasant task will wait for us until it would be either harder or too late to do a proper job, you might even waste valuable time avoiding the task just like I did looking for my keys.

The solution is simple – if you have a task outside of your “comfort zone” do it! instead of waiting till your initial fear (or discomfort) of that task will subside jump in head first. Getting out of the comfort zone takes some getting used to but it get easier the more you do it.

After the task is done you might even notice that it was not that difficult after all.

Related Posts Plugin for WordPress, Blogger...