Today I Learned How to Create Custom NUnit Constraints – Part 2: Constraint Usage Syntax

In Part 1 we created a custom NUnit constraint but you where limited in using the constraint.  You could only write:

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

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

It would be nice to write:

// Directly access the constraint from Is.
Assert.That(expected, Is.EquivalentPropertyWiseTo(actual));

// Chain the constraint in Is.
Assert.That(expected, Is.Not.EquivalentPropertyWiseTo(actual));

To do that you should read the documentation.  End of blog post.

So is that joke still funny?  No, OK, no more.  Lets get back to what we are doing.  To access the method using Is you need to override the Is class and add a static method.  I recommended making the method name similar to your constraint name.  For example:

[Read More]

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.

[Read More]

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:

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

  // Save the value.
  _someDbService.Save(dtoToSave);

  // 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.

[Read More]
Categories: code-examples  Tags: nconstraints nunit 

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).

[Read More]

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.

[Read More]