Today I Learned How to Create Custom NUnit Constraints – Part 1: Creating the Constraint

NUnit has has built in constraints for most the tests you will need to write so there is no need to create your own.  End of blog post.

OK, that was a bad joke.  The first step in creating a NUnit custom constraint is to read the documentation.  That’s it.  End of blog post.

Not as funny as the first time?  Not funny at all?  I won’t use that joke again.  But you did read the documentation right?  OK, good.

The example for this blog post is from the NConstraint project.  It shows how I created a custom constraint to compare the property values or two objects.  In my case I wanted to write something like:

var expected = new MyClass() { SomeProperty = 1};

// Property values match.
var actual = new MyClass() { SomeProperty = 1 };
Assert.That(expected, Is.EquivalentPropertyWiseTo(actual));

// Property values don't match.
actual.SomeProperty = 2
Assert.That(expected, Is.Not.EquivalentPropertyWiseTo(actual));

Lets start.  First you need to override the Constraint class.

public class EquivalentPropertyWiseToConstraint : Constraint

Then create a constructor that accepts the expected value for the test.  In our case it’s the object you want to compare but it could be anything.

public EquivalentPropertyWiseToConstraint(object expected)
  Expected = expected;

public object Expected { get; }

Notice that we save the expected object.  In my case I saved it to a public property for unit testing purposes but it could be a private variable.  I know, unit testing a new unit test constraint, very meta.

The next thing we have to implement is the logic for the constraint by overriding the ApplyTo method.

public override ConstraintResult ApplyTo<TActual>(TActual actual)

This method takes a the actual value from the test that you want to compare to the expected.  It returns a ConstraintResult contains a reference to the constraint, the actual value, and if the constraint passed or not.

Add whatever logic you need to to the ApplyTo method.  In my case I wrote some logic that loops through all the properties of both objects and compares the values.  It’s a bit long so I won’t include it in this blog post.  In general your method will look something like:

public override ConstraintResult ApplyTo<TActual>(TActual actual)
  // You code to do the comparison.

  // If the comparison succeeds.
  return new ConstraintResult(this, actual, true);

  // If the comparison fails.
  return new ConstraintResult(this, actual, false);

Now you should be able to run your tests by instanciating your constraint or using the Matches syntax.

// Instantiate the constraint.
Assert.That(expected, new EquivalentPropertyWiseToConstraint(actual));

// Matches syntax.
Assert.That(expected, Is.Not.Matches(new EquivalentPropertyWiseToConstraint(actual)));

One thing you might notice is the error message is not very descriptive if the test fails.

But was: 1

The “But was” part is the actual value you passed into the constraint.  In my case I passed in the property value, which was an integer, hence the 1.  Your actual value might be different.

To get a better expected message you need to set the description value in the Constraint class.  If you have a simple test you could hard code the description.  For example the built in TrueConstraint always sets the description to “True” in the constructor.

public TrueConstraint()
  Description = "True";

In my case I needed to set the description in the ApplyTo method.  For example if a property does not exist then we set a different description then if the property values don’t match.

// Property does not exist message.
Description = $"expected property {expectedProperty.Name} does not exist.";

// Property values don't match message
Description = $"property {expectedProperty.Name} value to be {expectedValue}";

Once I set the descriptions my test failed messages looked better:

Expected: property IntegerProperty value to be 2
But was: 1

You can find the entire EquivalentPropertyWiseToConstraint class and example test client at the NConstraint project.

Now your custom constraint is complete but you might notice is that you can’t use the NUnit built in “Is” syntax.  For example, you currently can’t write:

Assert.That(expected, Is.EquivalentPropertyWiseTo(actual));

I’ll cover how to do this in a future post.  End of blog post.  No joke, this is really the end.  Unless you count the P.S. video.


P.S. – R.I.P. Gord Downie.  I had trouble picking my favourite Tragically Hip song but I think Bobcaygeon will do.  Read more about the lyrics and their meaning here.

So I’m at your house this morning
Just a little after nine
‘Cause it was in Bobcaygeon, where I saw the constellations
Reveal themselves one star at a time

Posted in Code Examples, Software Development, Today I Learned | Tagged , , | Comments Off on Today I Learned How to Create Custom NUnit Constraints – Part 1: Creating the Constraint

NConstraints Version 1.0.0 Released

A common unit test assert I need to do is compare all the property values of one object to another.  For example:

public void TestSaveWorks()
  // Create a value to save.
  var expectedDto = new SomeDto() {ValueOne = 1, ValueTwo = "Blah"};

  // Save the value.

  // Load the value from the database.  The DTO ID
  // should have been set by the save.
  var actualDto = _someDbService.Find(expectedDto.Id);
  Assert.That(expectedDto.ValueOne, Is.EqualTo(actualDto.ValueOne));
  Assert.That(expectedDto.ValueTwo, Is.EqualTo(actualDto.ValueTwo));

In this test we only have two properties to test but if the object has 10 plus properties to test that would be quite onerous.   I often forget to update the tests after I add a new property to the object.

To fix this problem I created a custom constraint for NUnit called EquivalentProperyWiseTo that compares all the property values of two objects.   Now the above tests looks like:

public void TestSaveWorks()
 // Create a value to save.
 var expectedDto = new SomeDto() {ValueOne = 1, ValueTwo = "Blah"};

 // Save the value.

 // Load the value from the database. The DTO ID
 // should have been set by the save.
 var actualDto = _someDbService.Find(expectedDto.Id);
 Assert.That(expectedDto, Is.EquivalentPropertyWiseTo(actualDto));

Notice you don’t need to test each property value independently.  Just pass in the two objects you want to test and you are done.  High Five!

The EquivalentPropertyWiseTo custom constraint can be found in the just released NConstraints.  You can find the latest stable version of the NConstraints on NuGet.  If you like living on the edge you can find the developer builds on MyGet.

If you have any questions, spot an bug, or have an enchantment for NConstraints let me know by:

  • opening an issue or pull request GitHub.
  • asking a question on StackOverlow with the tag nconstraints.
  • send me an e-mail to

Finally thank you to the NUnit team for creating NUnit and continue to support it.  I’ve used NUnit for most of my career to properly test and improve my code.

Posted in Code Examples | Tagged , | Comments Off on NConstraints Version 1.0.0 Released

Today I Learned How to Run NUnit Tests for a .NET Core Project in TeamCity

In TeamCity you can’t use the usual NUnit Runner to run .NET Core unit tests.  At least I couldn’t get it to work.  I’m sure this will be fixed in the future but for now the below works for me.  I got lots of inspiration from this TeamCity blog post.

For this example I’ll use the NConstraints project.  It contains a .NET Standard project (SaturdayMP.Constraints) that we want to test and two .NET Core Test projects (SaturdayMP.Constratints.Test and TestClient).

NConstraint Solution Explorer

In TeamCity I created a project with the usual first two steps of NuGet Install and Compile.

TeamCity Project Before Adding Test Step

If you read the TeamCity blog post, the one I got my inspiration from, then you probably noticed it used the .NET Core Runner to compile the project.  You might have had to do this when the TeamCity blog post was first written but now using the Visual Studio Runner will compile .NET Core projects.

Before we can add the .NET Core unit test build step we need to install the TeamCity .NET CLI Plugin.  Do this by downloading the plugin zip file from here.

TeamCity .Net Plugin Download

Then install the plugin by clicking on Administration in the top right then Plugins List on the bottom left.

TeamCity Navigate To Plugins List

Then click Upload Plugin Zip link and choose the plugin zip file you just downloaded.

TeamCity Upload Plugin Zip


TeamCity Upload .Net Core Plugin

Once the upload is complete you should see something similar to the screen shot below.

TeamCity Upload Successful

The plugin is uploaded but before it’s active you need to reboot TeamCity.  In my case I just rebooted the server.

Once the reboot is complete you can check that the plugin installed correctly by going to an agent with Visual Studio 2017 installed.

TeamCity Navigate To Agent Parameters

Once on the agent navigate to the parameters page and you should see something similar to the below screen shot.

TeamCity Check .Net CLI Plugin Installed

If you don’t see the DotNetCli listed then you either forgot to reboot TeamCity, the plugin didn’t install correctly, or you don’t have .NET Core installed on you build agent.

Now that the plugin is installed we can go back to our project and add the unit test build step.  Add a new build step and in the runner type pick .NET CLI (dotnet).  Then in the command drop down pick test and finally enter the paths to your projects.  If you have several projects you can use wildcards.

TeamCity .Net CLI Build Step

If you don’t see all the options in the screen shot above them make sure to click Show Advanced Options.  In my case I also enabled code coverage at the bottom of the list.

TeamCity .Net CLI Build Step Enable Code Coverage


Now when we build the tests should get executed.  You can see the results of the tests in the build log or the tests tab of the build results.

TeamCity Test Results

As I said at the beginning I’m sure the NUnit Runner will be updated to handle .NET Core but use the above for now.

P.S. – R.I.P. Tom Petty.

Posted in Code Examples, Software Development, Today I Learned | Tagged , , , | Comments Off on Today I Learned How to Run NUnit Tests for a .NET Core Project in TeamCity

Odds Are you Work for a Small Business

If I was a betting man I would bet you work for a small company (99employees or less).  Odds are I would be right half the time.  How did I come up with those numbers?  Using some math and stats I found on the Internet.

About 24% of the working population are public employees (i.e. employed by government).  The remaining 76% are employed by private companies with just a hair under 70% working for a company of less then 100 employees.

Share Of Total Private Employment by Size of Business - 2015

Share of Total Private Employment by Size of Business, 2015

That means if 100 employed people are reading this blog post at the same time then:

  • 24 are Public Employees
  • 7 work at Large Companies (500+)
  • 15 work at Medium Companies (100 – 499)
  • 54 work at Small Companies (1-99)

Pro tip for job seekers: don’t just apply to large companies.  Apply to small companies you never heard of because they hire a lot more people.


Posted in Business Side | Tagged | Comments Off on Odds Are you Work for a Small Business

Importance of Spouses in Small Businesses

Starting a new business is hard.  In 2010 study just over half of new business in Canada survive 5 years or more and only 30% survive 10 years.  Similar statics for our neighbours in the south.

Canada's Performance from 2001 to 2006 on Key Indicators of Entrepreneurial Performance

Saturday Morning Productions has been around for 7 1/2 years, if my maths are correct.  Our odds of surviving this long are about 45%.  Another way to look at it is if 100 business opened in 2009 then only 45 are still around today.

Why has Saturday MP survived when others business have faltered?  I think an important but often overlooked reason is my supportive spouse and business partner, Ada.  I know there are other reasons (i.e. hard work, grit, frugality, luck, etc) but when you start a business later in life when you are married with a young daughter then a supportive spouse is a requirement.  Well, I guess you could start a new business without the support of your spouse but I wanted to stay married and run a business.

I couldn’t find any data but my guess is that most small business that survive for more then 5 years have a supportive spouse.  That or the owners are unmarried or divorced.  The closest data I could find was from Thomas Stanley’s Millionaire Mind book and quoted on his blog:

The typical millionaire couple has been together for nearly thirty years and their bond tends to be permanent as well as economically productive. . . . ask the husband or wife to explain their household’s productivity . . . . Each gives substantial credit to the other.

For every 100 millionaires who say that having a supportive spouse was not important in explaining their economic success, there are 1,317 who indicate their spouse was important. Of the 100 who did not give credit to their spouse, 22 were never married and 23 were either divorced or separated. That leaves only 55 in 1,317 [4.2%] who believed that their spouse did not play an important role in their economic success.

So before I thank my wife for her support I would like to thank the spouses of all the small business and hope you will join me.  Think of all the small business you have either worked for, I’ve worked for 5 and counting, or done business with.

Thank you to the spouse that looked after the home and kids while the small business owner was working 60+ hours a week.  The spouse that was willing to give up the security of regular pay checks, benefits, and pension for someone else’s dream.  The spouse that became the companies first book keeper, janitor, and/or secretary.  The spouse that worked overtime or a 2nd job to help fund the new business.  The spouse that became a partner in the business.    The spouse that believed in their wife/husband and loved them through all the ups and downs.

AdaSpecial thanks to my wife for helping me start Saturday Morning Productions.  Thank you for being a business partner and a great consultant.  Thank you for looking after our home and daughter when you weren’t consulting.  Thank you for continuing to the book keeping while working a full time job at the university.  Thank you for supporting me during my summer sabbatical.  Finally thank you for continuing to support and believe in me.  Love you lots.

P.S. – Happy Anniversary.

I’m falling even more in love with you
Letting go of all I’ve held on to
I’m standing here until you make me move
I’m hanging by a moment here with you



Posted in Business Side | Tagged , , | Comments Off on Importance of Spouses in Small Businesses

My Takeaway from Reading Meatball Sundae

Meatball Sundae CoverBook: Meatball Sundae: Is Your Marketing out of Sync?
Author: Seth Godin

My takeaway from Meatball Sundae is don’t use a new marketing trend because it’s the hip new thing to do.  Make sure the marketing you do aligns with your business.  If the new marketing you does not align either change your business or don’t bother with the new marketing.

The book was written in 2007 and focuses mostly on marketing using the web but the basic idea applies to any new marketing technology.  Just because others companies use dream ads doesn’t mean you need too.

For me personally it means I should understand the social media platforms I use.  My Twitter, Facebook, and LinkedIn post mostly links back to this blog.  Is that the best way to use these platforms?  Probably not.

For October I’ll focus on Twitter and see if I can figure out if it aligns with my basement based business.  Do I need to change my business, change how I use Twitter, or give up on Twitter?


Posted in Takeaways | Tagged , , | Comments Off on My Takeaway from Reading Meatball Sundae

Today I Learned How to Lock a Dependency to a Specific Version in Project JSON

While upgrading the NUnitXmarin project the project maintainers asked me lock the version of NUnit to a specific version.  They where also kind enough to provided a link to a possible solution.  Lets test if it works.

First lets review how NuGet resolves dependencies.  If you specify a dependency version the standard way, i.e. just listing the version number with not brackets, NuGet tries to find the version you specified of the next highest version.

For example the NuGet feed for NUnit has the following versions in it’s feed:


If you list version 3.7.1 in project.json file, as shown below, then NuGet uses version 3.7.1.  Duh.

"dependencies": {
  "NUnit": "3.7.1"

Now say you list version 3.7.99 as shown below.  Now 3.7.99 does not exist so NuGet find the next highest version, which is 3.8.0, and uses that version.

"dependencies": {
 "NUnit": "3.7.99"

When you compile you will see a message similar to the below.

Dependency specified was NUnit (&amp;amp;gt;= 3.7.99) but ended up with NUnit 3.8.0.

NuGet Dependency Resolution Message

To tell NuGet to only use the version you specified you need to put square brackets around the version number.  This changes the logic from “>= Version#” to “= Version#”.

"dependencies": {
 "NUnit": "[3.7.99]" 

Now when you try to build you will get an error because version 3.7.99 does not exist and NuGet will not try to find a newer version.

NuGet Package restore failed for project Runner\nunit.runner.Droid for 'NUnit (= 3.7.99)'.

NuGet Dependency Error Message

That’s great you say, but why would you link to a version that does not exist?  That is stupid.

Let’s try something that might actually happen.  Your project references a project but another package links to a different version.  Say you have the following in your JSON file:

"dependencies": {
  "NUnit": "[3.6.1]",
  "NUnitLite": "3.8.1" 

NUnitLite 3.8.1 references to NUnit 3.8.1.  If you compile you get the following warning:

Detected package downgrade: NUnit from 3.8.1 to 3.6.1&amp;amp;nbsp;&amp;amp;nbsp;
ClassLibrary1 (&amp;amp;gt;= 1.0.0) -&amp;amp;gt; NUnitLite (&amp;amp;gt;= 3.8.1) -&amp;amp;gt; NUnit (= 3.8.1)&amp;amp;nbsp;&amp;amp;nbsp;
ClassLibrary1 (&amp;amp;gt;= 1.0.0) -&amp;amp;gt; NUnit (= 3.6.1)

NuGet Dependency Warning Message

That’s good.  It’s what we wanted.  Now what happens if we remove the square brackets from the NUnit dependency so our JSON file looks like:

"dependencies": {
 "NUnit": "3.6.1",
 "NUnitLite": "3.8.1" 

Will it use NUnit 3.6.1 that we reference in the project or NUnit 3.8.1 referenced by NUnitLite?

Detected package downgrade: NUnit from 3.8.1 to 3.6.1&amp;amp;amp;nbsp;&amp;amp;amp;nbsp;
ClassLibrary1 (&amp;amp;amp;gt;= 1.0.0) -&amp;amp;amp;gt; NUnitLite (&amp;amp;amp;gt;= 3.8.1) -&amp;amp;amp;gt; NUnit (= 3.8.1)&amp;amp;amp;nbsp;&amp;amp;amp;nbsp;
ClassLibrary1 (&amp;amp;amp;gt;= 1.0.0) -&amp;amp;amp;gt; NUnit (= 3.6.1)

NuGet Dependency Warning Message

Wait, that is exactly the same message we got last time.  What is going on?  It turns NuGet prefers the version of a package directly referenced by your project then one referenced by another package.  This is all explained in the How NuGet resolves package dependencies article but in summary NuGet tries to use the lowest version it can get away with with direct references overriding sub-references.

P.S. – I’m glad Barenaked Ladies (it’s a Canadian band, link is SFW) where able to continue without Steven Page but I miss his melancholy.  Plus him and Ed harmonized really well together.  This is one of my favorite melancholy BNL songs.  Yes I like it more then Brian Wilson.  Just ignore the cheesy video.

I couldn’t tell you
That I was wrong
I chickened out grabbed a pen and a paper
Sat down and I wrote this song

I couldn’t tell you
That you were right
So instead I looked in the mirror
Watched TV, laid awake all night

Posted in Uncategorized | Comments Off on Today I Learned How to Lock a Dependency to a Specific Version in Project JSON

My Takeaway from Reading Option B

Option B Book Cover

Book: Option B: Facing Adversity, Building Resilience, and Finding Joy
Author: Sheryl Sandberg, Adam Grant

Option B is not what I thought it would be about.  I thought it would be about what to do when your first plan fails for some reason.  Like plans you consciously made, such as a business plan, trip, etc.

Turns out the book is coming up with an option B for plans you didn’t even think you where making.  Such as assuming your family won’t die or won’t get seriously ill.  It is Sheryl Sandberg’s account of her husbands unexpected passing how she dealt with it.  She talks to others who dealt with death of loved ones and also what research says about dealing with death.  It does focus on other issues such as illness and other major life tragedies but mostly about death.

I have two takeaways from this book.  The first is that we have plans that we don’t even know about.  I dubbed them default plans but they are still plans.  That same way not making a choice is still a choice.

The second is that you should acknowledge and talk to the survivor about the death of their loved one.  We are often afraid of hurting the survivor by bringing up the subject.  That and hurting ourselves by bring up the topic.  The takeaway is to talk to your friends and family about the traumatic events.  They will appreciate it more then ignoring the elephant in the room.


Posted in Takeaways | Tagged , | Comments Off on My Takeaway from Reading Option B

Notes: Upgrade NUnit.Xamarin to NUnit 3.8.1

The notes I took when trying to upgrade the NUnit Xamarin project to NUnit 3.8.1.  The reason I did this was to fix issue #87.  When I try to us NUnit Xamarin on another project it fails with the following error:

D/Mono ( 2233): Assembly Loader probing location: 'System.Runtime.Loader'. 
F/monodroid-assembly( 2233): Could not load assembly 'System.Runtime.Loader' during startup registration. 
F/monodroid-assembly( 2233): This might be due to an invalid debug installation. 

Remember these are just notes and lack the polish [what polish? –Ed] of my other technical articles.


First I forked the project to my personal GitHub account then I cloned the repo to my local machine.

Open and compile the application.  No errors on the first try, that is a good sign.

NUnit Xamarin Compile Successful

For some reason ReSharper says there are 796 errors in 31 files.  The ReSharper “errors” are mostly in the shared projects.  I don’t think ReSharper knows how to deal with Shared projects correctly.  I’ll just ignore these errors for now.

Run iOS Project

Try to run it on iOS.  Can’t start the nunit.runner.iOS project because of the following error:

NUnit Xamarin Error Running NUnit.Runner.iOS Project

Changed it to nunit.runner.tests.iOS and it ran with the following results:

NUnit Xamarin iOS Test Result Summary

NUnit Xamarin iOS Test Result Failed Tests

Some of the tests failed.  Are they supposed to fail?  One second while I take a look at the tests.  I see some of them are supposed fail.  Thanks deliberate failure comment.

NUnit Xamarin Deliberate Failure Comment

Run Droid Project

I assume everything works on iOS.  Lets try running the Droid project.  Bummer, it no work.

NUnit Xamarin Droid Version Error

It appears that the Droid project file was updated to Android 7.1 but the json file still points to 6.0.  Why did VS auto update the project file to 7.1?  It appears that the project is set to use the latest Android version platform, which is currently 7.1 (Nougat).

NUnit Xamarin Droid Project Uses Default Android Version

Lets try switching it to 6.0 and see what happens.  It sill no work but I have seen this error message before.  It’s the initial bug I experienced in another project.

D/Mono ( 2233): Assembly Loader probing location: 'System.Runtime.Loader'.
F/monodroid-assembly( 2233): Could not load assembly 'System.Runtime.Loader' during startup registration.
F/monodroid-assembly( 2233): This might be due to an invalid debug installation.

Oh wait, there is a 3.8.1 branch in the repo that has already updated to Android 7.1.  At least there was an attempt but it looked like it failed.  Do I stay with master or switch to the branch?

Upgrade to NUnit 3.8.1

Lets stay on the master branch and work out a fix.  I’ll then ask if it should be merged to master or the branch.  First things first lets updated to Android 7.1 and see what happens.  First in the project properties switch back to Use the Lastest Platform setting and then updated the json file.  I currently looks like:

 "dependencies": {
 "NUnit": "3.6.1",
 "Xamarin.Forms": ""
 "frameworks": {
 "monoandroid60": {}
 "runtimes": {
 "win": {}

It links to an old Xamarin.Forms but lets leave that for now.  Hopefully upgrading to Android 7.1 will work with old version of Xamarin.  Lets upgrade the framework then run it.

 "frameworks": { 
   "monoandroid71": {} },

Well, got the same “Could not load assembly ‘System.Runtime.Loader'” error.

NUnit Xamarin Cannot Load Assembly System Runtime Loader

Lets see how NUnit.Xamarin links to NUnit.  How hard is it to upgrade to NUnit 3.8.1?

NUnit Xamarin NUnit Nuget

Notice that the packages come from NUnit AppVeyor CI.  I know AppVeyor is a continuous build system but not much else about it.  Do we have to use the packages only from AppVeyor?  I’ll have to ask.

It appears that NUnit is linked to via Nuget package.  I heard that it might NUnit.Xamarin might be compiled against NUnit but it appears to just use the Nuget package.  That is promising.  Lets try upgrading it to 3.8.1 and see what happens.

NUnit Xamarin Nuget Upgrade To 3.8.1

NUnit Xamarin Nuget Upgrade To 3.8.1 Changes

After the upgrade everything seems to work.  That is good news.

NUnit Xamarin Droid Success

Now I just need to check with the NUnit.Xamarin team what branch they would like this pull request put into.  I wonder if it will pass automated build tests?


Posted in Code Examples, Notes, Software Development | Tagged , , | Comments Off on Notes: Upgrade NUnit.Xamarin to NUnit 3.8.1

PSA: Arduino H Bridge is Sharp and Pointy

Just a quick public service announcement (PSA) that the Arduino H Bridge is sharp and pointy.  Both my daughter and myself hurt our fingers but only I was lucky enough to draw blood when one of the H Bridge pins got stuck under my finger nail.  I mean look at this insect looking pointy bastard.

H Bridge Top

H Bridge Bottom

All that pain and blood was worth it as we did finally get the lesson 10 (Zoetrope) from the Arduino Start Kit working.  We couldn’t get the example as shown in the book to work because we didn’t understand how the H Bridge worked.

Lesson 10 Zeotrope

With help from this article (with a handy diagram) we figured out the H Bridge and got the motor to spin.

Special thanks to Startup Edmonton for hosting us on their Monthly Hack Day.

Posted in Fun, Hardware | Tagged , , , | Comments Off on PSA: Arduino H Bridge is Sharp and Pointy