How To Order Unit Tests in Visual Studio 2008

Sometimes it is maybe necessary to bring some order into your unit tests in Visual Studio. Indeed, this is not really the idea of unit testing, however it might be necessary for a variety of reasons. Facing this issue, I had to search the MSDN documentation for a while.

I was desperately looking for some syntax like:

[TestMethod(Order = 1)]

However, the only way to bring some order in your tests is, by creating some ordered tests in addition to your unit tests.

To do so, you first create a set of unit tests. In my case, some of the tests should be only executed after other run successfully. Otherwise the result might look like:

Failed Unit Test

Now we create a new test project called Order Test:

Add New Ordered Test

When opening the project you actually can arrange the order of the previously written unit tests:

Arrange an Ordered Test

Running the test then is possible by selecting the test from the Test View window. Your tests will actually only appear as one entry in the list but you will see that all the selected tests run successfully – in the correct order.

Succesfull Ordered Test

You Have Created a Service

Sometimes, simple things end up as epic battles. If you try to MSN Search and Google for the nasty “You have create a Service” page for IIS hosted WCF services, you will and up with a lot of pre-release information and noise in the search results. Actually, you might spend days in finding some relevant information.

You have created a service

Yesterday, I was finally pointed to the right place in the MSDN documentation where you can find this specific information:

<serviceDebug httpHelpPageEnabled="Boolean"
    httpHelpPageUrl="Uri"
    httpsHelpPageEnabled="Boolean"
    httpsHelpPageUrl="Uri"
    includeExceptionDetailInFaults="Boolean" />

With the serviceDebug tag it should be possible to get rid of the page. It is easy to find as long as you know you have to search for the term HTML help page.

WCF, .NET 3.5 and HTTP Request Content

The new Web Programming Model of WCF and the .NET Framework brings some very useful features such as the WebGet and the WebInvoke Attribute and the capability to deal directly with HTTP requests. But how do we access the HTTP request in these methods? Dealing with HTTP requests and responses using the System.Net namespace is quite straight forward. Therefore, we have a look in sending and receiving a simple HTTP request:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(myUrl);byte[] a_dataBytes = Encoding.UTF8.GetBytes(data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = a_dataBytes.Length;

Stream requestStream = request.GetRequestStream();
requestStream.Write(a_dataBytes, 0, a_dataBytes.Length);
WebResponse response = request.GetResponse(); StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); var content = responseReader.ReadToEnd();

Now we want to access the HTTP context within a operation with the following signature:

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "{uri}")]
bool Put(string uri);

Accessing the context of the incoming request is straightforward using the WebOperationContext class.

IncomingWebRequestContext context =
WebOperationContext.Current.IncomingRequest;

var length = context.ContentLength;
var type = context.ContentType;
var headers = context.Headers;

Since the documentation does not provide many example code, and the ORCAS samples do not cover this yet, I had to figure out how to access the content of the incoming request. After spending hours on MSN Search, Google and the MSDN Library I finally tried out something. In some general examples, either a Message or a Stream is passed over as single parameter to the operation. Hence, I changed the contract as follows:

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "{uri}")]
bool Put(string uri, Stream stream);

Reading the content then is easy:

var reader = new StreamReader(stream);string content = reader.ReadToEnd();

This solution however comes up with one major drawback: If you try to access your foo.svc via browser you will end up some error telling you “For request in operation Post to be a stream the operation must have a single parameter whose type is Stream.”. Also calling foo.svc?wsdl will end up in some exception in the WSDL export extension.

Disabled MenuItems in WPF

Maybe this is a bug: Creating a ContextMenu as child of a Canvas, the event handlers CanExecute and Execute have never been executed? This took me almost two hours to figure out why. Basically, I do my command binding like

CommandBinding exitCmd = new CommandBinding(DesignerCommands.Exit);
exitCmd.CanExecute += new CanExecuteRoutedEventHandler(exitCmd_CanExecute);
exitCmd.Executed += new ExecutedRoutedEventHandler(exitCmd_Executed);
CommandBindings.Add(exitCmd);

To me, it looks like there is a minor problem in command binding whilst using command binding this manner in controls which never get the focus (such as a Canvas). Since the handlers are never fired. An easy workaround is to add additional command binding within your XAML code.

<ContextMenu.CommandBindings>
 <CommandBindingCommand="e:MyCommands.Exit" CanExecute="exitCmd_CanExecute" Executed="exitCmd_Executed"/>
</ContextMenu.CommandBindings>

I checked a couple of web sites and found one site, where the problem is described a bit more in detail. Also Aaron describes a second work around that requires some lines of code.