Tag: Programming
TDD with Powershell, from the client perspective
by bhartsock on Mar.27, 2009, under Uncategorized
Is it possible? Yes, and it is actually relatively easy. The hardest part can be figuring out how to mock/stub out certain functionality so your testing small units as opposed to full acceptance testing. The line can get blurry, but I don’t really care. Testing is half the battle.
But to be clear, the focus of my testing is to test how a snap-in is working. It sounds like an integration test, and maybe it is, but there is no reason you couldn’t test any powershell script using some of the steps outlined here. Just be flexible and creative.
Start by creating a nice base test fixture.
public abstract class PowershellTestFixture { protected Runspace Runspace { get; set; } public void CreateRunspace() { Runspace = RunspaceFactory.CreateRunspace(CreateConfiguration()); Runspace.Open(); } public void CloseRunspace() { Runspace.Close(); Runspace.Dispose(); } private RunspaceConfiguration CreateConfiguration() { var config = RunspaceConfiguration.Create(); PSSnapInException warning; config.AddPSSnapIn("SomeCustomSnapIn", out warning); return config; } protected Collection<PSObject> ExecutePowershell(string script) { return Runspace.CreatePipeline(script).Invoke(); } }
In this base class, I just created some nice helper methods, and one method that samples how to add a snap-in to the runspace.
Now, just write a test, and add your assertions. And it will work!
[TestFixture] public class TestSomethingStupid : PowershellTestFixture { [SetUp] public void Setup() { CreateRunspace(); } [TearDown] public void TearDown() { CloseRunspace(); } [Test] public void GetServiceWhereNameIsMySql() { string cmd = @" get-service | where { $_.name -eq 'MySQL' } "; var result = ExecutePowershell(cmd) .Select(o => o.BaseObject) .Cast<System.ServiceProcess.ServiceController>() .First(); Assert.AreEqual(result.ServiceName, "MySQL"); } }
Obviously this test is stupid. But that’s not the point. In a very few lines, I have tested the output of some powershell. Pretty cool if you ask me, especially if your coding a snap in. If you aren’t automating the tests, how else can you verify it works except to recompile, install, open powershell, and type some code? Very slow and painful!
It is important to note, there are other ways to test snap-in’s. What I really wanted to see is how my cmdlet’s looked from the powershell perspective, not the C# perspective.
DateTime.ParseExact()
by bhartsock on Mar.13, 2009, under Uncategorized
Today I used this function for the first time. Somehow, I have never needed this before. It is super simple and very useful though. It is basically the inverse of DateTime.ToString(“yyyy-mm-dd”). It allows you to parse a custom date/time format.
So, for example a Pound proxy log has a date in the format 12/Mar/2009:05:00:36 -0400, which isn’t a standard format in .NET. Using ParseExact, parsing it was a piece of cake.
$provider = New-Object System.Globalization.CultureInfo "en-US" [DateTime]::ParseExact($line.date, "dd/MMM/yyyy:hh:mm:ss zz00", $provider)
What are your developers talking about?
by bhartsock on Feb.19, 2009, under Uncategorized
I found myself in the middle of an argument over the differences between static and global variables today. I started to give my input, and stopped dead in my tracks.
I don’t care what the answer is.
I love the fact that my developers enjoy programming enough to argue about the semantics of it. I love that they take software development so seriously. I love that they want to know the answer.
Facilitating an environment where developers can continually improve is super important to me. Good software developers are passionate about programming and are always learning. Great software developers are obsessive about programming and are learning too fast for me to be comfortable. If there isn’t constant communication about new programming principles and practices, something isn’t right.
What are your developers talking about?
Database integration testing, Interested?
by bhartsock on Feb.18, 2009, under Uncategorized
Testing database queries is super important, but not easy enough. It can be done, but usually you have to write custom test fixtures and spend too much time doing it. Every project needs to test database integration, so why isn’t there a library out there to make it easier? NDbUnit seems somewhat dead, and only works on a specific set of database providers. Even worse, it relies on a ton of XML setup, which I hate.
So I decided to play around with creating an NUnit addin that makes this a lot easier. Does the following code interest anyone?
[MySqlDbTestFixture("Data Source=localhost;User ID=root;"), RandomDatabaseName, ImportFromFile("schema.sql")] public class SomeTestFixture { [DbTest, ImportFromFile("BadCompanyDataInDb.sql")] public void SomethingWorksWithBadCompanyData(IDbConnection conn) { ... } }
I have most of it working, and want to see if there are any developers that might think of using it. Am I on the right track? Any thoughts or ideas for improvement?
(I told you I would have some code in a post this week)
Superbowl Weekend – Uncle Bob vs. Joel
by bhartsock on Jan.31, 2009, under Uncategorized

This is the weekend when titans collide. Where everything is put on the line. Where Uncle Bob challenges Joel to a post-off.
Today on Object Mentor, Uncle Bob railed Joel Spolsky and Jeff Atwood for their recent Stack Overflow Podcast.
Whose side am I on? Well, if it’s on SOLID principles, Agile, testing, or anything to do with code, Bob.