Cory Foy

Wednesday, November 23, 2005

Fitnesse and .NET - a basic tutorial

I've been working more with FIT and FITnesse, especially the .NET port. I tried to locate a quick tutorial to get someone up and running on it with a very basic project, but couldn't seem to come across one. Hopefully this will help you, or someone you love, get Fitnesse hooked into your .NET project for some Testing goodness.

Step 1 - Download and run Fitnesse

First, download the latest version of Fitnesse from http://www.fitnesse.org. The DotNet fitserver comes installed as part of the standard download.

Next, start the FIT server. Browse to it using ${browser.of.choice}. Create a page called MyFirstTest by appending MyFirstTest to your URL like: http://localhost:8080/MyFirstTest

Step 2 - Create a test page

Following the instructions from the DotNetFitServer page (http://www.fitnesse.org/FitNesse.DotNet.DotNetFitServer), edit your page and add the following lines:

!define COMMAND_PATTERN {%m %p}
!define TEST_RUNNER {dotnet\FitServer.exe}
!define PATH_SEPARATOR {;}


This hooks your wiki to the FitServer runner.

Now, add a test. We'll use the normal division test. Add the following lines to your wiki page:


|Division| 
|numerator|denominator|quotient?|
|10       |2          |5        |
|12.6     |3          |4.2      |
|100      |4          |24       |


Now save it. It should look something like:



You'll notice that you don't see a "Test" button on the left hand side menu. This is because you need to tell Fitnesse this is a test page. Click on Properties, check "Test" under actions, and click "Save Properties". You'll now see the Test link show up on the left hand side menu. Click it! You should now see something like:



Which is Ok, because we haven't defined an assembly. In fact, we haven't done anything yet with .NET.

Step 3 - Create the .NET Class

So, our next step is to create a .NET project to run our code. Open Visual Studio.NET, and create a new class library project in a location that doesn't have any spaces, like C:\FitnesseTutorial.



Once that is open, delete the Class1.cs file. Next, add a reference to the fit.dll which is in the dotnet directory of your fitnesse install location. For me, that is c:\fitnesse\dotnet\fit.dll. Now add a new class, Division, which inherits from fit.ColumnFixture. Notice that is the same name as the first row from the table we put in our wiki. Your class should look like:

public class Division : fit.ColumnFixture
{
  public double numerator = 0.0;
  public double denominator = 0.0;

  public double quotient()
  {
    return 1.0;
  }
}


Save and build that project.

Step 4 - Hook Fitnesse to the .NET Class

Now we have to get the wiki to "see" our class. This is accomplished with the !path line, so edit your wiki page and add the line:

!path C:\FitnesseTutorial\bin\Debug\FitnesseTutorial.dll

and save it. Now try running your test again. You should see something like:



Yay! Do a dance, or a jig. This means you have everything hooked up properly. If you are running into problems make sure:

  • You don't have a namespace

  • You've specified the full path to your output dll

  • Your class extended fit.ColumnFixture


Step 5 - Get to Green

Now that you are here, modify your class to let this test pass:

public class Division : fit.ColumnFixture
{
  public double numerator = 0.0;
  public double denominator = 0.0;

  public double quotient()
  {
    return numerator/denominator;
  }
}


Assuming everything has worked up to this point, you should now see the following:



And that's it! You've now succesfully modified a Fitnesse page to use the DotNet fit server and talk to a project you created. Now, go forth and get busy with those customer tests!

32 Comments:

  • Thank you for all your effort!

    I have a question about the Test table format. When I created the Division table in FitNesse, the first line (|Division|) was not rendered as part of the table. I tried a few things and found that entering a space between the 'goalpost' (|) and the word "Division" allowed it to be recognized as the 'Fixture' for that table (| Division|). Is this expected behavior?

    Also, could you number your steps? It would make it easier to refer to the related section.

    Thanks again, Matt

    By Anonymous Matt Scilipoti, at 10:46 PM  

  • Hi Matt,

    Check to make sure there isn't a space at the end of the pipes. That will cause it as well.

    Thanks for the info - I'll number the steps, and let me know if the above works for you.

    By Blogger Cory Foy, at 8:42 PM  

  • Everything worked fine when I created my .NET project using Visual Studio 2003, but when I converted the project to Visual Studio 2005, FitNesse could not find the Division class in the assemblies. Have you encountered or otherwise heard of this problem?

    Thanks

    By Anonymous Mike Krumlauf, at 2:03 PM  

  • I was able to test .NET 2.0-compiled classes after downloading the FitNesse source and compiling the dotnet portion in VS 2005 and referencing the new FitServer.exe. It might be helpful to pass this information along - thanks for the great tool!

    By Anonymous Mike Krumlauf, at 2:25 PM  

  • Not to make myself a pest, but I'm now trying to test a Visual Basic .NET 2.0 version of the Division example in your tutorial, and I get the error "System.ApplicationException: Type 'division' could not be found in assemblies." Is it possible to test VB .NET classes with FitNesse?

    Thanks again.

    By Anonymous Mike Krumlauf, at 2:49 PM  

  • I had the same problem as Matt, but realized there was a space after the first |. I removed it ant it worked.

    I do however now have the same problem mentioned by Mike. It says that it can't find the type "division". Well, I noticed there is a case difference, my class and the header of the table are both "Division" but it is looking for lower-case "division". Any idea why?

    I have release 20050731, which release did you use?

    Thanks, Chris

    By Anonymous Anonymous, at 10:02 AM  

  • Hi Mike,

    Sorry about the delayed response. I haven't played with 2005 yet, but I have done stuff with VB.NET, so I can't image it not working. You might want to try on the NUnit list to see what they have to say.

    By Blogger Cory Foy, at 1:27 AM  

  • Hi Chris,

    First, is your class declaring a namespace? That was a tricky part - if you're class is in a namespace you'll have to declare that as part of the name or import it.

    As far as lower vs upper, I don't think it should matter for .NET - I don't believe the classes are case sensitive for reflection.

    Does changing it to be lower-case division fix the problem?

    I'll give it a shot this weekend and see what's going on.

    By Blogger Cory Foy, at 1:30 AM  

  • Changing the Division to lowercase did not work either. I also tried including the namespace, and it didn't work. I get the following error:

    System.ApplicationException: Type 'fitnessetutorialdivision' could not be found in assemblies.
    Assemblies searched: file:///C:/FitnesseTutorial/bin/Debug/FitnesseTutorial.dll

    at fit.ObjectFactory.GetInstance(TypeName typeName, Assembly assembly, Type type)
    at fit.ObjectFactory.GetTypeOrInstance(TypeName typeName, GetTypeOrInstanceDelegate getTypeOrInstance)
    at fit.ObjectFactory.CreateInstance(String submittedName)
    at fit.Fixture.LoadFixture(String className)
    at fit.Fixture.LoadFixture(Parse theTable)
    at fit.Fixture.DoTables(Parse tables)

    By Anonymous Chris, at 4:37 PM  

  • Actually, Cory, it appears that having a namespace is what caused the problem. I can't get namespaces to work. When I removed the namespace, it started working.

    Thanks for the tutorial, it was a great help.

    -Chris

    By Anonymous Chris, at 4:43 PM  

  • Great tutorial - saved me a ton of time!

    Now if I could just get you to write a tutorial showing how to implement an ActionFixture using C#...

    Many Thanks!
    Tom H

    By Anonymous Anonymous, at 6:26 PM  

  • "Now if I could just get you to write a tutorial showing how to implement an ActionFixture using C#..."

    Consider it done

    By Blogger Cory Foy, at 11:01 PM  

  • Cory, thank for the tutorial for people, like me, who don't RTFM.

    I made my Fitnesse & FitLibrary for .Net2.0 following Jeff Mattfield's excellent instructions.
    See:
    http://jeffsbits.blogspot.com/2006/02/building-fitnesse-and-fitlibrary-for_10.html

    I was having trouble getting the VB.Net stuff to work, always getting the System.ApplicationException: Type 'division' message, so I had a look at the .dll I was trying to test using ILDASM. (Use Programs, VS 2005, VS Tools, VS Command Prompt, enter ILDASM)

    This shows that the name of the Type is actually the NamespaceName.Division .
    i.e. The class is declaring a namespace as Cory said. 1:30am

    (It seems that, although only one class is defined, the namespace name is placed there because the 'My' class is also present in all? VB builds. )

    Entering the full name
    i.e. namespacename.classname
    into the first row of the FIT table makes the test work.

    Don't forget to insert a ! before the table, or the, probably CamelCase'd, NameSpace name will think it's a new Wiki page.

    By Anonymous Paul Vlagsma, at 10:48 PM  

  • Thank you for the lovely tutorials (both this and the actionfixture). I was wondering if you could do own for column fixture - I haven't been able to get it to work properly, and I think that looking at someone else's code might help.

    By Anonymous MarFarMa, at 10:51 AM  

  • Hi there,

    I did what Paul has suggested .with or without namespaces ,I am getting the error that numerator cannot be found in DLL. So does it suggest me that Fitnesse is not working for Net 2.0 Framework.

    Does any one have any suggestions .

    Raja

    By Anonymous Anonymous, at 2:06 PM  

  • Never Mind ,

    It worked with the way Paul has suggested. Make sure that your class is public when you are getting not found Division error.

    Because VS by default will not create as public.

    One more thing if you used namespace in your class ,make sure that you refer that .

    For ex :
    !define COMMAND_PATTERN {%m %p}
    !define TEST_RUNNER {dotnet\FitServer.exe}
    !path dotnet\*.dll
    !path C:\projects\Foo\FitenesseContentStoreTests\bin\Release\DivisionTest.dll

    |Div.Division|
    |numerator|denominator|quotient?|
    |10 |2 |5 |
    |12.6 |3 |4.2 |
    |100 |4 |50 |


    and here is the class

    using fit;

    namespace Div
    {
    public class Division : fit.ColumnFixture
    {
    public double numerator = 0.0;
    public double denominator = 0.0;



    public double quotient()
    {
    return numerator / denominator;
    }
    }
    }


    Lastly if you are running on .Net 2.0 be sure to follow the below blog

    http://jeffsbits.blogspot.com/2006/02/building-fitnesse-and-fitlibrary-for_10.html

    Raja

    By Anonymous Anonymous, at 4:23 PM  

  • Thanks for this effort, saved us a lot of time

    By Blogger Sujit Raghavan, at 5:01 PM  

  • I have ported most of the FitBook examples to .NET 2005.

    see http://www.vlagsma.com/fitnesse

    The fixture & application code in J#, C#, VB.NET as VS 2005 projects.

    The Wiki pages are included. Each individual test page has links to the relevant code.

    By Anonymous Paul Vlagsma, at 9:23 PM  

  • Ok, I have successfully completed the Division sample using C# (2005) and it works (in the end it took just under 4 hours to figure out what was happening). I have tried to replicate the example using vb.net (2005) but I am getting a System.ApplicationException: Type 'MyFitnessTutorialVB.Division' could not be found in assemblies.'

    I have followed the same steps as in the C# example. Added my fit.dll, inherited from fit.ColumnFixture, kept my class, global variables and method public. Referenced my projects dll in the bin folder.

    Any suggestions on what the difference between C# and VB.NET examples are?

    Michael O'Donnell.
    (Ireland)

    (I only found this blog in the last 45 minutes, and it was about that time that things started to go right)

    By Anonymous Michael O'donnell, at 7:17 AM  

  • Hi Michael,

    First, can you try the example in VB without your class being in a Namespace? I've seen some problems with that before.

    Feel free to email me directly, or head over to the Fitnesse list (http://groups.yahoo.com/group/fitnesse) if that doesn't work.

    Thanks, and I'm glad it was helpful!

    Cory

    By Blogger Cory Foy, at 7:45 AM  

  • WARNING! The table code in Cory's post has a space at the end of the line

    |Division|

    which, if you copy and paste directly into your test page, will produce the error:

    System.ApplicationException: Type 'numer' could not be found in assemblies.
    Assemblies searched:
    maybe/some/path/to/some.dll
    file:///C:/code/Sandbox/FitnesseTutorial/FitnesseTutorial/bin/Debug/FitnesseTutorial.dll
    maybe/some/other/path/to/another.dll
    Load errors:
    at fit.ObjectFactory.GetInstance(TypeName typeName, Type type)
    at fit.ObjectFactory.CreateInstance(String submittedName)
    at fit.Fixture.LoadFixture(String className)
    at fit.Fixture.DoTables(Parse tables)

    This took me a few hours and finally tracing the source code to find and figure out.

    This is apparently another side effect of Division not being rendered/recognized as part of the table.

    By Anonymous Tim, at 7:59 PM  

  • Yep! If you cut and past, BE SURE TO DELETE THE SPACES AFTER THE PIPES. Otherwise, you get: "System.ApplicationException: Type 'numerator' could not be found in assemblies."

    Also, hilarious, but the third test row is incorrect. 100/4 is 25. But how cool to see that it fails and cause us to think, "is return numerator/denominator; incorrect?"

    By Anonymous rusty, at 11:12 AM  

  • I get this error for some input (string, integer) given to a fixture.

    System.IndexOutOfRangeException: Index was outside the bounds of the array.
    at fit.MethodAccessor.Set(Fixture fixture, Object value)
    at fitnesse.handlers.AbstractCellHandler.HandleInput(Fixture fixture, Parse cell, Accessor accessor)
    at fit.CellOperation.Input(Fixture fixture, String memberName, Parse cell)
    at fit.Binding.HandleCell(BoundFixture fixture, Parse cell, OperationType operationType)
    at fit.Binding.HandleCell(BoundFixture fixture, Parse cell)
    at fit.BoundFixture.DoCell(Parse cell, Int32 column)

    What is the solution for this problem if anyone has encountered this error?

    By Anonymous Anonymous, at 3:52 AM  

  • I was having a lot of trouble with "Class Division Not Found." I switched to Mono and SharpDevelop, compiled the exact same code as I was doing in Visual Studio, and the problem went away.

    By Blogger Jonathan Mark, at 2:40 PM  

  • Very helpful for someone just getting started with Fitnesse. Thanks!

    By Anonymous Anonymous, at 4:48 PM  

  • Cory,

    Thank you for my sanity!! I was beating my head against a wall just trying to get the Quotient example to run. (The Fitnesse documentation is a real piece!)

    I'm officially out of the starting gate with Fitnesse!

    By Anonymous Gnome, at 7:12 PM  

  • This is an excellent information on getting started on Fitnesse without doubt.
    Only think is to make sure if you are attempting the same project in VS 2005, you need to build the Fitness dlls to 2005.

    By Anonymous Anonymous, at 3:55 PM  

  • great tutorial finally got it to work with what i read here

    Few things worth noting though if your using .Net 2.0

    1)download the patched libraries for .net 2.0

    From here
    http://gojko.net/fitnesse/fdnpatch
    follow the instructions on the page
    OR
    head over to source forge to get the latest release when i checked it was 1.3
    find it here
    https://sourceforge.net/project/showfiles.php?group_id=167811&package_id=219790&release_id=516420


    2)make sure the class path to your application dll or exe you are testing has no spaces

    3)make sure when you reference the fit.dll from VS 2005 you set the copy local property to false

    there you done Fitnesse is now up and running

    By Blogger colwin, at 5:17 AM  

  • hmmm the whole Url wasn't displayed for the source forge link so here it is again

    https://sourceforge.net/project/showfiles.php?group_id=167811

    By Blogger colwin, at 5:21 AM  

  • I was able to get the .Net 2.0 namespaces to work by escaping the Namespace.Class name by enclosing them between !- and -! as below:

    |!-FitnesseTutorial.Division-!|

    Hope this helps. I couldn't get it to work any other way because Fitnesse kept putting a ? in front of the period like FitnesseTutorial?.Division

    By Anonymous mdemeglio, at 3:32 PM  

  • I have a NUNIT TEST FIXTURE, having some TEST methods into it, can anyone tell me how can i REUSE that without having it to rewrite ???

    By Blogger Sheikh Ather Ali, at 11:57 AM  

  • Another question is, how to create that pipe delimited text for complex objects, like for example i need to test a method which takes a BankAccount Object and which has several other objects encapsulated into that, How to create that pipe delimeted stuff for this case ???

    By Anonymous ARTHER, at 12:02 PM  

Post a Comment

Links to this post:

Create a Link

<< Home