Dynamic tests with mstest and T4

 Mar 5th, 2011 

 , , , , , ,

If you used mstest and NUnit you might be aware of the fact that the former doesn’t support dynamic, data driven test cases. For example, the following scenario cannot be achieved with the out-of-box mstest: given a dataset, create distinct test cases for each entry in it, using a predefined generic test case.

The best result that can be achieved using mstest is a single testcase that will iterate through the dataset. There is one disadvantage: if the test fails for one entry in the dataset, the whole test case fails.

So, in order to overcome the previously mentioned limitation, I decided to create a text template that will generate the test cases for me. As an example, I will write some tests for an integer multiplication function that has 2 bugs in it:

public int Multiply(int a, int b)
{
    //This conditions are simulating the 2 bugs
    if (a == 0 && b == 1)
        return 100;
    if (a == 1 && b == 0)
        return -100;
    return a * b;
}

The classical approach (no dynamic test)

Without using any ‘hacks’, one could write the tests for the Multiply function in the following way:

//Tuple description <value of param a, value of param b, expected result>
private static readonly Tuple<int, int, int>[] TestData = new Tuple<int, int, int>[]{
    new Tuple<int, int, int>(0,0,0),
    new Tuple<int, int, int>(2,3,6),
    new Tuple<int, int, int>(1,0,0), //These will trigger one of the bugs
    new Tuple<int, int, int>(-2,-3,6),
    new Tuple<int, int, int>(0,1,0) //These will trigger one of the bugs
};
[TestMethod]
public void TestMultiply()
{
    foreach (var data in TestData)
    {
        Assert.AreEqual(data.Item3, Multiply(data.Item1, data.Item2),
                        "Failed for input ({0}, {1})", data.Item1, data.Item2);
    }
}

Running the test will surface only one of the bugs, the one triggered by the input (1,0):

This is not only bad because it doesn’t give a complete overview of the bugs but it also violates the principle of one assertion per test because more than one assertion could be triggered in the test case above.

The T4 approach (dynamic test)

Read the rest of this post »

To visualize or not to visualize

 Nov 16th, 2009 

 , ,

VSImageVisualizer is one of my free open-source projects. Basically, it is debug visualizer for images for Visual Studio 2008 that allows developers to inspect the graphical representation of Image objects while in Debug mode.

Check the project page for more info.

New Windows Azure Tools (July 2009)

 Jul 21st, 2009 

 , , , ,

AzureLogoAfter releasing the pricing scheme and the business model, Microsoft announced a new set of tools for Windows Azure.

Windows Azure Tools for Microsoft Visual Studio extend Visual Studio 2008 and Visual Studio 2010 Beta 1 to enable the creation, building, debugging, running and packaging of scalable web applications and services on Windows Azure.

The most interesting feature is probably the support for multiple roles. Now one can deploy many web/worker roles.

What’s new in the Windows Azure SDK?

  • The Windows Azure SDK supports developing and deploying services containing multiple web and worker roles. A service must include at least one role, of either type, and may include zero or more web roles, and zero or more worker roles.
  • The new PowershellRole sample hosts the Powershell runtime within a Windows Azure role. It demonstrates a simple remote console web role as well as a worker role whose implementation is a Powershell script. The host also includes the CloudDrive sample snap-in to provide access to Windows Azure Storage from script.

What’s new in Windows Azure Tools for Visual Studio

  • New project creation dialog
  • Support for developing and deploying services containing multiple web and worker roles. A service may contain zero or more web roles and zero or more worker roles with a minimum of one role of either type.
  • Ability to associate any ASP.NET Web Application project in a Cloud Service solution as a Web Role
  • Support for building Cloud Services from TFS Build
  • Enhanced robustness and stability

The new CTP Release can be downloaded from this link.

Software That You Should Install

 May 26th, 2009 

 , , , , , ,

Here is a list of what I recently installed or I plan to install. It is a list of software (updates) that you might consider useful:

Windows Vista/2008 Service Pack 2

It was released a few hours ago for general audience and it brings some important fixes. I think is a must-have. It is a must have and now can be downloaded through Windows Update.

I have just installed it through Windows Update. It took about 30 minutes to complete (download + install).

vistasp2

Visual Studio 2010 Beta

The lastest version of the well known IDE brings a lot of new features: editor completely written in WPF, a more eye candy user interface, .NET Framework 4 (dynamic types for C#, named parameters, implicit values, etc.). It is available for free and you can download either Professional or the Team Suite edition (I recommend the last one).

VS2010Beta

Office Mobile 6.1 Upgrade

Read the rest of this post »

Binding in WinForms like in WPF Part 1

 Jul 6th, 2008 

 , ,

If you have ever used Windows Presentation Foundation (WPF) then you might have been impressed by the binding features of it.

Part one of this tutorial will show how to bind properties of the same type while the second one will also anlyse type conversion.

Using data binding you can synchronize object properties. The source of the binding is the object or property that is changing the value and the destination is the one who receives it (the new value).

There are two types of binding:

  • one way binding – there is only one source that updates the information and as many destinations who receive the update.
  • two way binding – every binding element can update and can receive updates so there is no distinction between source and destination.

Read the rest of this post »

How expensive are cross-domain operations?

 Apr 28th, 2008 

 , , , , ,

I was curios how expensive is a cross domain operation so I have made a test.

The test procedure is simple. Perform a number of cross-domain and non cross-domain operations (get the value of NextNumber() from same domain and from another domain) and measure the time elapsed. For each value I have runned the application 3 times, recorded the time (in milliseconds) and created the mean of this three.

For this I’ve created a Console Application containing two classes, the main class and “NumberClass” which has a method that returns the next long. The code for this two is below:

NumberClass:

//MarshalByRefObject is used because this object will cross domain boundary
class NumberClass:MarshalByRefObject
{
    public ulong number = 0;
    public ulong NextNumber()
    {
        return number++;
    }
}

The Main method from the main class:

Read the rest of this post »

EventInfo.AddEventHandler – bug or not?

 Apr 16th, 2008 

 , , , , ,

Reflection is the process by which a computer program can observe and modify its own structure and behavior. The programming paradigm driven by reflection is called reflective programming. [Wikipedia]

Working a few days ago on a plugin architecture on .NET Framework I’ve found something very interesting. The method System.Reflection.EventInfo.AddEventHandler does late-binding. You’ll probably say “so what?”. Let’s see the problem with an example:

Using Visual Studio 2005/2008, create two C# projects (a windows forms application project and a class library one). Rename the class from the class library project to “TestClass.cs”.

Read the rest of this post »