Using Code Coverage in batchmode
There are 4 arguments that can be passed in batchmode:
-enableCodeCoverage, to enable code coverage.
-coverageResultsPath (optional), to set the location where the coverage results and report will be saved to. The default location is the project's path.
-coverageHistoryPath (optional), to set the location where the coverage report history will be saved to. The default location is the project's path.
-coverageOptions (optional), to pass extra options. Options are separated by semicolon. Some shells use semicolons to separate commands. Therefore, to ensure that coverage options are parsed correctly, enclose them in quotation marks.
Coverage Option | Description |
---|---|
generateHtmlReport |
Add this to generate a coverage HTML report. |
generateHtmlReportHistory |
Add this to generate and include the coverage history in the HTML report. |
generateBadgeReport |
Add this to generate coverage summary badges in SVG and PNG format. |
generateAdditionalMetrics |
Add this to generate and include additional metrics in the HTML report. These currently include Cyclomatic Complexity and Crap Score calculations for each method. See the Risk Hotspots section for more information. |
verbosity |
Add this to set the verbosity level of the log messages. The default value is info .Values: verbose , info , warning , error , off |
useProjectSettings |
Add this to use the settings specified in ProjectSettings/Settings.json instead. Any options passed in the command line will override this. This option can only be used in batchmode and it does not take effect when running the editor from the command line in non-batchmode. |
sourcePaths |
Add this to specify the source directories which contain the corresponding source code. The source directories are used by the report generator when the path information of classes cannot be determined. This is a comma separated string. Globbing is not supported. Example: See Generate combined report from separate projects. |
assemblyFilters |
Add this to specify the assemblies that should be included or excluded in the coverage calculation and/or report. This is a comma separated string. Prefix assemblies with + to include them and with - to exclude them. Globbing can be used to filter the assemblies.Available aliases: <user> maps to the assemblies under the Assets folder.<project> maps to all the assemblies in the project.<packages> maps to the Packages' assemblies in the project, including the built-in packages.By default, if there are no included assemblies specified, only the assemblies under the Assets folder will be included. Examples: assemblyFilters:+<project> will include code from all the assemblies in the project.assemblyFilters:+my.assembly will only include code from the assembly called my.assembly.assemblyFilters:+unity.* will include code from any assembly whose name starts with unity.assemblyFilters:-*unity* will exclude code from all assemblies that contain the word unity in their names.assemblyFilters:+my.assembly.*,-my.assembly.tests will include code from any assembly whose name starts with my.assembly., but will explicitly exclude code from the assembly called my.assembly.tests.assemblyFilters:+my.locale.?? will only inlcude code from assemblies whose names match this format, e.g. my.locale.en, my.locale.99, etc.assemblyFilters:+my.assembly.[a-z][0-9] will only inlcude code from assemblies whose names match this format, e.g. my.assembly.a1, my.assembly.q7, etc. |
pathFilters |
Add this to specify the paths that should be included or excluded in the coverage calculation and/or report. This is a comma separated string. Prefix paths with + to include them and with - to exclude them. Globbing can be used to filter the paths.Currently, only absolute paths are supported. However, you can use globbing to make them shorter e.g. **/Assets/Scripts/ . We are exploring allowing relative paths in a future release.Note: If pathFilters are specified and there are no included assemblies specified in assemblyFilters , then all the assemblies in the project are included in order for path filtering to take precedence over assembly filtering.Examples: pathFilters:+C:/MyProject/Assets/MyClass.cs will only include the MyClass.cs file.pathFilters:+C:/MyProject/Assets/Scripts/* will include all files in the C:/MyProject/Assets/Scripts folder. Files in subfolders will not be included.pathFilters:-C:/MyProject/Assets/AutoGenerated/** will exclude all files under the C:/MyProject/Assets/AutoGenerated folder and any of its subfolders.pathFilters:+**/Assets/Editor/** will include just the files that have /Assets/Editor/ in their path.pathFilters:+C:/MyProject/Assets/**/MyClass.cs will include any file named MyClass.cs that is under the C:/MyProject/Assets folder and any of its subfolders.pathFilters:+C:/MyProject/**,-**/Packages/** will only include files under C:/MyProject/ folder and exclude all files under any Packages folder.pathFilters:+**/MyGeneratedClass_??.cs will include only files with filenames that match this format, i.e. MyGeneratedClass_01.cs, MyGeneratedClass_AB.cs, etc.pathFilters:+**/MyClass_[A-Z][0-9].cs will include only files with filenames that match this format, i.e. MyClass_A1.cs, MyClass_Q7.cs, etc. |
pathStrippingPatterns |
Add this to strip specific sections from the paths that are stored in the coverage results xml files. This is a comma separated string. Globbing is supported. You can change the file paths in the coverage results xml to relative paths so that coverage data generated on different machines can be merged into a single report. Use the pathStrippingPatterns option in conjunction with the sourcePaths option to specify the source directories which contain the corresponding source code. For more information see Generate combined report from separate projects.Note: The OpenCover results xml format specifies file paths as absolute paths ( fullPath ). Changing these paths to relative paths will invalidate the OpenCover standard format. When the results xml files are fed into other tools, these may not work as expected if the paths are relative.Examples: pathStrippingPatterns:C:/MyProject/ will store the path as Assets/Scripts/MyScript.cs, when the original path is C:/MyProject/Assets/Scripts/MyScript.cspathStrippingPatterns:**Assets/ will store the path as Scripts/MyScript.cs, when the original path is C:/MyProject/Assets/Scripts/MyScript.cspathStrippingPatterns:C:/*/Assets/ will store the path as Scripts/MyScript.cs, when the original path is C:/MyProject/Assets/Scripts/MyScript.cspathStrippingPatterns:C:/MyProject??/ will store the path as Assets/Scripts/MyScript.cs, when the original path is C:/MyProject01/Assets/Scripts/MyScript.cspathStrippingPatterns:**/MyProject[A-Z][0-9]/ will store the path as Assets/Scripts/MyScript.cs, when the original path is C:/MyProjectA1/Assets/Scripts/MyScript.cs |
Example
Unity.exe -projectPath <path-to-project> -batchmode -testPlatform editmode -runTests -testResults
<path-to-results-xml> -debugCodeOptimization
-enableCodeCoverage
-coverageResultsPath <path-to-coverage-results>
-coverageHistoryPath <path-to-coverage-history>
-coverageOptions "generateAdditionalMetrics;generateHtmlReport;generateHtmlReportHistory;generateBadgeReport;
assemblyFilters:+my.assembly.*,+<packages>;
pathFilters:-**/Tests/**,-**/BuiltInPackages/**;
verbosity:verbose"
The example above will open the project at <path-to-project>, run the EditMode tests and produce an HTML coverage report and coverage summary badges in <path-to-coverage-results>. The report will include the coverage history, Cyclomatic Complexity and Crap Score calculations. The coverage history files will be saved in <path-to-coverage-history>.
Additionally, the report will include code from any assembly whose name starts with my.assembly., as well as include code from all the Packages' assemblies, but will exclude files that have /Tests/ in their path (i.e. all the files under the Tests folder) and also exclude files that have /BuiltInPackages/ in their path (i.e. all the built-in packages).
Note: -debugCodeOptimization
is passed above to ensure Code optimization is set to Debug mode. See Using Code Coverage with Code Optimization.
Generate combined report from EditMode and PlayMode tests
To get coverage information for both EditMode and PlayMode tests, run the editor three times as shown in the example below:
Unity.exe -projectPath <path-to-project> -batchmode -testPlatform editmode -runTests -debugCodeOptimization -enableCodeCoverage -coverageResultsPath <path-to-coverage-results>
-coverageOptions "generateAdditionalMetrics;assemblyFilters:+my.assembly.*"
Unity.exe -projectPath <path-to-project> -batchmode -testPlatform playmode -runTests -debugCodeOptimization -enableCodeCoverage -coverageResultsPath <path-to-coverage-results>
-coverageOptions "generateAdditionalMetrics;assemblyFilters:+my.assembly.*"
Unity.exe -projectPath <path-to-project> -batchmode -debugCodeOptimization -enableCodeCoverage -coverageResultsPath <path-to-coverage-results>
-coverageOptions "generateHtmlReport;generateBadgeReport;assemblyFilters:+my.assembly.*" -quit
The first will generate the coverage results for the EditMode tests, the second will generate the coverage results for the PlayMode tests and the third will generate the coverage report and summary badges based on both coverage results.
Generate combined report from separate projects
To get a coverage report for your shared code which is used on separate projects, run the tests for each project making sure the -coverageResultsPath points to a separate location inside a shared root folder as shown in the example below:
Unity.exe -projectPath C:/MyProject -batchmode -testPlatform playmode -runTests -debugCodeOptimization -enableCodeCoverage -coverageResultsPath C:/CoverageResults/MyProject
-coverageOptions "generateAdditionalMetrics;assemblyFilters:+my.assembly.*;pathStrippingPatterns:C:/MyProject/"
Unity.exe -projectPath C:/MyOtherProject -batchmode -testPlatform playmode -runTests -debugCodeOptimization -enableCodeCoverage -coverageResultsPath C:/CoverageResults/MyOtherProject
-coverageOptions "generateAdditionalMetrics;assemblyFilters:+my.assembly.*;pathStrippingPatterns:C:/MyOtherProject/"
Unity.exe -projectPath C:/MyProject -batchmode -debugCodeOptimization -enableCodeCoverage -coverageResultsPath C:/CoverageResults
-coverageOptions "generateHtmlReport;generateBadgeReport;assemblyFilters:+my.assembly.*;sourcePaths:C:/MyProject" -quit
The first run generates the coverage results for the PlayMode tests for MyProject and stores these in C:/CoverageResults/MyProject. The second run generates the coverage results for the PlayMode tests for MyOtherProject and stores these in C:/CoverageResults/MyOtherProject. The third run generates the coverage report and summary badges based on the results found under the common C:/CoverageResults folder.