I must admit I hadn’t heard of glob patterns until seeing some settings in VS Code that use them. They are a way of defining a pattern that matches one or more files and folders. For example, the Text Editor section of the settings (UI) in VS Code allows you to exclude files and folders from the file explorer with glob patterns and gives you this useful link for more information.
My AL Test Runner extension has a glob pattern setting to identify your test codeunits (used when identifying the file path and line number of a failing test to give you a clickable link in the output window).
Turns out that glob patterns are case-sensitive. You can, however, match from a group of characters (like regular expressions) with square brackets. Useful if you’ve got inconsistently named files. My Test Codeunit Glob Pattern setting (in AL Test Runner) is set to **Tests/*.[cC]odeunit.al i.e. match files in a Tests folder which end with either codeunit.al or Codeunit.al
Apologies for another post about AL Test Runner. If you don’t use or care about the extension you can probably stop reading now and come back next time. It isn’t my intention to keep banging on about it – but the latest version (v0.2.1) does plug a significant gap.
Next time I’ll move onto a different subject – some thoughts about how we use Git to manage our code effectively.
Developing Against a Remote Docker Container
While I still prefer developing against a local Docker container I know that many others publish their apps to a container hosted somewhere else. In which case your options for running tests against that container are:
Using the Remote Development capability of VS Code to open a terminal and execute PowerShell on the remote host – discussed here and favoured by Tobias Fenster (although his views on The Beautiful Game may make you suspicious of any of his opinions 😉)
Enabling PS-Remoting and opening a PowerShell session to the host to execute some commands over the network – today’s topic
Again, shout out to Max and colleagues for opening a pull request with their changes to enable this and for testing these latest mods.
Enable PS Remoting
Firstly, you’re going to need to be able to open a PowerShell session to the Docker host with:
New-PSSession <computer name>
I won’t pretend to understand the intricacies of setting this up in different scenarios – you should probably read the blog of someone who knows what they are talking about if you need help with it.
The solution will likely include:
Opening a PowerShell session on the host as administrator and running Enable-PSRemoting
Making sure the firewall is open to the port that you are connecting over
Passing a credential and possibly an authentication type to New-PSSession
To connect to my test server in Azure I run the following:
They are several new keys in the AL Test Runner config file to accommodate remote containers. There are also a few new commands to help create the required config.
The Open Config File command will open the config JSON file or create it, if it doesn’t already exist. Set Container Credential and Set VM Credential can be used to set the credentials used to connect to the container and the remote host respectively.
The required config keys are:
dockerHost – the name of the server that is hosting the Docker containers. This name will be used to create the remote PowerShell session. Leaving this blank implies that containers are hosted locally and the extension will work as before
vmUserName / vmSecurePassword – the credentials used to connect to the Docker host
remoteContainerName – the name of the container to run tests against
newPSSessionOptions – switches and parameters that should be added to New-PSSession to open the session to the Docker host (see below)
The extension uses New-PSSession to open the PowerShell session to the Docker host. The ComputerName and Credential parameters will populated from the dockerHost and vmUserName / vmSecurePassword config values respectively.
Any additional parameters that must be specified should be added to the newPSSessionOptions config key. As in my case I run
The most obvious limitation of the AL Test Runner extension for VS Code has been that you need to run VS Code on the Docker host machine. That’s fine for us because we do all our development on local Docker containers but I’m aware that this isn’t everyone’s preferred process.
Local Repo and VS Code, Remote Docker Host
I guess if you’re not hosting the Docker container locally then you are hosting it on some remote server – maybe on your own hardware or maybe in Azure or another cloud. To get AL Test Runner working in this scenario you’d need the AL Test Runner PowerShell module imported on the host and PS Remoting enabled to execute PowerShell on the host from your local VS Code terminal.
This post isn’t about getting that working. It’s not supported yet – although I do have a pull request to review from a team that are using it like this (thanks Max).
Remote Development with VS Code
An alternative approach is to use remote development with VS Code. The files that you are working on and the Docker host are remote but you are using VS Code locally. Kind of like RemoteDesktop Apps – the benefit of running on a server and using its resources but with the experience of an app that is running locally.
Install Open SSH server on the remote machine, install some VS Code extensions (using an insider build of VS Code – for now) and connect over SSH to the machine. Some magic happens at the other end and a few spells, invocations and minutes later a VS Code Server is installed on the remote machine.
It allows you to work in a local VS Code window but access the file system of the server and execute commands on it. Install VS Code extensions and PowerShell modules as if you are working locally and they are installed on the remote.
It is smooth. Impressively so. You quickly forget that you aren’t just working with files and extensions on your local machine. This clip shows:
Selecting a folder on the remote server from my recent history
Connecting via SSH
Entering the password for the remote account that I am authenticating as (a local account as my VM in Azure is not joined to a domain)
Running all the tests in the project and working with VS Code as I would do locally
I still prefer actual local development, but I have to admit that this is pretty great.
AL Test Runner
I span up a remote development scenario out of curiosity but I also wanted to test how/if AL Test Runner would work. It works almost seamlessly. Almost. There is just a little stretch of exposing stitching – but it’s easy to work around.
Once you’ve opened a remote development window you’ll need to:
Install the AL extension
Install the AL Test Runner extension
Install the navcontainerhelper PowerShell module (you can use install-module in the integrated terminal)
If you try to run tests you’ll find that it appears to hang indefinitely. Actually it has popped a window to enter the credentials to connect to the server with – but you can’t see it and it won’t continue until you dismiss the window.
If you’re interested in trying it the workaround for now is to manually edit the config.json file in the .altestrunner folder.
When you first install the extension you won’t have a config.json file. Running a test, any test is enough to create it. You’ll also notice that the command appears to hang in the terminal. You can kill that terminal once the file has been created.
Open config.json and enter the userName to authenticate with BC. Next you need to enter the securePassword (this is not your plain text password). You can get the secure password running the following in the terminal:
This is a brief update about the AL Test Runner extension that I’ve been working on for VS Code. I’ve had some great feedback and suggestions via GitHub, a pull request correcting me being a muppet and 300-odd installs so far. Thanks to everyone who has got involved to improve it.
This version includes a new setting “Publish Before Test” which allows you to invoke either the “Publish without debugging” or “Rapid Application Publish without debugging” commands in the AL Language extension.
If you’re aiming for a quick write test->run test->write code->run test cycle then you might like to try setting this option to “Rapid application publish”. Create a test->hit Ctrl+Alt+T (rapid publish and run the current test)->see the test fail->write the code->run the test…
Obviously, the above is a silly example. Don’t make your tests pass by simply commenting out any code that causes it to fail 😉 You’ll still need to do a full publish from time to time as not all changes are pushed in a rapid publish.
The main improvement to the extension would come in the form of support for remote development. There are, I think, two key scenarios:
Visual Studio Code is running locally but Docker host is not i.e. apps are being published to some central server
The ALTestRunner module would need to be installed on the Docker host (maybe served from the PowerShell Gallery?) and the PowerShell commands invoked on the server (through SSH? through a PowerShell session?)
Remote Visual Studio Code development – where both VS Code and the Docker host are running remotely somewhere
I don’t know enough about this way of working yet (we’ve only ever developed on Docker containers running on our own laptops) but with AL Test Runner installed on the remote end maybe this sort of works already?
In both cases I think the results file would need to passed back somehow to the developer’s machine in order to decorate the local copy of the AL file that they are working on. If you know more about these scenarios than I do or have other suggestions I’d be glad to hear from you.
I’m planning a few other bits and pieces like adding the path to file/line of a failing test in the Output window so that you can click and jump straight to it.
And with that I’m going to sign off for this year. Merry Christmas and see you in 2020!
I’ve written an extension for VS Code to help run your AL tests in local Docker containers. Search for “AL Test Runner” in the extension marketplace or click here. Feedback, bugs, feature suggestions all gratefully received on the GitHub repo or email@example.com
As soon as Freddy added capability to the navcontainerhelper module to execute automated tests I was excited about the potential for:
Making test execution in our build pipeline simpler and more reliable
Running tests from Visual Studio Code as part while developing
I’ve written about both aspects in the past, but especially #1 recently – about incorporating automated tests into your Azure DevOps pipeline.
This post is about #2 – incorporating running tests as early as possible into your development cycle.
Finding Bugs ASAP
You’ve probably heard the idea – and it’s common sense even if you haven’t – that the cost of finding a bug in your software increases the later in the development/deployment cycle you find it.
If you realise you made a silly mistake in code that you wrote 2 minutes ago – there’s likely no harm done. Realise there is a bug in software that is now live in customers’ databases and the implications could be much greater. Potentially annoyed customers, data that now needs fixing, support cases, having to rush out a hotfix etc.
We’ve all been there. It’s not a nice place to visit. I once deleted all the (hundreds of thousands of) records in the Purch. Rcpt. Line table with a Rec.DELETEALL on a temporary table…turns out it wasn’t temporary…and I was working in the live database.
Writing automated tests can help catch problems before you release them out into the wild. They force you to think about the expected behaviour of the code and then test whether it actually behaves like that. Hopefully if the code that we push to a branch in Azure DevOps has a bug it will cause a test to fail, the artifacts won’t be published, the developer will get an email and the customer won’t be the hapless recipient of our mistake. No harm done.
However, the rising cost of finding a bug over time still applies. Especially if the developer has started working on something else or gone home. Getting back your head back into the code, reproducing and finding the bug and fixing it are harder if you’ve had a break from the code than if you went looking for it straight away.
Running Tests from VS Code
That’s why I’m keen that we run tests from VS Code as we are writing them. Write a test, see it fail, write the code, see the test pass, repeat.
I’ve written about this before. You can use tasks in VS Code to execute the required PowerShell to run the tests. The task gives you access to the current file and line no. so that you can fancy stuff like running only the current test or test codeunit.
AL Test Runner
However, I was keen to improve on this and so have started work on a VS Code extension – AL Test Runner.
The goals are to:
Make it as simple as possible to run the current test, tests in the current codeunit or all tests in the extension with commands and keyboard shortcuts
Cache the test results
Decorate test methods according to the latest test results – pass, fail or untested
Provide extra details e.g. error message and callstack when hovering over the test name
Add a snippet to make it easier to create new tests with placeholders for GIVEN, WHEN and THEN statements
Important:this is for running tests with the navcontainerhelper PowerShell module against a local Docker container. Please make sure that you are using the latest version of navcontainerhelper.
Download the extension from the extension marketplace in VS Code and reload the window.
Open a folder containing an AL project
Open a test codeunit, you should notice that the names of test methods are decorated with an amber background (as there are no results available for those tests)
The colours for passing, failing and untested tests are configurable if you don’t like them or they don’t fit with your VS Code theme. Alternatively you can turn test decoration off altogether if you don’t like it
Place the cursor in a test method and run the “AL Test Runner: Run Current Test” command (Ctrl+Alt+T)
You should be prompted to select a debug configuration (from launch.json), company name, test suite name and credentials as appropriate (depends if you’re running BC14 or BC15, if you have multiple companies, authentication type etc.)
I’ve noticed that sometimes the output isn’t displayed in the new terminal when it is first created – I don’t know why. Subsequent commands always seem to show up fine 🤷♂️
Use the “ttestprocedure” to create new test methods
If you’re using Git then I’d recommend adding the .altestrunner folder to your .gitignore file:
Committing the config file and the test results xml files doesn’t feel like a great idea.