Testing framework is not just for writing… tests

Quick question – from the top of your head, without running the code, what is the result of:

var foo = -00.053200000m; 
 
var result = foo.ToString("##.##");

Or a different one:

var foo = "foo"; 
var bar = "bar"; 
var foobar = "foo" + "bar"; 
var concaternated = new StringBuilder(foo).Append(bar).ToString(); 
 
var result1 = AreEqual(foobar, concaternated); 
var result2 = Equals(foobar, concaternated);
 
 
 
public static bool AreEqual(object one, object two) 
{ 
    return one == two; 
}

How about this one from NHibernate?

var parent = session.Get<Parent>(1); 
 
DoSomething(parent.Child.Id); 
 
var result = NHibernateUtil.IsInitialized(parent.Child);

The point being?

Well, if you can answer all of the above without running the code, we’re hiring. I don’t, and I suspect most people don’t either. That’s fine. Question is – what are you going to do about it? What do you do when some 3rd party library, or part of standard library exhibits unexpected behaviour? How do you go about learning if what you think should happen, is really what does happen?

Scratchpad

I’ve seen people open up Visual Studio, create ConsoleApplication38, write some code using the API in question including plenty of Console.WriteLine along the way (curse whoever decided Client Profile should be the default for Console applications, switch to full .NET profile) compile, run and discard the code. And then repeat the process with ConsoleApplication39 next time.

 

The solution I’m using feels a bit more lightweight, and has worked for me well over the years. It is very simple – I leverage my existing test framework and test runner. I create an empty test fixture called Scratchpad.

scratchpad

scratchpad_fixture

 

This class gets committed to the VCS repository. That way every member of the team gets their own scratchpad to play with and validate their theories, ideas and assumptions. However, as the name implies, this all is a one-off throwaway code. After all, you don’t really need to test the BCL. One would hope Microsoft already did a good job at that.

If you’re using git, you can easily tell it not to track changes to the file, by running the following command (after you commit the file):

git update-index --assume-unchanged Scratchpad.cs

scratchpad_git

With this simple set up you will have quick place to validate your assumptions (and answer questions about API behaviour) with little friction.

scratchpad_test

 

 

So there you have it, a new, useful technique in your toolbelt.


Posted 01-18-2012 2:06 PM by Krzysztof Koźmic

[Advertisement]

Comments

Michal Sakowicz wrote re: Testing framework is not just for writing… tests
on 01-18-2012 8:52 AM

You can use LinqPad for checking code snippets as well.  It's faster to start it than another instance of VS.

John Woodard wrote re: Testing framework is not just for writing… tests
on 01-18-2012 9:06 AM

Excellent article, except for one thing:  I would check them in, because later when someone questions whether an equation will always produce the correct results they can see the test showing that it does.  So you're not verifying that the behavior doesn't change, since that would be the road to disaster, but you're documenting how a no-s0-clear behavior actually behaves, because if you weren't sure about it, others won't be either.

Ray wrote re: Testing framework is not just for writing… tests
on 01-18-2012 10:12 AM

Like, Mikke, I generally use LinqPad for this sort of thing unless I'm testing a theory/assumption that requires a bunch of references and setup that my test project already has in place. Hell, I use it for that a ton more than I do for linq-querying.

Klaus Hebsgaard wrote re: Testing framework is not just for writing… tests
on 01-18-2012 12:57 PM

LinqPad is cool.

The mono framework has a csharp REPL, which is even easier.

Your capcha does by the way not work on an ipad...

dagda1@scotalt.net wrote re: Testing framework is not just for writing… tests
on 01-19-2012 8:27 AM

If C# or .NET had an interactive console then this would be unneccessary.  

I'm not sure if .NET 5.0 will have one of these.

Mike Paterson wrote re: Testing framework is not just for writing… tests
on 01-19-2012 9:43 AM

Big +1 for LinqPad.  

Elliott Edwards wrote re: Testing framework is not just for writing… tests
on 01-19-2012 2:08 PM

I certainly appreciate the concept of using unit tests as a quick write, compile, run loop and totally agree with that angle. If I can avoid launching the full app to test/verify then I will.

That said, LinqPad works very well for testing stuff you wouldn't care to check in to source control.

And on the topic of the Mono REPL stuff, check out Roslyn, Microsoft's new Compiler as a Service. It includes an interactive C# window in visual studio.

Nicholas Carey wrote re: Testing framework is not just for writing… tests
on 01-20-2012 3:09 PM

On occassion, I've used NUnit as an execution framework for batch jobs in an ETL environment, where we'd be FTP'ing a zip file with a lot of different files. The zip file would need to be unpacked and each different file or file type would need to be parsed, translated and loaded into our system. Worked great!

Test harnessess...they're not just for testing!

Carlos Augusto wrote re: Testing framework is not just for writing… tests
on 02-29-2012 8:06 PM

Hi,

I like the SnippetCompiler its free and fast.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)