Saturday, December 31, 2016

Enable Live Unit Testing on project created before VS 2017

To be able to run live unit testing in Visual Studio 2017 on projects created with earlier versions of Visual Studio you need to add two nuget packages to your test-projects. Supported testprojects are MsTest, NUnit and xUnit; and they demands different packages.
To add nuget packages right click on your test project and choose Manage NuGet Packages then pick Browse and search for them.
Projects will continue to work in earlier version of Visual Studio and your build server will not have a problem with this since all they need is located inside the test projects now. You can also remove all previous references to earlier test dll:s in your test projects.

Microsoft Test

- MSTest.TestAdapter minmium 1.1.4-preview
- MSTest.TestFrameworks minimum 1.0.5-preview 


- NUnit3TestAdapter minimum 3.5.1
- NUnit minimum 3.5.0


I havn’t had the option of updating a xUnit test project but according to Microsoft, it should be:
- xunit.runner.visualstudio minimum 2.2.0-beta3-build1187
- xunit minimum 2.0

Live Unit Testing i Visual Studio 2017

Would it be nice to, directly in your code see if it are covered by any tests or even better; if what you just changed, broke any test? This is possible in Visual Studio 2017 Professional. Do you have test projects created in earlier version of Visual Studio? Then you need to update some references first: Enable Live Unit Testing
To start the live testing use the menu Test and choost Start under Live Unit Testing:
The test will run in the background and when it’s done all your code rows will be marked with one out of three symbols:

- This row is associated with at least one failed test.

- Every tests that runs this row is approved.

- This row doesn’t belong to any tests.

If you turn on this feature for a project that are completely without tests, you get a result like this::

Then when you start adding test you will get markings on what rows are approved by tests or not. If you continue to add code, your markings will update for your new code directly:

Saturday, December 17, 2016

Nice news in c#7

C#7 is coming together with Visual Studio 2017 and here is my two favorite news.

Throw Exception

It’s a common thing to control your input parameters to prevent future reference null exceptions before you assign it to a field like this:
if (person == null)
{ throw new ArgumentException("Person can't be null"); }
_person = person;
But now you can do it like this:
_person = person ?? throw new ArgumentException("Person can't be null");

Out variables

When we use a function that demands an out variable it’s common that half the times we don’t even care about that value when the function returns, but we do have to be nice and declare that variable before calling that function. Like this::
int i;
var isNumber = int.TryParse(s, out i);
Now i c#7 we can declare inside the function call:
var isNumber = int.TryParse(s, out int i);