AL Build Helper for Dynamics 365 Business Central Builds

If you’re interested in setting up a build pipeline to build apps for Business Central then you’re probably interested in running the automated tests as part of it. (I take it you are writing automated tests?)

Turns out getting your test codeunits and methods populated into a test suite ready to run isn’t straightforward. We use a separate “Build Helper” app that exposes a couple of web service methods to prep and clear a test suite. It helps us get the container ready for running Run-TestsInBCContainer (from the navcontainerhelper module).

I’ve uploaded a couple of versions of the app to a GitHub repo here: https://github.com/CleverDynamics/al-build-helper. One for BC15 and the other for BC14 and earlier.

I use it all the time for running test from VS Code as well as in our build pipelines. Our PowerShell module has an Install-BuildHelper function to download and install it. Alternatively you could slip some PowerShell like the below into your pipeline and smoke it.

$Container = 'de'
$Company = 'CRONUS DE'
$User = 'admin'
$Password = 'P@ssword1'
$TestSuite = 'DEFAULT'
$StartRange = 130000
$EndRange = 160000
$WSPort = '7047'
$BuildHelperUrl = 'https://github.com/CleverDynamics/al-build-helper/raw/master/Clever%20Dynamics_Build%20Helper_BC14.app'

$Credential = [PSCredential]::new($user, (ConvertTo-SecureString $Password -AsPlainText -Force))
$BHPath = Join-Path $env:Temp 'BH.app'
Download-File $BuildHelperUrl $BHPath
Publish-NavContainerApp $Container -appfile $BHPath -sync -install
$BH = New-WebServiceProxy ('http://{0}:{1}/NAV/WS/{2}/Codeunit/AutomatedTestMgt' -f (Get-NavContainerIpAddress $Container), $WSPort, $Company) -Credential $Credential
$BH.GetTests($TestSuite, $StartRange, $EndRange)

The above is BC14 and assumes that you’ve got the navcontainerhelper module loaded (so you can use Publish-NavContainerApp). For BC15 you’d change the script slightly to the below (different URL for Build Helper, the instance name is “BC” rather than “NAV”).

$Container = 'bc15'
$Company = 'My Company'
$User = 'admin'
$Password = 'P@ssword1'
$TestSuite = 'DEFAULT'
$StartRange = 130000
$EndRange = 160000
$WSPort = '7047'
$BuildHelperUrl = 'https://github.com/CleverDynamics/al-build-helper/raw/master/Clever%20Dynamics_Build%20Helper.app'

$Credential = [PSCredential]::new($user, (ConvertTo-SecureString $Password -AsPlainText -Force))
$BHPath = Join-Path $env:Temp 'BH.app'
Download-File $BuildHelperUrl $BHPath
Publish-NavContainerApp $Container -appfile $BHPath -sync -install
$BH = New-WebServiceProxy ('http://{0}:{1}/BC/WS/{2}/Codeunit/AutomatedTestMgt' -f (Get-NavContainerIpAddress $Container), $WSPort, $Company) -Credential $Credential
$BH.GetTests($TestSuite, $StartRange, $EndRange)

No doubt, given the rate of change in Business Central there will be a different/better way to do this by the time BC15/wave 2/Fall ’19/whatever the heck we call it is released – but this how we build against BC15 for now.

Feel free to use anything you find helpful with my blessing…but not necessarily my support. No warranties, own risk etc.

8 thoughts on “AL Build Helper for Dynamics 365 Business Central Builds

  1. Hi James,

    Why not populate the the test suite in an install codeunit?

    I’m using an approach inspired by Freddy from here: https://dev.azure.com/businesscentralapps/HelloWorld/_git/HelloWorld?path=%2Ftest%2FCodeunit%2050131%20-%20HelloWorld%20Test%20Install.al&version=GBmaster&_a=contents

    Just noticed that he just updated for BC15 as well, even more straight forward than for BC14. (But prefer to use the SelectTestMethodsByExtension instead of SelectTestMethodsByRange)

    Cheers,
    Johannes

    Like

    1. Hi Johannes,

      Thanks for the question.

      Partly out of principle – I prefer to separate code that is for the benefit of the app and code that is for the benefit of the pipeline.

      Partly because our app is a little more flexible. We can avoid hardcoding the test suite name or codeunit range. We read the range(s) from app.json instead. It also gives us a service method to clear the test suite. Not really relevant for a pipeline but sometimes useful when I’m testing while developing and using it from VS Code.

      Of course, if you’re happy with the install codeunit approach you needn’t complicate things.

      Cheers,
      James

      Like

      1. Thanks for sharing your thought about this. I can agree with you on your points, but not to a degree that we’ll switch approach. 😉

        I like the fact that the test suite is always there while developing and we rip that code out of the app in the pipeline when we are extracting the test app from the main app. The only thing I don’t really like is that I had to hardcode the test suite name, the object range is solved automatically (very easy in BC15 with the SelectTestMethodsByExtension function).

        But of course, it is all about preferences.

        Anyway, keep up the good work! Unfortunately you blogged about the multi stage yaml pipelines just a bit too late, so I’ve struggled my own way through there as well… It’s really nice, and it seems as they are hard at work to improve it even more.

        Cheers,
        Johannes

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s