Archive for the ‘Tools’ Category

Testing framework is not just for writing… tests

Wednesday, January 18th, 2012

Quick ques­tion – from the top of your head, with­out run­ning the code, what is the result of:

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

Or a dif­fer­ent 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 with­out run­ning the code, we’re hir­ing. I don’t, and I sus­pect most peo­ple don’t either. That’s fine. Ques­tion is – what are you going to do about it? What do you do when some 3rd party library, or part of stan­dard library exhibits unex­pected behav­iour? How do you go about learn­ing if what you think should hap­pen, is really what does happen?

Scratch­pad

I’ve seen peo­ple open up Visual Stu­dio, cre­ate ConsoleApplication38, write some code using the API in ques­tion includ­ing plenty of Console.WriteLine along the way (curse who­ever decided Client Pro­file should be the default for Con­sole appli­ca­tions, switch to full .NET pro­file) com­pile, run and dis­card the code. And then repeat the process with ConsoleApplication39 next time.

 

The solu­tion I’m using feels a bit more light­weight, and has worked for me well over the years. It is very sim­ple – I lever­age my exist­ing test frame­work and test run­ner. I cre­ate an empty test fix­ture called Scratchpad.

scratchpad

scratchpad_fixture

This class gets com­mit­ted to the VCS repos­i­tory. That way every mem­ber of the team gets their own scratch­pad to play with and val­i­date their the­o­ries, ideas and assump­tions. How­ever, as the name implies, this all is a one-off throw­away 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 eas­ily tell it not to track changes to the file, by run­ning the fol­low­ing com­mand (after you com­mit the file):

git update-index –assume-unchanged Scratchpad.cs

scratchpad_git

With this sim­ple set up you will have quick place to val­i­date your assump­tions (and answer ques­tions about API behav­iour) with lit­tle friction.

scratchpad_test

So there you have it, a new, use­ful tech­nique in your toolbelt.

Approval testing – value for the money

Monday, January 16th, 2012

I am a believer in the value of test­ing. How­ever not all tests are equal, and actu­ally not all tests pro­vide value at all. Raise your hand if you’ve ever seen (unit) tests that tested every cor­ner case of triv­ial piece of code that’s used once in a blue moon in an obscure part of the sys­tem. Raise your other hand if that test code was not writ­ten by human but generated.

 

As with any type of code, test code is a lia­bil­ity. It takes time to write it, and then it takes even more time to read it and main­tain it. Con­sid­er­ing time is money, rather then blindly unit test­ing every­thing we need to con­stantly ask our­selves how do we get the best value for the money – what’s the best way to spend time writ­ing code, to write the least amount of it, to best cover the widest range of pos­si­ble fail­ures in the most main­tain­able fashion.

Notice we’re opti­mis­ing quite a few vari­ables here. We don’t want to blindly write plenty of code, we don’t want to write sloppy code, and we want the test code to prop­erly ful­fil its role as our safety net, alarm­ing us early when things are about to go belly up.

Test­ing conventions

What many peo­ple seem to find chal­leng­ing to test is con­ven­tions in their code. When all you have is a ham­mer (unit test­ing) it’s hard to hit a nail, that not only isn’t really a nail, but isn’t really explic­itly there to being with. To make mat­ters worse the com­piler is not going to help you really either. How would it know that Login­Con­troller not imple­ment­ing ICon­troller is a prob­lem? How would it know that the new depen­dency you intro­duced onto the con­troller is not reg­is­tered in your IoC con­tainer? How would it know that the pub­lic method on your NHiber­nate entity needs to be virtual?

 

In some cases the tool you’re using will pro­vide some level of val­i­da­tion itself. NHiber­nate knows the meth­ods ought to be vir­tual and will give you quite good excep­tion mes­sage when you set it up. You can ver­ify that quite eas­ily in a sim­ple test. Not every­thing is so black and white how­ever. One of diag­nos­tics pro­vided by Cas­tle Wind­sor is called “Poten­tially mis­con­fig­ured com­po­nents”. Notice the vague­ness of the first word. They might be mis­con­fig­ured, but not nec­es­sar­ily are – it all depends on how you’re using them and the tool itself can­not know that. How do you test that efficiently?

Enter approval testing

One pos­si­ble solu­tion to that, which we’ve been quite suc­cess­fully using on my cur­rent project is approval test­ing. The con­cept is very sim­ple. You write a test that runs pro­duc­ing an out­put. Then the out­put is reviewed by some­one, and assum­ing it’s cor­rect, it’s marked as approved and com­mit­ted to the VCS repos­i­tory. On sub­se­quent runs the out­put is gen­er­ated again, and com­pared against approved ver­sion. If they are dif­fer­ent the test fails, at which point some­one needs to review the change and either mark the new ver­sion as approved (when the change is legit­i­mate) or fix the code, if the change is a bug.

 

If the expla­na­tion above seems dry and abstract let’s go through an exam­ple. Wind­sor 3 intro­duced way to pro­gram­mat­i­cally access its diag­nos­tics. We can there­fore write a test look­ing through the poten­tially mis­con­fig­ured com­po­nents, so that we get noti­fied if some­thing on the list changes. I’ll be using Approval­Tests library for that.

[Test]
pub­lic void Approved_potentially_misconfigured_components()
{
    var con­tainer = new Wind­sor­Con­tainer();
    container.Install(FromAssembly.Containing<HomeController>());

    var han­dlers = GetPotentiallyMisconfiguredComponents(container);
    var mes­sage = new String­Builder();
    var inspec­tor = new DependencyInspector(message);
    fore­ach (IEx­poseDe­pen­den­cy­Info han­dler in han­dlers)
    {
        handler.ObtainDependencyDetails(inspector);
    }
    Approvals.Approve(message.ToString());
}

pri­vate sta­tic IHan­dler[] GetPotentiallyMisconfiguredComponents(WindsorContainer con­tainer)
{
    var host = container.Kernel.GetSubSystem(SubSystemConstants.DiagnosticsKey) as IDi­ag­nos­tic­sHost;
    var diag­nos­tic = host.GetDiagnostic<IPotentiallyMisconfiguredComponentsDiagnostic>();
    var han­dlers = diagnostic.Inspect();
    return han­dlers;
}

What’s impor­tant here is we’re set­ting up the con­tainer, get­ting the mis­con­fig­ured com­po­nents out of it, pro­duce read­able out­put from the list and pass­ing it down to the approval frame­work to do the rest of the job.

Now if you’ve set up the frame­work to pup-up a diff tool when the approval fails you will be greeted with some­thing like this:

approval_diff

You have all the power of your diff tool to inspect the change. In this case we have one new mis­con­fig­ured com­po­nent (Home­Con­troller) which has a new para­me­ter, appro­pri­ately named miss­ing­Pa­ra­me­ter that the con­tainer doesn’t know how to pro­vide to it. Now you either slap your­self in the fore­head and fix the issue, if that really is an issue, or approve that depen­dency, by copy­ing the diff chunk from the left pane to the right, approved pane. By doing the lat­ter you’re noti­fy­ing the test­ing frame­work and your team­mates that you do know what’s going on and you know it’s not an issue the way things are going to work. Cou­pled with a sen­si­ble com­mit mes­sage explain­ing why you chose to approve this dif­fer­ence you get a pretty good trail of excep­tion to the rule and rea­sons behind them.

 

That’s quite an ele­gant approach to a quite hard prob­lem. We’re using it for quite a few things, and it’s been giv­ing us really good value for lit­tle effort it took to write those tests, and main­tain them as we keep devel­op­ing the app, and the approved files change.

 

So there you have it, a new, use­ful tool in your toolbelt.

Connector: Simple, zero friction Github –> AppHarbor integration

Sunday, September 18th, 2011

Recently to play with some new tech­nol­ogy I came up with an idea to build an inte­gra­tion layer between Github and AppHar­bor. What that means, is give you abil­ity to work with your Github repos­i­tory, reap­ing ben­e­fits of all of it’s VCS-centric fea­tures, and auto­mat­i­cally, con­tin­u­ously deploy your code to AppHarbor.

The actual sce­nario I had in mind is to be able to use that for deploy­ment of Open Source projects. AppHar­bor is fan­tas­tic, no-headache deploy­ment in the cloud, but Github is per­fect for keep­ing and devel­op­ing your code in the open, in social way. To have the cake and eat it too, Con­nec­tor was born.

Connector

I hope you find it use­ful. it is free, use-at-your-own-risk-and-don’t-sue-me-if-something-breaks soft­ware. There’s still some work to be done, feature-wise and a whole lot of pol­ish­ing but I decided to announce it early and get early feed­back. If you have any sug­ges­tions, ideas or (gulp) bugs, let me know!

link: http://connector.apphb.com/

Hope that helps.

Simple guide to running Git server on Windows, in local network (kind of)

Saturday, August 20th, 2011

Last year I found myself in a sud­den and quick need to set up work­ing envi­ron­ment for a team of four, and as I like Git very much, I wanted to use it as our VCS. The prob­lem was, we weren’t allowed to use any third party provider, so GitHub was off the table. As I searched the Inter­net there were a few guides to set up team Git envi­ron­ment on Win­dows, but they all seemed very com­pli­cated and time con­sum­ing. For our mod­est needs we exper­i­mented a lit­tle and came up with a solu­tion that was very sim­ple, didn’t require any addi­tional soft­ware to be installed any­where and worked like a charm.

Recently I used it again on my cur­rent engage­ment, and one of my col­leagues sug­gested I should blog it, so here goes.

Ready, steady, go

The guide assumes you already have your local Git set up. For that, there are plenty of resources on the Inter­net, includ­ing my own blog­post about Win­dows Git tool­ing.

The entire tricks works like this – expose folder con­tain­ing your shared Git repos­i­tory as Win­dows net­work share.

Step one – bare git repository

There are two twists to the entire solu­tion – one of them is – your shared repos­i­tory needs to be ini­tial­ized with –bare flag.

git_bare_repository

Step two – Win­dows share

Sec­ond step is to expose the folder with our newly cre­ated repos­i­tory on the Win­dows share. You also use your stan­dard Win­dows mech­a­nisms to con­trol and limit access to the folder (make sure you give the devel­op­ers write access!).

Step three – Map the share as net­work drive

This step is per­haps not exactly nec­es­sary but I couldn’t get it to work oth­er­wise, so here comes the sec­ond twist. In order for your devel­op­ers to be able to access the shared folder via Git they need to map it as net­work drive.

sshot-10

Step four – Add remote repos­i­tory in Git and code away

Last step is the stan­dard Git pro­ce­dure – every devel­oper on your team needs to add the repos­i­tory sit­ting under their newly cre­ated net­work drive as remote. Notice the use of “file:///” pre­fix in front of the mapped drive name.

sshot-11

Step five

That’s all. I hope you find it use­ful, and if you know a way to elim­i­nate step three, let me know in the comments.

Tests and issue trackers – how to manage the integration

Tuesday, March 1st, 2011

In highly dis­ci­plined teams when a bug is dis­cov­ered the fol­low­ing happens:

  • test (or most likely a set of tests) is writ­ten that fails expos­ing the bug
  • a ticket in issue track­ing sys­tem is created
  • a devel­oper fixes the bug, runs all the tests includ­ing the new ones and if every­thing is green, the ticket is resolved

My ques­tion is what if some time later (say sev­eral weeks after) a devel­oper wants to find which issue relates to the tests he’s look­ing at, or which tests were doc­u­ment­ing the bug he’s look­ing at. How do you man­age the link between the tests and issues in your tracker?

Solu­tion one – file per ticket

Quite com­mon solu­tion to this prob­lem is to have a spe­cial folder in your test project and for each ticket have a file named after the ticket’s ID like this:

Tests solution

That works and is quite easy to dis­cover. How­ever the down­side of this solu­tion is that it intro­duces frag­men­ta­tion. If a bug was found in a ship­ping mod­ule does it really make sense to keep some tests for ship­ping mod­ule in Ship­ping­Mod­uleTests class and some other in CRM_4 class merely because the lat­ter ones were dis­cov­ered by a tester and not orig­i­nal developer?

Solu­tion two – ticket id in method name

To alle­vi­ate that another solu­tion is often used. The tests end up in Ship­ping­Mod­uleTests bug the id of the issue is encoded in the name of the test method. like this:

[Test]
public void CRM_4_Gold_preferred_customer_should_have_his_bonus_applied_to_net_price()
{
   //some logic here
}

[Test]
public void CRM_4_Silver_preferred_customer_should_have_his_bonus_applied_to_net_price()
{
   //some logic here
}

That’s a step in the right direc­tion. It makes the link explicit and you can quickly nav­i­gate the rela­tion either direc­tion. How­ever I don’t like it very much, because most of the time I couldn’t care less about the fact that this test doc­u­ments a long fixed bug. Yet I am con­stantly reminded about it every time I run my tests.

tests

Solu­tion three – description

The solu­tion I found myself using the most recently is to lever­age the descrip­tion most test­ing frame­works let you asso­ciate with your tests.

[Test(Description = "CRM_4")]
public void Gold_preferred_customer_should_have_his_bonus_applied_to_net_price()
{
   //some logic here
}

[Test(Description = "CRM_4")]
public void Silver_preferred_customer_should_have_his_bonus_applied_to_net_price()
{
   //some logic here
}

This still makes the asso­ci­a­tion explicit and search­able, but doesn’t remind me of it con­stantly where I don’t care.

tests

What about you? What approach do you employ to man­age that?

Git tooling for .NET developers

Wednesday, January 26th, 2011

Many devel­op­ers work­ing on Win­dows stay away from Git. There are many rea­sons for this but from my obser­va­tions and dis­cus­sions I’ve had, the most com­mon can be sum­ma­rized by this tweet by my friend Paul Stovell:

 

can everyone please stop using Git? Mercurial has a better UI (TortoiseHG), I'm sick of Git UI's

I’m not get­ting into holy wars, and I’m not try­ing to con­vince any­one that Git is bet­ter than any other VCS. Instead I’ll walk you through the tool­ing I use to inter­act with Git on Win­dows, with Visual Studio.

Git Exten­sions

First thing you should be get­ting is Git Exten­sions.

Git Extensions context menu

With that, sim­i­lar to Tor­toi­seX fam­ily of tools you get nice con­text menu that gives you access to most com­mon oper­a­tions quickly, via GUI, and with no need to mem­o­rize com­mand line options if you want to avoid it. You can also launch Git com­mand line in the selected folder and then all power of git is at your disposal.

Visual Stu­dio

If you’re a .NET devel­oper, you’ll want to work from within Visual Stu­dio. I’m sure you’ll be happy to learn that Git Exten­sions has a really nice Visual Stu­dio inte­gra­tion as well.

git_extensions_visual_studio

You get two things – a menu with all the options that Explorer con­text menu gives you and more, includ­ing abil­ity to edit .git­ig­nore file (also the tool will gen­er­ate a new .git­ig­nore file for you with Visual Stu­dio spe­cific rules!) and to launch a Git bash. Also you get a Git tool­bar with most com­monly used com­mands: Com­mit, browse, push, pull, stash and access to settings.

The way I usu­ally work with it, is I use Git bash for most oper­a­tions. There’s one excep­tion to that rule though – committing.

git_extensions_commit_window

I think Git Extensions’s Com­mit win­dow is the best of all VCS I’ve worked with. It clearly sep­a­rates files you want to com­mit (in your Index) and files you leave out for now. It clearly shows you sta­tus of each file (new, deleted, mod­i­fied) with dis­tinc­tive, large icons, also it shows you an on-the-fly diff of what changed in any given file, and it’s blaz­ing fast. Mostly the read­abil­ity ben­e­fits are why I stick to the UI for this operation.

Visual Stu­dio Git Source Con­trol Provider

In addi­tion to Git Exten­sions I use another tool called Git Source Con­trol Provider which plugs into stan­dard Visual Stu­dio VCS provider mech­a­nism to give me some addi­tional func­tion­al­ity (you can get the tool via Visual Stu­dio exten­sion manager).

git_source_control_provider

There are a few use­ful capa­bil­i­ties pro­vided by this tool that I tend to rely on quite often (there are more than that as you can read on the tool’s page):

  1. Over­lay icons show­ing you sta­tus of each file in Solu­tion Explorer.
  2. It shows you name of the cur­rent branch in the Solu­tion Explorer bar at the top (see “(mas­ter)” on the screen­shot below), and you will work a lot with branches in Git.
  3. It gives some addi­tional options in the con­text menu.

git_source_control_provider_screenshot

This (plus com­mand line) makes the job very, very sim­ple and quick, and that’s what I stick to on my machine.

There’s one more thing that makes work­ing with Git a plea­sure (espe­cially if you’re work­ing on a team that’s not com­pletely co-located).

Github

I love Github. It has a very clean, sim­ple inter­face that makes going through project his­tory, diff­ing com­mits and code reviews a very sim­ple and fric­tion­less process.

 

Sum­mary

Yes, per­haps those tools lack some eye candy that other tools have but frankly – I don’t care, and nei­ther should you. They are more than enough to let you quickly do what­ever you need to do with your code and don’t stand in your way. And that’s what a good VCS and tool­ing around it should be – some­thing you don’t really have to think about and you can rely on to keep track of what is hap­pen­ing with your code with con­fi­dence. And that’s pre­cisely what Git is – so if you’ve been held back, go ahead, install those tools and give Git a shot – you won’t look back.

My Visual Studio (with ReSharper) color settings

Monday, September 6th, 2010

That’s a post I’ve been mean­ing to write for quite a long time, yet I never got around to really doing it. Now, here it is – my Visual Stu­dio color theme and it looks like this:

vs_scheme

The theme works with Visual Stu­dio 2010 and requires Resharper 5.1 or newer with “Color Iden­ti­fiers” option turned on.

Some things to notice.

Instance classes, sta­tic classes and inter­faces have all very sim­i­lar but slightly dif­fer­ent shade of yellow/green.

Exten­sion meth­ods are slightly lighter than reg­u­lar methods.

Muta­ble locals, immutable locals and con­stants have also sim­i­lar but slightly dif­fer­ent color.

The back­ground is dark, but not com­pletely black.

 

Hope you enjoy it. If you do – you can grab it here.

.NET OSS dependency hell

Thursday, February 25th, 2010

Paul, whom some of you may know as the main­tainer of Horn project, left a com­ment on my blog, that was (or to be more pre­cise – I think it was) a con­tin­u­a­tion of series of his tweets about his dis­sat­is­fac­tion with the state of affairs when it comes to depen­den­cies between var­i­ous OSS projects in .NET space, and within Cas­tle Project in particular.

paul_twitter

I must say I under­stand Paul, and he’s got some valid points there, so let’s see what can be done about it.

Prob­lems

One of the goals of Cas­tle Project from the very begin­ning has been mod­u­lar­ity of its ele­ments. As cas­tle main page says:

Offer­ing a set of tools (work­ing together or inde­pen­dently) and inte­gra­tion with oth­ers open source projects, Cas­tle helps you get more done with less code and in less time.

How do you achieve mod­u­lar­ity. Say you have two projects, Foo and Bar that you want to inte­grate. You could just ref­er­ence one from the other.

eb1c2cc[1]

This how­ever means that when­ever you use Foo, you have to drag Bar with you. For exam­ple, when­ever you want to use Mono­Rail, you’d need to drag ActiveRe­cord with it, along with entire set of its depen­den­cies, and their depen­den­cies, etc.

Instead you employ Depen­dency Inver­sion (do not con­fuse with Depen­dency Injec­tion). You make your com­po­nents depend on abstrac­tions, not the imple­men­ta­tion. This how­ever means, that in .NET assem­bly model, you need to intro­duce third assem­bly to keep the abstrac­tions in.

51067fb6[1]

Now we have 3 assem­blies instead of 2 to inte­grate two projects. Within Cas­tle itself com­mon abstrac­tions are being kept in Castle.Core.dll. But what if we want to take more direct advan­tage of one project in another project still main­tain­ing the decou­pled struc­ture? We need to extract the func­tion­al­ity bridg­ing the two projects to yet another assem­bly. Tick – now we have 4 of them.

607f68d4[1]

In this case the Foo­Bar project would be some­thing like ActiveRe­cord inte­gra­tion facil­ity, which inte­grates ActiveRe­cord with Wind­sor.

When you mix mul­ti­ple projects together you enter another prob­lem – versioning.

Say you want to inte­grate few projects together, some of which are inter­de­pen­dent (via bridg­ing assem­blies, not shown here for brevity)

69f20a13[1]

Now, once a new ver­sion of one of the projects is released, you either have to wait for all the other projects to update their depen­dency to the lat­est ver­sion, do it your­self (pos­si­bly with some help from Horn), or stick to the old ver­sion. The sit­u­a­tion gets even more com­pli­cated when there were some break­ing changes intro­duced, in which case plain recom­pi­la­tion will not do – some actual code would need to be writ­ten to com­pen­sate for that.

These are the main issues with this model, let’s now look at pos­si­ble solutions.

Solu­tions

First thing that comes to mind – if hav­ing some assem­blies means you’ll need even more assem­blies, per­haps you should try to min­i­mize that num­ber? This has already come to our minds. With last wave of releases we per­formed some inte­gra­tion of projects. EmailSender got inte­grated into Core, one less assem­bly. Log­ging adapters for log4net and nlog were merged into core project, which means they still are sep­a­rate assem­blies (as they bridge Cas­tle with 3rd party projects) but they’re now synced with Core and are released with it, which means this is one less ele­ment in your ver­sion­ing matrix for you to worry about. Sim­i­lar thing hap­pened with Log­ging Facil­ity, which now is ver­sioned and released with Wind­sor itself.

For the next major ver­sion, there are sug­ges­tions to take this one step fur­ther. Merge Dynam­icProxy with Dic­tio­naryAdapter and (parts of) Core into a sin­gle assem­bly; Merge Wind­sor and Micro­Ker­nel (and other parts of Core) into an other assem­bly. With that you get from 5 assem­blies to 2.

That reduces Castle’s inter­nal depen­den­cies, but what about other projects that depend on it? After the recent release, we started a log of break­ing changes, along with brief expla­na­tion and sug­gested upgrade paths, to make it eas­ier for appli­ca­tions and frame­works to upgrade. We have yet to see how this plays out.

What else can be done?

This is the actual ques­tion to you? What do you think can be done, for Cas­tle specif­i­cally, but more broadly – for entire .NET OSS ecosys­tems to make prob­lems Paul men­tioned go away, or at least make them eas­ier to sort out?

ReSharper’s “hidden” gem – live templates.

Thursday, January 14th, 2010

I’m sure many of you, even those who use ReSharper on a daily basis, almost never use one of its most pow­er­ful fea­tures – live templates.

I just love how with sin­gle short­cut (ctrl + alt + insert) I can go to

reshareper_templates_step_one

enter, I go to

reshareper_templates_step_two

Type class name, enter

reshareper_templates_step_two

and I have just saved roughly 30s as com­pared to bare Visual Stu­dio, but most impor­tantly, I didn’t loose my momen­tum and focus. Sure it’s a small thing, but that’s how you build a palace – brick by brick.

Disabling Visual Studio F1 Help shortcut

Monday, July 27th, 2009

If you, like Mario (or me today), ever acci­den­tally hit F1 in Visual Stu­dio, and spent next cou­ple of min­utes look­ing at sim­i­lar screen, I have a remedy.

image[1]

How often do you really use F1 to access Visual Stu­dio help any­way? Wouldn’t it be good to just dis­able this short­cut? Well, as I learned today – you can do this.

Disable_F1

Go to Tools –> Options… nav­i­gate to the option above, find the com­mand Help.F1Help and click Remove, et voilà!

Happy cod­ing.