Koen about .Net

February 16, 2011

Custom C# formatter for Selenium

Filed under: Development, dotnetmag, Testing — Tags: , , — koenwillemse @ 22:20

In my previous post I mentioned that I had been working on a C# formatter for selenium which uses MSTest as test framework and also FluentAssertions to make the tests more readable. After a few hours last Saturday night I had something working which looks pretty ok for now. I guess I’m not the only one wanting to generate this code, so I wanted to share what I’ve got.

I started off by taking the source of the C# formatter which is installed together with the selenium IDE. Note that there is a bug (maybe more, but I ran into this one Winking smile) in the source, which results in a lack of options to set in the IDE. The problem is regarding the configForm setting, which is done in the options. This is pretty strange if you look at it, because that configForm is used to generate the UI for the options (which options are available can be found here). The configForm should be set outside the options. I tried to modify the source, but that’s not possible, it’s read-only. If you still want to use the NUnit formatting as done in the formatter, you can add another formatter and add the modified source. You can download the modified source here.

Now to the formatter I wrote. I wanted a few things:

  • MSTest as test framework
  • use FluentAssertions for readability
  • Start the selenium server when starting test run (and close it when tests finished)

There is a description here on how to start creating a custom formatter. However, I was lazy, so I copied the source from the existing C# formatter and modified it to accommodate my needs. First of all I fixed the problem with the options, which I described above.
Next I changed the assertion methods to use the FluentAssertions syntax.
I updated the generated header and footer, since I don’t want all the initialization of a Selenium instance etc. to be done on every TestInitialize since it takes several seconds. I changed it to be done on a central location.

The biggest change I made was add formatting for a test suite, which is not available in the default C# formatter. In the code for the test suite, I added methods for AssemblyInitialize and AssemblyCleanup. In these method, I will start and tear down the selenium server process and the selenium instance. This uses a static selenium instance, so this will cause problems if you want to use it in a multi-threaded environment, but for now, it matches my needs. The complete source of the formatter can be downloaded here.

I hope some of you find this helpful also. If you have some good suggestions for the formatter, then please let me know.

Edit 22-02-2011: The links to the sources were incorrect. I updated the links now, so you can download the files from my skydrive.


June 29, 2010

Fluent assertions release 1.2.2

Filed under: Development — Tags: , , — koenwillemse @ 15:44

The last few days I’ve been working on a new release of the Fluent Assertions, since I’ve started using them again on my current project. I ran into a bug myself and saw a few bugs and feature requests on the issue tracker, so I decided to fix some of them.

I just released version 1.2.2 so you can download it here.

The following fixes / new features are available:

Hope these fixes will also be helpful to you all. Download the latest release now and let us know if you have any suggestions of bugs found.

July 14, 2009

Custom assertions in unittests

Filed under: Development — Tags: , — koenwillemse @ 15:00

One of my collegues has been creating a project with custom assertions to be used in unit tests. The idea behind this is that those assertions make your unittests more readable. You get a notation like something.Should().NotBeNull("woops") instead of Assert.IsNotNull(something, "woops").

At first I was not really impressed with it and didn’t see much improvement in it, but that was because I only saw a few small examples. We’re using it also in our unittests and I must say that it makes it more readable, so check out this blog post that he made about it:

June 11, 2009

Custom enterprise library logging extender for easy overview of logging in unit tests

Filed under: Development — Tags: , , , — koenwillemse @ 14:59

We have been working on a full new .Net architecture at the recent customer I’m working for. We use a lot of tooling / guidance provided by the Patterns and Practices group of Microsoft. One of the components we are using is the Logging Application Block (part of the Enterprise Library 4.1).

We use a facade which we created to write logentries, which are stored in a file, etc. etc. etc… We also have this functionality in our unit tests which creates one huge log file every time we run all the tests. Very frustrating if you want to find a query that was executed or something like that. We added some markers before and after every unittest, but still, not so great.

Today I implemented a very simple solution for this. I created a custom listener which I configured to be used in the unittest project. The code looks like this:

using System;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;

namespace DemoCode
    public class ConsoleTraceListener : CustomTraceListener
        /// Writes trace information, a data object and event information to the listener specific output.
        /// A  object that contains the current process ID, thread ID, and stack trace information.
        /// A name used to identify the output, typically the name of the application that generated the trace event.
        /// One of the  values specifying the type of event that has caused the trace.
        /// A numeric identifier for the event.
        /// The trace data to emit.
        public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, 
                                       int id, object data)
            var logEntry = data as LogEntry;
            if ((logEntry != null) && (Formatter != null))

        /// When overridden in a derived class, writes the specified message to the listener you create in the derived class.
        /// A message to write. 
        ///                 2
        public override void Write(string message)
            throw new NotImplementedException();

        /// When overridden in a derived class, writes a message to the listener you create in the derived class, followed by a line terminator.
        /// A message to write. 
        ///                 2
        public override void WriteLine(string message)

Writing to the Console has as an effect that it is displayed in the Test Result View.
Next configure it in the app.config of the testproject and voila, the result when running a test:

The result

The result

Simple right? Why didn’t we think of that earlier…
Just one small thing; we use specific widths for the columns in the logging, which looks good in the plain text log file, but not nicely aligned in the result view for the test. Whatever, the information is there, if it must be nicely aligned, copy/paste it a text editor ;-).

See ya,

Create a free website or blog at WordPress.com.

%d bloggers like this: