Monday, October 2, 2023
HomeMatlabWe’ve obtained you coated! » Developer Zone

We’ve obtained you coated! » Developer Zone

Within the earlier weblog put up of this sequence, I gave an outline of MATLAB Check and went into the main points of the Check Supervisor. In the present day’s matter is code protection.

Code protection is a measure of “how a lot” of your code has been executed. By measuring and inspecting code protection, you may achieve insights into which elements of your code haven’t been absolutely examined, and due to this fact the place you need to direct your efforts in writing extra check instances. Equally, if the code protection report is saying that some elements of your code are usually not being executed and you understand you’re satisfying your necessities, chances are high you may delete this lifeless logic.

There are a selection of how of measuring code protection. Base MATLAB gives operate and assertion protection while MATLAB Check provides to that by offering determination, situation, and modified situation/determination protection metrics. Right here’s a abstract of the completely different protection metrics:


What stage of protection do I want?

Given the above protection sorts, which one(s) do you have to use and what stage of protection do it’s worthwhile to obtain? The reply to is that it’s largely your selection primarily based in your mission’s necessities and constraints.

Assertion protection is the place to begin for common objective functions. Nevertheless, you’ll have constraints imposed by rules for those who’re working in a security vital setting akin to medical (IEC 62304), aerospace (DO-178), or automotive (ISO 26262).

Considered one of my colleagues likes to make use of the analogy of selecting an insurance coverage coverage – if the danger is low and the affect of something going flawed can be low, you’ll seemingly select the most affordable, most elementary insurance coverage. Nevertheless, when the danger and penalties of one thing going flawed are excessive, you’ll need a complete coverage which comes at extra value. On the earth of testing, that extra complete insurance coverage corresponds to attaining growing ranges of protection with extra superior metrics. The fee is your time to write down the extra checks.

Gathering protection

How can we acquire protection? There are 3 ways!

Check Browser

In the event you’re utilizing the Check Browser (MATLAB R2023a onwards), click on the protection button, examine “Allow Protection reporting”, and add the recordsdata or folders of code that you just wish to measure protection for. Then run your checks and an easy-to-read HTML protection report will open.


Gathering assertion protection by way of the Check Browser.

Observe that if you wish to report determination, situation, or MC/DC, you’ll nonetheless want a MATLAB Check licence.

Check Supervisor

In the event you’re utilizing the Check Supervisor (MATLAB Check, R2023a onwards), click on the protection button and examine “Allow Protection” within the pop-out menu.


Gathering assertion protection by way of the Check Supervisor.

Run the checks after which view the report by clicking the adjoining menu button:


Opening the protection report from the Check Supervisor.

Command Window

Lastly, for those who’re working your checks programmatically utilizing runtests, you need to use the ReportCoverageFor choice to specify the recordsdata, folders, and packages that you just wish to report protection for.
(This offers you the default assertion and performance protection. If you wish to use the extra superior protection metrics, you’ll want to make use of the longhand technique to create your individual TestRunner and add the CodeCoveragePlugin right here you may specify the protection stage.)

As soon as your checks have completed working, a hyperlink will seem within the Command Window taking you to the protection report.

Interactive HTML protection report

The code protection report appears to be like like this:


Instance code protection report.

The highest part gives a abstract of all of the code you may have measured protection for, for all of the completely different protection sorts you may have chosen. Use the “Presently viewing” drop-down menu to decide on which kind of protection is being proven in the remainder of the report.

The center part gives a breakdown on a per file foundation. Click on a line within the desk to decide on which file is displayed within the backside part.

The underside part gives protection data on a line-by-line foundation for the chosen protection sort and the chosen file. We’ll discover the main points of this subsequent.

Sorts of protection

Perform protection

Perform and assertion protection are the default protection metrics that MATLAB information. Perform protection signifies whether or not every operate in your code will get executed at the very least as soon as. (This isn’t to be confused with purposefulprotection which pertains to the general performance being appropriate. This and requirements-based testing are matters for one more day!)

Within the following instance, now we have three capabilities – the top-level operate myFunction and two nested capabilities fcnOne and fcnTwo. I’ve recorded protection for after I name myFunction(0,0,0).


Instance operate protection report with 67% protection.

This executes myFunction itself and fcnTwo however not fcnOne. We due to this fact have 3 capabilities in whole of which 2 have been executed, giving 2/3 = 67% operate protection. There are clearly holes on our testing so let’s transfer onto assertion protection.

Assertion protection

Statements are small chunks of code that MATLAB executes which are separated by a comma, semicolon, or newline character. To realize 100% assertion protection, every assertion a lot be executed at the very least as soon as.

The next snippet from the protection report exhibits assertion protection for after I name myFunction(0,0,0).


Instance assertion protection report with 60% protection.

The department will not be executed so strains 4 and 12 are usually not coated and present in pink. We’ve 5 statements in whole, 3 of which have been executed, so the assertion protection is 3/5 = 60%. By inspection we will see that the ifassertion is just ever false; we’re lacking the case of it being true. Let’s take a look at how we will view this extra formally.

Choice protection

Selections are expressions that alter the execution stream of the code at the side of the MATLAB key phrases if, elseif, swap, for, or whereas. Choice protection measures the extent to which all these selections have been examined to be each true and false. It due to this fact gives you with a view of code branches with lacking checks.

(As well as, the short-circuit operators || and && are additionally selections for the reason that worth on the left-hand aspect of the expression determines whether or not the right-hand aspect is evaluated. Nevertheless, to be in line with Simulink Protection, these expressions (“non-branch selections”) are excluded from the metrics when recording determination protection however are included when recording MC/DC.)

Within the earlier instance we achieved full assertion protection for the if determination on line 3. Within the following instance, I’ve as soon as once more examined my code by calling myFunction(0,0,0) and have measured determination protection.


Instance determination protection report with 50% protection.

We now see that line 3 is just partially coated (orange) because it solely evaluates to false. There may be 1 determination with 2 doable values, so 2 mixtures in whole. We hit 1 of them in order that’s 1/2 = 50% determination protection.

I’ll now add in a further check level in order that I’m now calling myFunction(0,0,0) and myFunction(1,0,0). This achieves full determination protection:


Instance determination protection report with 100% protection.

You may additionally have observed that the protection recording stage is cumulative – recording determination protection additionally contains assertion and performance protection.

Situation protection

Selections may be composed of two or extra logical expressions separated by a short-circuit operator (|| or &&) – these are referred to as circumstances. Right here is an instance with 1 determination and a pair of circumstances:

if (x > -3) || (x == -5)

The next incorporates one determination however no circumstances as there are not any expressions separated by a short-circuit operator:

Beneath is the situation protection report for our check code after I name myFunction(0,0,0) and myFunction(1,0,0). Bear in mind how we achieved full protection beforehand? Not anymore! Situation protection reveals but extra holes in our testing.


Instance situation protection report with 50% protection.

The x situation is roofed (inexperienced) because it takes values of each true and false. The y situation is partially coated (orange) because it solely evaluates to false. The z situation will not be coated (pink) as it’s by no means executed – why? y is at all times false and so the && quick circuits and doesn’t trouble evaluating z.

We’ve 3 circumstances, every of which may take two values (true or false), so there are 6 mixtures in whole. We’re hitting 3/6 = 50% protection.

Let’s modify our checks to fill within the lacking protection:

  • myFunction(0,0,0)
  • myFunction(1,0,0)
  • myFunction(0,1,0)
  • myFunction(0,1,1)

Instance situation protection report with 100% protection.


MC/DC stands for Modified situation/determination protection. It identifies how checks independently train circumstances inside selections. To realize full MC/DC, each of the next should be achieved:

  1. All circumstances inside selections are evaluated to each true and false.
  2. Each situation inside a choice independently impacts the result of the choice.

MC/DC is each stricter than situation protection while permitting full protection to be achieved with fewer checks than could be required to check all mixtures of circumstances exhaustively. Think about our if assertion of 1 determination with 3 circumstances:

All of the doable mixtures of x, y, and z and the corresponding outcome are listed within the desk beneath.


Taking a look at this desk, we will establish check mixtures that fulfill the necessities of MC/DC. Every situation should consider to true and false, and independently have an effect on the outcome. For instance:

  • x – 2 and 6.
  • y – 2 and 4.
  • z – 3 and 4.

We are able to due to this fact obtain full MC/DC with check mixtures 2, 3, 4, and 6. Right here’s the corresponding protection report after I run these check mixtures:


Instance MC/DC report with 100% protection.

Underneath the “Check Pairs” heading, you may see the assorted mixtures of enter values used to attain 100% MC/DC. “T” denotes true, “F” false, and “x” is don’t care.


Against this, right here’s an instance of incomplete MC/DC after I solely ran check mixtures 2 & 6:


Instance MC/DC report with 33% protection.

As per the bulleted record above, check mixtures 2 & 6 present full MC/DC for x, and for y to independently produce a results of false. Observe how the framework has used check situation 2 (FFx) within the first check pair versus check situation 3 beforehand.

We’ve 1 out of our 3 circumstances attaining MC/DC, so the general protection is 1/3 = 33%. By comparability, now we have examined 3/6 circumstances so have achieved 50% situation protection. This demonstrates how MC/DC is a stricter metric than situation protection.


We’ve seemed on the following sorts of protection:

  • Perform (MATLAB)
  • Assertion (MATLAB)
  • Choice (MATLAB Check)
  • Situation (MATLAB Check)
  • Modified Situation / Choice Protection (MCDC) (MATLAB Check).

These metrics can be utilized to information your testing and due to this fact enable you to supply prime quality software program whether or not that’s for on a regular basis desktop use or for security vital functions. Bear in mind, getting 100% protection will not be the objective – the objective is healthier high quality software program in line with the time price range of the mission, the wants of the tip deliverable, and the place it is going to be deployed.

Within the subsequent put up on this sequence, we’ll take a look at MATLAB Check’s Code High quality dashboard.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments