Tuesday, October 22, 2013

Setting Up NUnit Testing With Azure Deployments From Git

A few days ago while deploying my latest build to Windows Azure and looking through the deployment logs I noticed that the deployment wasn't running any of my unit tests.

So I began with a simple question to someone I respect very much and figured would know the answer better than anyone else I knew. (I'll admit I am a fan and wanted an opinion) 


If you haven't heard of Scott than definitely go check out his site http://www.hanselman.com (perhaps wait till after the article). Scott has done many tutorials on .net topics and I am sure he will have a post in no time on this topic too.

Most of the skills and knowledge I've learned now in my first year as a developer have come from Scott and others like him, as well the Virtual Academy. 
http://www.microsoftvirtualacademy.com/#fbid=6c6vaptZwtl if you're new to development like me than take a moment to check these resources out. (Don't worry I will wait)

Alright to be honest his answer didn't surprise me at all given that if Azure had offered the support it would be in one of the many settings or features it offers. 

Azure is already an amazingly simple and easy to use platform with one of the most intuitive user experiences ever, given the amount of content and feature rich options it has. 

Moving along...

A quick Google search revealed well... this, http://blog.amitapple.com/post/51576689501/testsduringazurewebsitesdeployment and it looked fairly straight forward so I grabbed a coffee and started to learn.

After a few hours and various Azure deployment errors that sometimes made no sense at all it wasn't looking too good for me.

Another few tweets to Scott and he pointed me into the direction of 

Once I had logged in and taken a look around I went into the deployments\tools folder and saw the deploy.cmd file Azure uses. (At least when you don't provide one)

The guide showed me how to output the deploy.cmd file from the Azure command line tool onto my local machine and that was great except one thing... it wouldn't deploy.

Why would the generated deploy.cmd not deploy? I mean it was generated by the tool and intended to give you a working deployment. So I compared it to the one in the deployment\tools folder and noticed the project folder was wrong.

I looked at my local folder structure and discovered that the solution (.sln) wasn't at the root or top level folder. The assumption would be that the deploy file would still work considering every deployment up to this point had.

Either way a few quick moves and re-organizing of the solution folder and I tried again... and again... and again.

Now what! I thought, seriously this was going into the night and my caffeine intake was depleted as was my patience. The issue now was my nuget packages were not deploying and once more I didn't understand why, because up to this point I had not even modified the deploy file once.

Looking inside my nuget.targets file I saw "restore packages" and "download nuget if missing" were both set to false, quick edit to true and... they started to load.

Ok at this point the deployment was working and after one full day of work just to get the auto generated deployment file working it... I added the unit testing commands and watched as Azure deployed with unit tests. 

So after all this there are a few things to consider that could cause issues.
- Azure does not seem to work ideally if your solution isn't in the root or top level folder. This was the case with my first issue.

- If you are using package restore in your project as I am, make sure your nuget targets file in the .nuget folder has "restore-packages" and "download-nuget-if-missing" set to true. It's not required, as you can check in nuget.exe instead (up to you)

- Make sure your tests are working, also if any are ignored tried commenting them out completely. (I had one deployment fail because I ran an ignored test)


WHAT I AM USING
- Visual Studio 2012
- BitBucket (Git Repository)
- ASP.Net MVC4 Application
- NUnit Testing Framework

HOW TO:
Begin by deploying a clean working deployment to Azure. 


It's important that you have an active deployment as this indicates the Azure deploy.cmd is working on the server. So with an active deployment we can begin.

== STEP 1 ======
Grab the Azure Command Line Tool

After the Azure CLI is installed open a command prompt and navigate to your solution folder then run this command,
azure site deploymentscript --aspWAP {PROJECT-FOLDER}\{PROJECT-NAME}.csproj -s {SOLUTION-NAME}.sln

Make sure you replace {PROJECT-FOLDER} {SOLUTION-NAME} and {PROJECT-NAME} with the correct names. 

Verify the files were generated in the solution folder (.deployment & deploy.cmd)

== STEP 2 ======
Let's verify the deploy.cmd matches what Azure uses (mine ended up being different and caused a few headaches). 

Go to    and login using your deployment credentials. If you experience issues logging in, go to your Azure Website dashboard and reset your deployment credentials.





Once you've successfully logged into the debug console navigate to  deployments\tools and click on the edit icon for the deploy.cmd file.

== STEP 3 ======
Copy this into a text editor on your machine and look at the Deployment section of the script to verify the paths match the ones inside the  deploy.cmd in the project folder which we generated earlier using the Azure CLI.

At this point I recommend you check-in the file to your git repository and verify it deploys. This will save many headaches and ensure any further errors truly are the developers'. 

== STEP 4 ======
Now that we know the deployment works, hopefully... we can continue onto the important step and the that is setting up your build for the test project and deploying the test runner.

Find This Line.
:: 1. Build to the temporary path
Copy the following and paste it a few lines below after the IF !ERRORLEVEL! NEQ 0 goto error
:: 2. Building test project
echo Building test project
"%MSBUILD_PATH%" "%DEPLOYMENT_SOURCE%\{TEST-PROJECT-FOLDER}\{TEST-PROJECT-NAME}.csproj"
IF !ERRORLEVEL! NEQ 0 goto error

:: 3. Running tests
echo Running tests
vstest.console.exe "%DEPLOYMENT_SOURCE%\{TEST-PROJECT-FOLDER}\bin\Debug\{TEST-PROJECT-NAME}.dll"
IF !ERRORLEVEL! NEQ 0 goto error
Make sure again of course that you replace the placeholder  {TEST-PROJECT-FOLDER} and {TEST-PROJECT-NAME} respectively to the correct values.

Your %DEPLOYMENT_SOURCE% folder usually points to the location of your solution file so use this as a reference in where you are in the folder tree. 

The vstest.console.exe is the test runner supplied in VS 2012 and will run most testing frameworks mstest, xunit or nunit which we are using.


vstest.console.exe will recognize any test that exists in the supplied test assembly and will run it whether the test is mstest, xunit or nunit, and they could all be in the same assembly.
You're done, commit your modified deploy scripts into your git repository and try your deployment.

Azure is always updating with new features and the efforts from the Azure team are amazing. I know one day deploying a TDD application will be a much easier task than it currently is.

It's a lot of work I know and I'll admit many junior developers may not want to challenge themselves with a task like this, for me personally it's becoming a passionate mindset.

Tired of Waiting for your Website to Load? $1/ mo Hosting + Free domain!

ThinkMonkey - Web Design Company | Software Company