Rich006
Rich006

Reputation: 215

How can I prevent an invalid file warning when running a Matlab code coverage report?

Overview

I am getting a warning from Matlab's code coverage that a file is "invalid", and I would like to get rid of that warning. I can think of two approaches, but I don't know how to accomplish either one. My ideas are:

  1. Fix the file so that the plugin does not flag it as invalid. (preferred)
  2. Tell the Matlab code coverage plugin to ignore the file. (acceptable)

Details

I have a file runTests.m that imports the Matlab code coverage plugin for unit tests:

import matlab.unittest.TestRunner
import matlab.unittest.plugins.CodeCoveragePlugin
import matlab.unittest.plugins.codecoverage.CoverageReport

When I run the tests, they all pass and the coverage report seems to have the correct information except for one App Designer file. For that file, I get this warning:

Warning: Coverage result is empty for /MyPath/myApp.mlapp because the file is invalid.

How can I tell the code coverage plugin to ignore the App Designer .mlapp file? The app works, so I don't know why it's invalid. If I run the same test suite on a new blank app, there is no warning.

The rest of the warning output is:

In matlab.coverage.internal.ResultBuilder.createFromCodeCoverageCollectorData (line 20)
In matlab.coverage.internal/ResultBuilder/create (line 73)
In matlab.coverage.internal.getResults (line 11)
In matlab.unittest.internal.coverage.CodeCoverageCollector.get.Results (line 65)
In matlab.unittest.internal.plugins.CodeCoverageCollectionPlugin.runTestSuite/assignResultsAndResetProfilerCollector (line 25)
In matlab.unittest.internal.plugins.CodeCoverageCollectionPlugin>@()assignResultsAndResetProfilerCollector(plugin,pluginData.CommunicationBuffer) (line 22)
In onCleanup/delete (line 25)
In matlab.unittest.internal.plugins/CodeCoverageCollectionPlugin/runTestSuite (line 20)
In matlab.unittest.plugins/CodeCoveragePlugin/runTestSuite (line 350)
In matlab.unittest.plugins/TestRunnerPlugin/runTestSuite (line 265)
In matlab.unittest.plugins.xml/JUnitXMLOutputPlugin/runTestSuite (line 66)
In matlab.unittest/TestRunner/evaluateMethodOnPlugins (line 425)
In matlab.unittest.internal/SerialTestRunStrategy/runTestSuite (line 36)
In matlab.unittest.internal/SerialTestRunStrategy/runSession (line 16)
In matlab.unittest/TestRunner/runSession (line 1260)
In matlab.unittest.plugins/TestRunnerPlugin/runSession (line 228)
In matlab.unittest.plugins/CodeCoveragePlugin/runSession (line 345)
In matlab.unittest.plugins/TestRunnerPlugin/runSession (line 228)
In matlab.unittest.plugins.xml/JUnitXMLOutputPlugin/runSession (line 58)
In matlab.unittest/TestRunner/evaluateMethodOnPlugins (line 425)
In matlab.unittest/TestRunner/doRunWithFcn (line 413)
In matlab.unittest/TestRunner/run (line 297)
In runTests (line 55)

Upvotes: 1

Views: 54

Answers (1)

Erik
Erik

Reputation: 932

It appears that the ResultBuilder is very sensitive to syntax errors and even mlint warnings, coupled with a bug in createFromCodeCoverageCollectorData where it reports the wrong filename. In my case, using R2023b update 7, I got the same warning.

If you can edit your installation, the link below suggests editing createFromCodeCoverageCollectorData.m to:

if files(1).status == "FAILED"
    warning(message('MATLAB:coverage:result:InvalidFileForCoverage', ...
        files(1).path));
end

At work, I'm not able to edit my installation, and so have to put a break-point on the warning to inspect the real filename.

Once you have it reporting the correct problematic file, then hopefully you can fix your file.

In my case, I had the wrong function name on the first line of the m-file, which is only an mlint warning not a syntax error, but was enough to trigger this problem.

See: https://uk.mathworks.com/matlabcentral/answers/2048192-why-do-i-get-a-warning-that-the-code-coverage-result-is-empty-because-the-file-is-invalid

Upvotes: 1

Related Questions