Matlab with Azure CI

Matlab CI services include Azure Pipelines. Matlab Azure Pipelines CI is useful to automatically unit test Matlab code on each “git push” as with other coding languages. We use an “azure-pipelines.yml” file in the Git repo to control CI behavior. A top-level file named like “TestAll.m” is also needed to control CI run.

Matlab azure-pipelines.yml

This azure-pipelines.yml file will test a Matlab project on Azure Pipelines CI at no cost. It can be useful to specify a specific Matlab version to ensure a baseline Matlab version compatibility. For example, specify the older Matlab version the project must support, as developers typically use newer Matlab versions and may inadvertently use Matlab functions only available in newer Matlab versions than some users or project requirements specify.

pool:
  vmImage: ubuntu-latest
steps:
  - task: InstallMATLAB@0
    inputs:
      release: R2020a
  - task: RunMATLABCommand@0
    inputs:
      command: TestAll
  - task: PublishTestResults@2
    condition: succeededOrFailed()
    inputs:
      testResultsFiles: test-results/results.xml
  - task: PublishCodeCoverageResults@1
    inputs:
      codeCoverageTool: Cobertura
      summaryFileLocation: code-coverage/coverage.xml

A file at the top level with name like TestAll.m is also necessary, with contents like:

% this script is run on Azure (or local)
import matlab.unittest.TestRunner
import matlab.unittest.Verbosity
import matlab.unittest.plugins.CodeCoveragePlugin
import matlab.unittest.plugins.XMLPlugin
import matlab.unittest.plugins.codecoverage.CoberturaFormat

name = "MyProject";

suite = testsuite(name);
runner = TestRunner.withTextOutput('OutputDetail', Verbosity.Detailed);

mkdir('code-coverage');
mkdir('test-results');

runner.addPlugin(XMLPlugin.producingJUnitFormat('test-results/results.xml'));
runner.addPlugin(CodeCoveragePlugin.forPackage(name, 'Producing', CoberturaFormat('code-coverage/coverage.xml')));


results = runner.run(suite);
assert(~isempty(results), "no tests found")

assertSuccess(results)

Examples

We have several projects using Azure Pipelines with Matlab. It’s often useful to start from a known-working example as below: