Friday, September 18, 2009

Changing the Visual Studio start page feed to show news from Dotnetkicks

Open Visual Studio and navigate to Tools-Options-Environment-Startup

Change the start page news channel variable to

http://feeds.feedburner.com/dotnetkicks

There you go, Dotnetkicks in the Visual Studio start page

kick it on DotNetKicks.com

Wednesday, February 25, 2009

Using NUnit to play around with class libraries

I'm not going to tell you why or when you should use the Test Driven Development (TDD), but I am going to show you a short example of how to use the NUnit framework. Normally, the mantra of TDD[1] is to write a test, watch it fail and then write the code needed to get it working. I'm going to do it the other way around and start with a finished class library and write some tests for it. There's a valid reason for doing this. For instance, with Testdriven.NET[2], you could debug as you test and step through the code and discover where the tests fails. In this case, I write some short tests just to get acquaint with a new class library, perhaps later I'll write some assertions as I extend the functionality to whatever I need.

Recently I've started using Twitter, which got med to look at the Twitter API. Yeadda[3] is a good Twitter class library written in C#. Lets see how we can write some tests and experiment with the classes included. I am currently running Visual Studio 2005 with Testdriven.NET, but I guess you could run just about any version if Visual Studio with NUnit or Microsofts own unit testing integration.

In Visual Studio, I open the Yeadda solution. It includes one project: Yedda.Twitter. This project contains the classes for the Twitter API wrapper. I then add a class library project and call it TestYedda. It includes one class file: TestTwitter.cs. This is an overview of my solution:



I start by adding some includes to the TestTwitter class. We'll need a reference to the Yedda project, some XML classes and the NUnit framework.

using NUnit.Framework;
using Yedda;
using System.Xml;

Now it's time to add some methods to the TestTwitter class. We'll want at least one method to include some tests and we'll want events to fire before and after this test function. We also have to add some keywords in order to tell the NUnit framework that this is a test and to set the order of exceution. The most common keywords are:

[TestFixture]
Tells the NUnit framework that this class contains tests and adds a public default constructor if one is not preent.

[Test]
Tells the NUnit frameword that this function is a test.

[Setup]
Event that is run before each test.

[TearDown]
Event that is run after each test

We'll soon write some simple assertions. But first lets have a look at what my class looks like after adding these keywords. The order of excecution starts with the Start() function. In this function, you would typically don initializations of variables or setup or input some test data into a table in a database. Then the test is run ant then the teardown function. Typically, one would remove the testdata from the database or cleanup member variables in here. This is my setup:



Now we'll add some simple assertions. These are quite self explainatory and you should experiment a bit with the Assert class. Its methods are static and consists normally of a boolean expression or two comparable input values and then a string that is displayed to screen if the assertion fails. All of these assertions have simple names like Assert.That(expr), Assert.IsNotNull(expr), Assert.IsNull(expr).


In this case, I wouldn't bother to write assertions at all and rather use the NUnit Framework as a mean to run some code on the Yedda library. The TestDriven.NET framework lets you debug as you run the tests which is a really cool feature.

That's it, have a go, write some tests, do some coding and do some googling on "the tdd mantra" :D

kick it on DotNetKicks.com

Tuesday, February 10, 2009

Save as Indesign .inx (CS2)

Ok, a bit off topic, but here's some thoughts on file formats and monopoly organisations.

This week, we got Adobe CS4 Design Premium at work. We need this because designers send us Indesign documents made with Creative Suite 3 or 4 and we still use CS2. I took it for granted that Adobe incorporates backwards compatibility, but no. This is cut/pasted from Adobes web pages:

"To open the InDesign CS4 document in InDesign CS2, open the exported INX file in InDesign CS3, export to INX again, and then open the exported file in InDesign CS2. Make sure that all versions have been updated."

The past year, Microsoft have had to defend their file format policy, BUT, Microsoft Office IS compatible with older versions. You can even save to html and old formats back to Office 95. How Adobe gets away with their policy on this subject is not quite as apparent to me...

Tuesday, February 03, 2009

Friendly error messages

In some of my web applications, I need to explain to the user why the codebehind fails. That said, the user probably doesn't need to know all the technical details about your applications inner workings, but that is a different discussion.

What I normally do, is to throw an exception in the code and catch it in the Global.asax file. The error is then sent to the client as a block of xml which can be read by a JavaScript and then be showed to the user.

Take a look at the code in my Global.asax:

Tuesday, April 01, 2008

Luftguitar CMS released

The project

The Luftguitar CMS project is a great platform for small or medium sized web portals. It is made with simplicity in mind and at the same time it offers a great deal of flexibility when it comes to customization.

The Goal

The philosophy is that
- most users don't like to manage user roles and frontpage layout, they want a simple system that just works.
- most designers don't like javascript, they want simple html and css.
- most developers don't like hacking old code, they want to make new cool features fast.

http://sourceforge.net/projects/luftguitarcms

Sunday, October 07, 2007

Including Unit Testing

It struck me as a great idea to include NUnit test in the next release of my SQLite query browser. If the program crashes or misbehaves, users can run the tests, find out why and either report to me or fix it themselves. Being one person and writing this program as a hobby, I don't have the capacity of testing that many different platforms and setups myself.

Let me show you what I have in mind:


The user clicks help-> run tests from the toolstrip


NUnit starts


The NUnit licence says: "Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely (...)"

I've included the entire NUnit bin directory and my test library dll in the project root and these few lines of code on the toolstrip onclick event:

private void runTestsToolStripMenuItem_Click(object sender, EventArgs e)
{
Process nunit = new Process();

nunit.StartInfo.FileName = Application.StartupPath + "\\nunit\\nunit.exe";
nunit.StartInfo.Arguments = "SQLiteQHTest.dll /run";

nunit.Start();
}

kick it on DotNetKicks.com

Friday, August 10, 2007

Debugging C#

Visual Studio and the .NET platform have many advanced features for debugging apart from the traditional breakpoint-driven aproach.

Compiler directives
First off, you should know about compiler directives. With compiler directives, you can write code that runs under a specific configuration, for example code that only runs on the Debug configuration. In order to do this, simply capsulate your code like this:

#if debug

// Do stuff

#endif



Asserting conditions
The System.Diagnostics namespace contains some advanced features for evaluating your code. Using the method System.Diagnostics.Debug.Assert (bool condition, string errorMessage), you can evaluate a condition and, if necessary, spawn an error message with details and stack trace. Try the following:

#if debug

// Do stuff
System.Diagnostics.Debug.Assert (this == that, "Its different: " + that.toString());

#endif



Break using code
It is possible to break into debugging using code. This can be handy if you need to run code as a service or othervise can't debug the traditional way.

#if debug

// Do stuff
if(!System.Diagnostics.Debugger.IsAttached)
{
System.Diagnostics.Debugger.Launch();
}
System.Diagnostics.Debugger.Break();

#endif