Using LinqPad to replace my "test" console applications

I've used LinqPad since... well forever :) I've had the free version since the early days and then paid for the full thing when I started out on my own a few years ago. At the beginning I was mainly using it for quick testing DB queries and quick idea jottings without thinking about big architectures etc, basically a scratchpad, and it's always worked well for me. Even more so after I started using it to connect to Azure using the Azure Table Storage driver.

Another thing I have always done, even when I have lots of unit tests, is to often have a console app in most of my API projects. Be this WebApi or older ones using MVC or WCF. These exist to test the client libraries for the API's that I have written which integration easier. These console apps mainly have a few methods that allow me to easily and quickly try out my service api's. Be this querying, uploading / downloading files etc.

These console apps aren't instead of unit tests or integration tests but allow me a quick convenient way to test development / stage / live api end points without firing up VS or having to worry about what credentials I need etc.

Today I almost fell into "lets add a console app into a new project" but I had a brainwave. Why not use LinqPad to write these "apps" and then store them in my solution with source control etc? Thus avoiding unnecessary additional projects, compile times etc. LinqPad allows you to save queries as .linq files and if I create several can be opened by anyone with LinqPad and be ran within seconds. No open project, compile, run etc.

The process is dead simple. First I opened LinqPad and created a new query. I then switched the language from C# Expression to either C# Statements or C# Program. I generally find C# Statements is enough but if I'm trying a few thing out programs are sometimes easier to manage.
1 -Change Language Type

2 - Finding Query Properties
I then needed to reference my "client" libraries. It seems no matter how many times I do this after a week off, or sometimes less; I blame lack of sleep / caffeine depending on the day, I always forget how to do this. It's actually DEAD simple. Simply bring up query properties by either pressing F4, bringing up the context menu within the query editor or by choosing query in the menu and choosing the bottom option.

Once this is loaded you can easily add references by either finding the assemblies on disk or even via nuget directly which is very cool. Once the assemblies are referenced click the additional namespace tab and pull in the namespaces you'll want to use in your script. You can do this later but it often saves time to do this once the window is open.
3 - Choose References, even with Nuget

4 - Write the test script and run
With the query properties now set you can then enter your scripts for testing. You can even async/await which is handy as most things now I write are async.

Finally just save the query into your solution and you can open it when ever you need too.

And that's it, So far I find this quick and easy and it will be great to come back at a later date and know replaying things against stage / live will be one click away.

As a bonus point, whilst writing this post I thought what about using HttpClient to try out new third party client rest api's or client libraries. Simply really quick iterations before writing a concrete implementation with the full test suite etc. Something I will explore properly at a later date.

For today this works fantastic for me but I have thought it might be worth looking into ScriptCS as an alternative however I'm not sure if I prefer having the LinqPad IDE with autocompletion etc. I'll to need to investigate but that is for another day.

Hope this helps.


  1. You should switch to F# and use the REPL! Highlight code in VS; Alt+Enter and it's copied to REPL and executed :)

  2. That is a good point, I've heard a few people say the same. Alas it just has an initial barrier to entry. Effectively the entire team would need to know F# just to write / run these scripts.

    Probably a nice way to learn F# though and something I will investigate further.

  3. Note that you can execute LINQPad scripts from the command line using the bundled lprun.exe file, which means you can create automation scripts and simple command line utilities quite easily. Personally I create build scripts using LINQPad.


Post a Comment

Popular posts from this blog

WebUSB - An unexpected update...

Can you use BuildRoot with Windows Subsystem for Linux......

DotNet CLI , private NuGet feeds and Linux...