Pester Testing And PowerShell Development Workflow

In this post I’m going to have a look at Pester testing and PowerShell development workflow. I’ll be using Visual Studio and Team Foundation Server/Visual Studio Team Services to keep it simple. What I’m going to show won’t be news for developers but this can help to put testing in perspective for non developers.

Developer WorkFlow
Firstly lets have a look at the typical points during development when you want to run your tests and then we will dive into the details of getting it to work.

  • Running tests locally while developing.
    This depends on the development methodology you follow.

    • If you practice TDD you will be writing unit tests as you develop, a few lines at a time.
    • Depending on the size of the script most other developers will be writing a function and then a unit test for it or the whole script and then multiple unit tests. Remember it is better to write the tests while the code is still fresh in your mind.These scenarios work seamlessly if you use Visual Studio with PowerShell tools, Visual Studio will discover your Pester tests and show them in the test explorer.

Side Note: You can also use the tests to make development easier by mocking out infrastructure that is tricky to test like deleting user accounts in AD or deleting VMs.

  • Running tests with a gated checkin. In this case you want VSTS/TFS to run the build and test suite before the code is committed to the main code repository. VSTS/TFS  supports check in policies and one of them requires the build to pass before it is committed.
  • Running the tests after checkin as part of the build process. This is how most developers will run it and although VSTS/TFS doesn’t have native support for Pester tests you can run the tests using a Powershell script. Usually the whole test suite is executed at this point and can include integration tests.

For the last two scenarios to work you have to be able to install the Pester and PSCodeAnalyzer modules as well as any other modules required for your script on the build server. If you are using VSTS hosted build agents this is a bit of a problem since you need administrator access to install modules in the default locations for PowerShell. You can however manually import a module from a local folder during the build process. You can include the modules in you project or store them in a central location or repo and share them between your scripts. Some modules can even be installed from the Marketplace as a build step. If you are hosting your own Visual Studio Agents or using TFS this is not  a problem, you can install them as part of your build server or on demand.

Running A Pester Test
To execute Pester tests you have run a PowerShell script that will call Invoke-Pester. By default Pester will look for all scripts called *.Test.ps1 recursively but you can specify the script to test and even the specific functions in a script. Read the full documentation here.

Retrieving Test Results
Pester can output the test results in NUnit XML consult the documentation for the syntax and output options. VSTS/TFS can display this format on the test results tab. It doesn’t seem to do the same for the CodeCoverage results but you can still attach the output of the code coverage to your build artifacts by using VSTS/TFS commands.

Alternatively you can roll your own test results by passing the -PassThru parameter to Invoke-Pester, this will return the results as a typed object. You can use this to transform the results in any format you want.

Francois Delport

Windows 10 Core IOT And Raspberry PI 2 Introduction

In this post I’m going to give a quick Windows 10 Core IOT and Raspberry PI 2 introduction. I recently saw this article on LifeHacker to build your own Alexa clone using a Raspberry PI and thought it would be fun to build my own virtual assistant for Windows. My plan is to use Windows IOT on a Raspberry PI 2 with Cortana and/or Azure Cognitive Services and possibly a touch screen. I had a quick look at Windows IOT when support for Raspberry PI 2 was announced way back and it was pretty bare bones. Luckily it looks a lot better today and in this post I’m going to touch on some main points of functionality to highlight what is possible and identify possible blockers.

Windows IOT hardware drivers
Turns out you can create driver packages using a Windows 10 machine where the device is working and deploy them to Windows Core IOT machines but only for x86 devices not ARM. Sadly this won’t help my project since Raspberry PI is ARM. There is a hardware compatibility list here. I’ll try a few different Wifi dongles and report back in a few weeks. Same goes for a microphone, the PI has audio out but no microphone, you have to find a USB sound card that works.

Windows 10 Core Applications
As far as I can tell there are two different application types you can create for Windows IOT.

  • Windows IOT background applications which are basically like a “service”, it starts with the device and doesn’t have a GUI, link to templates here.
  • Universal Windows Platform applications and this is the cool part, you can develop the application once and deploy to many different devices types like mobile, Xbox, desktop and of course IOT machines. You can only have one foreground application running at a time with Windows IOT but you can deploy multiple apps to the device. You can also do something like embedding a browser control in your UWP application and use the official Raspberry PI touch screen to control it.

Development Experience
You can use Visual Studio by installing the Universal Windows App Development Tools during setup or the first time you try to use the templates it will prompt you to install it. I also have a feeling you need Visual Studio update 3 to install the latest version of the tools. You can deploy and do remote debugging of your application from Visual Studio.

Device management
You can use the Windows IOT dashboard to manage your devices and prepare SD cards for new devices, it can be downloaded from here.

Windows 10 Core IOT And Raspberry PI 2 Introduction

Windows IOT Core devices have a management service you can access from your browser by default it is running on port 8080.

Windows 10 Core IOT And Raspberry PI 2 Introduction

One of the great features is remote access to your device, you can enable the remote server from the device portal and the remote client is in the Windows Store, there is a link to it on the device portal. It is in preview but works well enough to be useful and it saves you connecting a screen and mouse/keyboard.

Francois Delport