Showing posts with label concordion. Show all posts
Showing posts with label concordion. Show all posts

Saturday, May 30, 2009

Your First Concordion.Net Project (Part 5)

Running Specs with Gallio

Part 1 – Part 2 – Part 3 – Part 4

We have our specifications developed so now we want to actually run this beasty! After trying to develop my own test runner for Concordion I decided to settle on writing a plugin for the Gallio framework instead.

Gallio is an open testing framework that runs all sorts of .Net test frameworks on a number of different platforms. You can find out more about its adaptability and rich reporting capabilities here.

So how do we get Gallio to run our specifications? We need to use a plugin. In the Concordion distributable file you will notice a directory called Gallio.ConcordionAdapter. This is the plugin specifically written to allow Concordion specifications to be run on Gallio.

What I like to do when running specifications is to start off by running them with Gallio.Echo. This is the command-line executable and I find this easiest to debug problems with. There are a number of other runners for Gallio (like Icarus (the GUI); or runners for Visual Studio, Test-Driven.Net, etc.).

I like to create the specification file in the root directory of the specification project so let’s do that right now. Here’s the file in the Calculator.Spec project folder

Calculator.Sample.11

The file is called run-spec-with-echo.cmd … as you can tell I really like long and descriptive names!

Now let’s fill this up with a command-line. We will need to tell Gallio where to find the Gallio.ConcordionAdapter plugin and where to find our specification assembly. This folder has contents like this:

Calculator.Sample.12

To tell Gallio to find this directory we add the command-line option

/pd:c:\Concordion\Gallio.ConcordionAdapter

This tells Gallio to search in this directory for a .plugin file that it can use to work with the Concordion plugin. Now we need to tell Concordion where our specification assembly is.

bin\Debug\Calculator.Spec.dll

Since the batch file is in our project directory but our specification assembly is in the project output folder we need to add the relative path to the specification assembly. Unless otherwise specified, Gallio.Echo’s working directory is the current folder. Our command file should now resemble this

set GALLIO_PATH=C:\Dev\concordion-net\tools\Gallio-trunk\bin
%GALLIO_PATH%\Gallio.Echo.exe /pd:c:\Concordion\Gallio.ConcordionAdapter bin\debug\Calculator.Spec.dll
pause

So let’s try and run the file! If you do you should see the following results on screen

Calculator.Sample.13

Uh oh … Houston we have a problem! Notice that there is a failure in our specification. If you remember back to the last post we intentionally made our ArithmeticTest fixture a dumb one so that we could see it fail.

Fixing the Fixture

Red-Green-Refactor is the name of the game here! Ok, let’s fix up our test by filling out the Calculator class in our Calculator project and calling that from the specification to perform the arithmetic operations. Our Calculator class should resemble this:

public class Calculator
{
public long Add(long first, long second)
{
return first + second;
}

public long Subtract(long first, long second)
{
return first - second;
}

public long Multiply(long first, long second)
{
return first * second;
}

public long Divide(long first, long second)
{
return first / second;
}
}

And we will want to modify our fixture like so

    [ConcordionTest]
public class ArithmeticTest
{
public long firstOperand;
public long secondOperand;

public long Addition(long first, long second)
{
return new Calculator().Add(first, second);
}

public long Subtraction(long first, long second)
{
return new Calculator().Subtract(first, second);
}

public long Multiplication(long first, long second)
{
return new Calculator().Multiply(first, second);
}

public long Division(long first, long second)
{
return new Calculator().Divide(first, second);
}
}

Ooops … we forgot something! Did you notice? For the above code to work we need to add a reference to the Calculator project in the Calculator.Spec project

Calculator.Sample.14

Good, now we have our reference so it should compile and run successfully now. Let’s see how it goes.

image

Woot! All of our tests now pass! Let’s take a peek at the output now.

image

Notice the green surrounding the results now? That means our Calculator class successfully performs the arithmetic we want!

Epilogue

If you want to see the source code for this series of posts you can download it here.

This sample was pretty basic but it should be enough to get you up and running!

Thanks for trying out this tool, I hope you enjoy it. Stay tuned, there will be more to come on advanced features of Concordion!

Monday, May 25, 2009

Your First Concordion.Net Project (Part 2)

Setting Up Visual Studio

Part 1 – Part 3 – Part 4 – Part 5

If you’re a diligent .NET Developer then you have probably read Microsoft’s guidelines for setting up a Visual Studio project. If you have read this then I would also point you towards this excellent posting

Most Concordion.Net projects should follow the same basic structure of three assemblies:

  • <project-name> – the assembly containing your business logic
  • <project-name>.Test – an assembly for your unit tests
  • <project-name>.Spec – an assembly for your Concordion.Net specifications.

In our example we will be creating a project called Calculator. This will be a very simple project that contains only a calculator class API for others to use in their own code. Yes, I know this is a very simple example but it will have to do for now because I’m not feeling very imaginative! With Calculator we will have the following three projects:

  • Calculator
  • Calculator.Test
  • Calculator.Spec

Calculator.Sample.1

This will be the basic structure of our assembly. While I normally use xUnit for my open source projects I won’t be covering the Calculator.Test project much more than to say that you should have one there, it’s more for completeness.

We will talk about adding references later on as they’re required.

The ConcordionAssembly Attribute

One of the very first things you need to do on any Concordion.Net project is to mark your assembly with a ConcordionAssembly attribute. This allows Gallio (the thing that runs the specifications) know that this assembly contains Concordion specifications. If this attribute is not present then your tests will not be found. You can mark the assembly like this

[assembly: ConcordionAssembly]

place this declaration in the AssemblyInfo.cs file of the Calculator.Spec project.

The next part in this series will talk a bit about how to add specifications to the Calculator.Spec project.