Unity-jump-proj
@ -0,0 +1,29 @@
|
||||
# Code Coverage window
|
||||
|
||||

|
||||
|
||||
|**Toolbar**|**Description**|
|
||||
|:---|:---|
|
||||
||Select **Start Recording** to start recording code coverage data and **Stop Recording** to stop recording.|
|
||||
||Select **Pause Recording** to pause recording code coverage data and **Resume Recording** to resume recording. The buttons are disabled if not in a [Coverage Recording](CoverageRecording.md) session.|
|
||||
|Generate Report|Select **Generate Report** to generate a coverage report from the last set of tests that were run in the [Test Runner](CoverageTestRunner.md) or from the last [Coverage Recording](CoverageRecording.md) session. Note that **Generate Report** is disabled if no tests ran, there is no Coverage Recording data or all **HTML Report**, **Additional Reports** and **Summary Badges** checkboxes are unchecked.|
|
||||
|Clear Results|Select **Clear Results** to clear the coverage data from previous test runs or from previous [Coverage Recording](CoverageRecording.md) sessions. **Clear Results** is disabled if the coverage results are cleared, if no tests ran, or if there is no Coverage Recording data.|
|
||||
|Clear History|Select **Clear History** to clear the coverage report history. **Clear History** is disabled if the history is cleared or if no reports were generated.|
|
||||
||Select **Help** to open the Documentation Reference for Code Coverage in the web browser.|
|
||||
|**Settings**|**Description**|
|
||||
|Enable Code Coverage|Check this to enable Code Coverage. This is required in order to generate Coverage data and reports. Note that Code Coverage can affect the Editor performance.|
|
||||
|Results Location|Select the dropdown to open or specify the folder where the coverage results and report are saved to, and to reset to the default location. The default location is the Project's folder.|
|
||||
|Report History Location|Select the dropdown to open or specify the folder where the coverage report history is saved to, and to reset to the default location. The default location is the Project's folder.|
|
||||
|Included Assemblies|Specify assemblies to be included in the coverage results. This is a dropdown list containing the available assemblies. Click the dropdown to view, select or deselect the assemblies. Select **All** to select all the assemblies in the project. Select **Assets** to select only the assemblies under the `Assets` folder. Select **Packages** to select only the Packages' assemblies. Select **Deselect All** to deselect all the assemblies.</br></br>**Note:** If searching, the buttons will apply only to the assemblies visible in the list.|
|
||||
|Included Paths|Select **Add (+)** to specify individual folders and files to include in coverage results. You can use globbing to filter the paths. If the list is empty, Unity includes all files in the **Included Assemblies**. To remove an individual list entry, select the entry and then select **Remove (-)**.|
|
||||
|Excluded Paths|Select **Add (+)** to specify individual folders and files to exclude from coverage results. You can use globbing to filter the paths. To remove an individual list entry, select the entry and then select **Remove (-)**.|
|
||||
|Log Verbosity Level|Click the dropdown to set the verbosity level for the editor and console logs. The default level is `Info`.<br/><br/>**Levels:**<br/>`Verbose` All logs will be printed.<br/>`Info` Logs, Warnings and Errors will be printed.<br/>`Warning` Warnings and Errors will be printed.<br/>`Error` Only Errors will be printed.<br/>`Off` No logs will be printed.|
|
||||
|**Report Options**|**Description**|
|
||||
|HTML Report|Check this to generate an [HTML report](HowToInterpretResults.md).|
|
||||
|Additional Reports|Check this to generate [SonarQube](https://docs.sonarqube.org/latest/analysis/generic-test), [Cobertura](https://cobertura.github.io/cobertura) and [LCOV](https://github.com/linux-test-project/lcov) reports.|
|
||||
|Report History|Check this to generate and include the coverage history in the HTML report.|
|
||||
|Summary Badges|Check this to generate coverage summary badges in SVG and PNG format.|
|
||||
|Additional Metrics|Check 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](HowToInterpretResults.md#risk-hotspots) section for more information.|
|
||||
|Test Runner References|Check this to include test references to the generated coverage results and enable the [Coverage by test methods](HowToInterpretResults.md#coverage-by-test-methods) section in the HTML report. This shows how each test contributes to the overall coverage.|
|
||||
|Auto Generate Report|Check this to generate the report automatically after the [Test Runner](CoverageTestRunner.md) finishes running or the [Coverage Recording](CoverageRecording.md) session is complete.|
|
||||
|Auto Open Report|Check this to open the coverage report automatically after it has been generated.|
|
@ -0,0 +1,97 @@
|
||||
# Using Code Coverage in batchmode
|
||||
|
||||
You can pass the following arguments in batchmode:
|
||||
|
||||
**-enableCodeCoverage**, to enable code coverage.
|
||||
**-coverageResultsPath** (optional), to set the location where the coverage results and report are saved to. The default location is the project's path.
|
||||
**-coverageHistoryPath** (optional), to set the location where the coverage report history is 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](HowToInterpretResults.md).|
|
||||
|`generateHtmlReportHistory`|Add this to generate and include the coverage history in the HTML report.|
|
||||
|`generateAdditionalReports`|Add this to generate [SonarQube](https://docs.sonarqube.org/latest/analysis/generic-test), [Cobertura](https://cobertura.github.io/cobertura) and [LCOV](https://github.com/linux-test-project/lcov) reports.|
|
||||
|`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](HowToInterpretResults.md#risk-hotspots) section for more information.|
|
||||
|`generateTestReferences`|Add this to include test references to the generated coverage results and enable the [Coverage by test methods](HowToInterpretResults.md#coverage-by-test-methods) section in the HTML report. This shows how each test contributes to the overall coverage.|
|
||||
|`verbosity`|Add this to set the verbosity level for the editor and console logs. The default value is `info`.<br/>**Values:** `verbose`, `info`, `warning`, `error`, `off`|
|
||||
|`useProjectSettings`|Add this to use the settings specified in `ProjectSettings/Packages/com.unity.testtools.codecoverage/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.|
|
||||
|`dontClear`|Add this to allow coverage results to be accumulated after every code coverage session. If not passed the results are cleared before a new session. For more information see [Generate combined report from EditMode and PlayMode tests](#generate-combined-report-from-editmode-and-playmode-tests).|
|
||||
|`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.<br/><br/>**Example:** See [Generate combined report from separate projects](#generate-combined-report-from-separate-projects).|
|
||||
|`assemblyFilters`|Add this to specify the assemblies to include or exclude in the coverage calculation and/or report. This is a comma-separated string. Prefix assemblies with `+` to include them or with `-` to exclude them. Globbing can be used to filter the assemblies.<br/><br/>**Available aliases:**<br/><br/>`<all>` maps to all the assemblies in the project.<br/>`<assets>` maps to the assemblies under the `Assets` folder.<br/>`<packages>` maps to the Packages' assemblies in the project, including the built-in packages.<br/><br/>**By default, if there are no included assemblies specified, only the assemblies under the `Assets` folder will be included.**<br/><br/>**Examples:**<br/><br/>`assemblyFilters:+<all>` will include code from all the assemblies in the project.<br/>`assemblyFilters:+my.assembly` will only include code from the assembly called `my.assembly`.<br/>`assemblyFilters:+unity.*` will include code from any assembly whose name starts with `unity.`<br/>`assemblyFilters:-*unity*` will exclude code from all assemblies that contain the word `unity` in their names.<br/>`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`.<br/>`assemblyFilters:+my.locale.??` will only include code from assemblies whose names match this format, e.g. `my.locale.en`, `my.locale.99`, etc.<br/>`assemblyFilters:+my.assembly.[a-z][0-9]` will only include 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.<br/><br/>Both absolute and relative paths are supported. Absolute paths can be shortened using globbing e.g. `**/Assets/Scripts/`. Relative paths require the `sourcePaths` option to be set. See [Using relative paths in path filters](#using-relative-paths-in-path-filters).<br/><br/>**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`.<br/><br/><br/>**Examples:**<br/><br/>`pathFilters:+C:/MyProject/Assets/MyClass.cs` will only include the `MyClass.cs` file.<br/>`pathFilters:+C:/MyProject/Assets/Scripts/*` will include all files in the `C:/MyProject/Assets/Scripts` folder. Files in subfolders will not be included.<br/>`pathFilters:-C:/MyProject/Assets/AutoGenerated/**` will exclude all files under the `C:/MyProject/Assets/AutoGenerated` folder and any of its subfolders.<br/>`pathFilters:+**/Assets/Editor/**` will include just the files that have `/Assets/Editor/` in their path.<br/>`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.<br/>`pathFilters:+C:/MyProject/**,-**/Packages/**` will only include files under `C:/MyProject/` folder and exclude all files under any `Packages` folder.<br/>`pathFilters:+**/MyGeneratedClass_??.cs` will include only files with filenames that match this format, i.e. `MyGeneratedClass_01.cs`, `MyGeneratedClass_AB.cs`, etc.<br/>`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.|
|
||||
|`pathFiltersFromFile`|Add this to specify the file to read path filtering rules from. Instead of defining all path filtering rules directly in the command line, as you would with the `pathFilters` option, this allows you to store them in a separate file, making your commands clearer and easier to manage.<br/><br/>Like with the `pathFilters` option, `pathFiltersFromFile` also supports relative paths. See [Using relative paths in path filters](#using-relative-paths-in-path-filters).<br/><br/>**Examples:**<br/><br/>`pathFiltersFromFile:C:/MyProject/FilteringRules.txt` will read rules from a file located in `C:/MyProject/FilteringRules.txt`<br/>`pathFiltersFromFile:FilteringRules.txt` will read rules from `FilteringRules.txt` located in the root of your project.<br/><br/>Syntax of the rules is the same as with the `pathFilters` option, however, rules should be listed in separate lines in the file.<br/><br/>**File example:**<br/><br/>This will include all the files in the `Scripts` folder and exclude all the files in the `Scripts/Generated` folder<pre><code>+**/Scripts/**<br/>-**/Scripts/Generated/**</code></pre><br/>**Note:** The `pathFiltersFromFile` option will be deprecated in the next package major release. Please use the `filtersFromFile` option instead.|
|
||||
|`filtersFromFile`|Add this to specify the json file to read path and assembly filtering rules from. Instead of defining all filtering rules directly in the command line, as you would with `pathFilters` and `assemblyFilters` options, this allows you to store them in a separate file, making your commands clearer and easier to manage.<br/><br/>Like with the `pathFilters` option, `filtersFromFile` also supports relative paths. See [Using relative paths in path filters](#using-relative-paths-in-path-filters).<br/><br/>**Examples:**<br/><br/>`filtersFromfile:C:/MyProject/FilteringRules.json` will read rules from a file located in `C:/MyProject/FilteringRules.json`.<br/>`filtersFromFile:FilteringRules.json` will read rules from `FilteringRules.json` located in the root of your project.<br/><br/>**File example:**<br/><br/>This will include the `my.included.assembly`, exclude `my.excluded.assembly` and all assemblies with `unity` in their name. It will also include all files in the `Scripts` folder, and exclude all files in the `Scripts/Generated` folder<pre><code>{<br/> "assembliesInclude": [<br/> "my.included.assembly"<br/> ],<br/> "assembliesExclude": [<br/> "my.excluded.assembly",<br/> "*unity*"<br/> ],<br/> "pathsInclude": [<br/> "**/Scripts/**"<br/> ],<br/> "pathsExclude": [<br/> "**/Scripts/Generated/**"<br/> ]</br>}</pre></code><br/>**Note:** The `pathFiltersFromFile` option will be deprecated in the next package major release. Please use the `filtersFromFile` option instead.|
|
||||
|`pathReplacePatterns`|Add this to replace specific sections from the paths that are stored in the coverage results xml files. This is a comma separated string and requires elements to be passed in pairs i.e. `pathReplacePatterns:from,to,from,to`. Globbing is supported.<br/><br/>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 `pathReplacePatterns` 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](#generate-combined-report-from-separate-projects).<br/><br/>**Note:** The [OpenCover](https://github.com/OpenCover/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.<br/><br/>**Examples:**<br/><br/>`pathReplacePatterns:C:/MyProject,C:/MyOtherProject` will store the path as `C:/MyOtherProject/Assets/Scripts/MyScript.cs`, when the original path is `C:/MyProject/Assets/Scripts/MyScript.cs`<br/>`pathReplacePatterns:@*,,**/PackageCache/,Packages/` will store the path as `Packages/com.unity.my.package/Editor/MyScript.cs`, when the original path is `C:/Project/Library/PackageCache/com.unity.my.package@12345/Editor/MyScript.cs`<br/>`pathReplacePatterns:C:/MyProject/,` will store the path as `Assets/Scripts/MyScript.cs`, when the original path is `C:/MyProject/Assets/Scripts/MyScript.cs`<br/>`pathReplacePatterns:**Assets/,` will store the path as `Scripts/MyScript.cs`, when the original path is `C:/MyProject/Assets/Scripts/MyScript.cs`<br/>`pathReplacePatterns:C:/*/Assets/,` will store the path as `Scripts/MyScript.cs`, when the original path is `C:/MyProject/Assets/Scripts/MyScript.cs`<br/>`pathReplacePatterns:C:/MyProject??/,` will store the path as `Assets/Scripts/MyScript.cs`, when the original path is `C:/MyProject01/Assets/Scripts/MyScript.cs`<br/>`pathReplacePatterns:**/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/**"
|
||||
```
|
||||
The example above opens the project at `\<path-to-project\>`, runs the `EditMode` tests and produces an HTML coverage report and coverage summary badges in `\<path-to-coverage-results\>`. The report includes the coverage history, Cyclomatic Complexity and Crap Score calculations. The coverage history files are saved in `\<path-to-coverage-history\>`.
|
||||
|
||||
Additionally, the report includes code from any assembly whose name starts with `my.assembly.`, and includes code from all the Packages' assemblies. It excludes files that have `/Tests/` in their path (i.e. all the files under the Tests folder) and also excludes 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](UsingCodeCoverage.md#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.*;dontClear"
|
||||
|
||||
Unity.exe -projectPath <path-to-project> -batchmode -testPlatform playmode -runTests -debugCodeOptimization -enableCodeCoverage -coverageResultsPath <path-to-coverage-results>
|
||||
-coverageOptions "generateAdditionalMetrics;assemblyFilters:+my.assembly.*;dontClear"
|
||||
|
||||
Unity.exe -projectPath <path-to-project> -batchmode -debugCodeOptimization -enableCodeCoverage -coverageResultsPath <path-to-coverage-results>
|
||||
-coverageOptions "generateHtmlReport;generateBadgeReport;assemblyFilters:+my.assembly.*" -quit
|
||||
```
|
||||
The first generates the coverage results for the EditMode tests, the second generates the coverage results for the PlayMode tests and the third generates the coverage report and summary badges based on both coverage results.<br/><br/>**Note:** In [Unity Test Framework 2.0](https://docs.unity3d.com/Packages/com.unity.test-framework@2.0) and above the coverage results from both the EditMode and PlayMode test runs are stored in the `Automated` folder. In this example, passing the `dontClear` coverage option ensures that the results from the EditMode test run are not cleared during the PlayMode test run.
|
||||
|
||||
## 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.*;pathReplacePatterns:C:/MyProject/,"
|
||||
|
||||
Unity.exe -projectPath C:/MyOtherProject -batchmode -testPlatform playmode -runTests -debugCodeOptimization -enableCodeCoverage -coverageResultsPath C:/CoverageResults/MyOtherProject
|
||||
-coverageOptions "generateAdditionalMetrics;assemblyFilters:+my.assembly.*;pathReplacePatterns: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.
|
||||
|
||||
## Using relative paths in path filters ##
|
||||
|
||||
When the `sourcePaths` option is specified, the path filtering rules set by the `pathFilters`, `pathFiltersFromFile` and `filtersFromFile` options can be defined as relative paths.
|
||||
|
||||
**Example:**
|
||||
|
||||
```
|
||||
Unity.exe -projectPath C:/MyProject -batchmode -testPlatform playmode -runTests -debugCodeOptimization -enableCodeCoverage -coverageResultsPath C:/CoverageResults/MyProject
|
||||
-coverageOptions "generateHtmlReport;generateAdditionalMetrics;assemblyFilters:+<all>;pathFiltersFromFile:FilteringRules.txt;sourcePaths:C:/MyProject/Assets"
|
||||
```
|
||||
|
||||
`FilteringRules.txt`
|
||||
```
|
||||
+Scripts/Animation/**
|
||||
-**/Generated/**
|
||||
+C:/MyPackages/com.my.company.mypackage/**
|
||||
```
|
||||
|
||||
This example contains three rules:
|
||||
* `+Scripts/Animation/**` - because the `sourcePaths` option was set and this is a relative path, this rule will include all the scripts in the `C:/MyProject/Assets/Scripts/Animation` folder and its subfolders.
|
||||
* `-**/Generated/**` - excludes all the files that have `/Generated/` in their path. This is not a relative path so the `sourcePaths` option has no effect.
|
||||
* `+C:/MyPackages/com.my.company.mypackage/**` - includes all the scripts located in the package outside of the project. This is an absolute path so the `sourcePaths` option has no effect.
|
@ -0,0 +1,34 @@
|
||||
# On-demand coverage recording
|
||||
|
||||
With Coverage Recording you can capture coverage data on demand and generate an [HTML report](HowToInterpretResults.md) which shows which lines of your code run while recording. It supports capturing in EditMode as well as in PlayMode, and you can switch between the two.
|
||||
|
||||
To start recording coverage data, select **Start Recording**. While recording, use the Editor as usual, for example to enter PlayMode. To stop recording coverage data, select **Stop Recording**. If **Auto Generate Report** is checked, then an [HTML report](HowToInterpretResults.md) is generated and a file viewer window opens (if **Auto Open Report** is checked too). It contains the coverage results and the report. Otherwise, select **Generate Report** to generate the report. The results are based on the assemblies specified in **Included Assemblies**.
|
||||
|
||||
You can also control Coverage Recording via the [CodeCoverage ScriptingAPI](https://docs.unity3d.com/Packages/com.unity.testtools.codecoverage@latest/index.html?subfolder=/api/UnityEditor.TestTools.CodeCoverage.CodeCoverage.html).
|
||||
|
||||
## Steps
|
||||
|
||||
1. Open the [Code Coverage window](CodeCoverageWindow.md) (go to **Window** > **Analysis** > **Code Coverage**).<br/><br/>
|
||||

|
||||
|
||||
2. Select **Enable Code Coverage** if not already selected, to be able to generate Coverage data and reports.<br/>
|
||||
<br/>**Note:** Enabling Code Coverage adds some overhead to the Editor and can affect the performance.
|
||||
|
||||
3. Select the [Assembly Definitions](https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html) you would like to see the coverage for. In this example we selected `Assembly-CSharp` and `Assembly-CSharp-Editor`. By default, Unity compiles almost all project scripts into the `Assembly-CSharp.dll` managed assembly and all Editor scripts into the `Assembly-CSharp-Editor.dll` managed assembly.<br/><br/>
|
||||

|
||||
|
||||
4. Select **Start Recording**.<br/>
|
||||

|
||||
|
||||
5. Continue using the Editor as normal, for example enter PlayMode to test your application or run some manual testing. You can also select **Pause Recording** to pause recording and **Resume Recording** to resume recording.
|
||||
|
||||
6. When you have finished your testing and have collected enough coverage data, select **Stop Recording**.<br/>
|
||||

|
||||
|
||||
7. If **Auto Open Report** is checked a file viewer window opens containing the coverage report. Alternatively, select the **Results Location** dropdown to open it in the file viewer.<br/><br/>**Note:** To generate the report automatically after you stop recording, select **Auto Generate Report** in the [Code Coverage window](CodeCoverageWindow.md). Alternatively, you can select **Generate Report**.<br/>
|
||||
|
||||
8. Select `index.htm`.<br/><br/>
|
||||

|
||||
|
||||
9. This opens the [HTML coverage report](HowToInterpretResults.md).<br/><br/>
|
||||

|
@ -0,0 +1,55 @@
|
||||
# Using Code Coverage with Test Runner
|
||||
|
||||
When running your tests in the [Test Runner](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/manual/workflow-run-test.html) you can generate an [HTML report](HowToInterpretResults.md) which shows which lines of your code the tests cover. This includes both `EditMode` and `PlayMode` tests.
|
||||
|
||||
If **Auto Generate Report** is checked, then an [HTML report](HowToInterpretResults.md) is generated and a file viewer window opens (if **Auto Open Report** is checked too). It contains the coverage results and the report. Otherwise, select **Generate Report** to generate the report. The results are based on the assemblies specified in **Included Assemblies**.
|
||||
|
||||
## Steps
|
||||
|
||||
1. Open the [Code Coverage window](CodeCoverageWindow.md) (go to **Window** > **Analysis** > **Code Coverage**).<br/><br/>
|
||||

|
||||
|
||||
2. Select **Enable Code Coverage** if not already selected, to be able to generate Coverage data and reports.<br/>
|
||||
<br/>**Note:** Enabling Code Coverage adds some overhead to the Editor and can affect the performance.
|
||||
|
||||
3. Select the [Assembly Definitions](https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html) you would like to see the coverage for. In this example we selected `Assembly-CSharp` and `Assembly-CSharp-Editor`. By default, Unity compiles almost all project scripts into the `Assembly-CSharp.dll` managed assembly and all Editor scripts into the `Assembly-CSharp-Editor.dll` managed assembly.<br/><br/>
|
||||

|
||||
|
||||
4. Switch to the [Test Runner](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/manual/workflow-run-test.html) and run your `EditMode` and/or `PlayMode` test(s).<br/><br/>
|
||||

|
||||
|
||||
Example test:
|
||||
```
|
||||
using NUnit.Framework;
|
||||
using Assert = UnityEngine.Assertions.Assert;
|
||||
|
||||
public class EditorTests
|
||||
{
|
||||
[Test]
|
||||
public void MyPublicClass_PublicFunctionCanBeCalled()
|
||||
{
|
||||
MyPublicClass myPublicClass = new MyPublicClass();
|
||||
Assert.IsTrue(myPublicClass.MyPublicFunction());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
5. When the test(s) finish running, a file viewer window opens containing the coverage report. Alternatively, select the **Results Location** dropdown to open it in the file viewer.<br/><br/>**Note:** To generate the report automatically after the Test Runner has finished running the tests, select **Auto Generate Report** in the [Code Coverage window](CodeCoverageWindow.md). Alternatively, you can select **Generate Report**.<br/>
|
||||
|
||||
6. Select `index.htm`.<br/><br/>
|
||||

|
||||
<br/><br/>
|
||||
This opens the [HTML coverage report](HowToInterpretResults.md).<br/><br/>
|
||||

|
||||
<br/>
|
||||
|
||||
## Get results for EditMode and PlayMode tests
|
||||
|
||||
Coverage data are generated from the last set of tests that were run in the [Test Runner](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/manual/workflow-run-test.html).<br/><br/>
|
||||
**Note:** Currently the [Test Runner](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/manual/workflow-run-test.html) does not support `EditMode` and `PlayMode` tests running at the same time. In [version 2.0](https://docs.unity3d.com/Packages/com.unity.test-framework@2.0/manual/whats-new.html#added) of the Test Framework this will be possible. In the meantime, to include coverage for both `EditMode` and `PlayMode` tests, you must run these separately. In this case, the last Coverage Report generated will include the combined coverage of `EditMode` and `PlayMode` tests.
|
||||
|
||||
If a fresh start is required, select **Clear Results** to clear the Coverage data from all previous test runs for both `EditMode` and `PlayMode` tests.
|
||||
|
||||
## Get coverage by test methods
|
||||
|
||||
To see how each test contributes to the overall coverage check **Test Runner References**. For more details see [Coverage by test methods](HowToInterpretResults.md#coverage-by-test-methods).
|
@ -0,0 +1,21 @@
|
||||
# Document revision history - Archive
|
||||
|
||||
|Date|Reason|
|
||||
|:---|:---|
|
||||
|Feb 26, 2021|<li>Added information about the *Enable Code Coverage* setting in the Code Coverage window<li>Updated *Installing Code Coverage* section<li>Added *verbosity* section in *-coverageOptions*<li>Added *Generate combined report from separate projects* section<li>Matches package version *1.0.0-pre.4*|
|
||||
|Jan 13, 2021|<li>Added information about *assemblyFilters* aliases and note about how *path filtering* can take precedence over *assembly filtering* in *Using Code Coverage in batchmode* section<li>Matches package version *1.0.0-pre.2*|
|
||||
|Nov 12, 2020|<li>Matches package version *1.0.0-pre.1*|
|
||||
|Nov 11, 2020|<li>Added information about *Included Paths* and *Excluded Paths* settings in the Code Coverage window<li>Updated information about *Included Assemblies* setting in the Code Coverage window<li>Matches package version *0.4.0-preview*|
|
||||
|Aug 3, 2020|<li>Matches package version *0.3.1-preview*|
|
||||
|May 20, 2020|<li>Added information about the *History Location* and *Generate History* settings in the Code Coverage window and *coverageHistoryPath* and *generateHtmlReportHistory* in *-coverageOptions*<li>Added information about the *Generate Additional Metrics* setting in the Code Coverage window and *generateAdditionalMetrics* in *-coverageOptions*<li>Added information about *Coverage History* and *Crap Score* in *How to interpret the results* page<li>Updated *Installing Code Coverage* section<li>Matches package version *0.3.0-preview*|
|
||||
|Feb 18, 2020|<li>Added *Generate combined report from EditMode and PlayMode tests* section<li>Matches package version *0.2.3-preview*|
|
||||
|Dec 11, 2019|<li>Matches package version *0.2.2-preview*|
|
||||
|Dec 3, 2019|<li>Added *pathFilters* section<li>Added examples for *assemblyFilters* and *pathFilters*<li>Added a reference to the *Coverage Recording ScriptingAPI*<li>Matches package version *0.2.1-preview*|
|
||||
|Nov 10, 2019|<li>Split documentation into separate pages<li>Matches package version *0.2.0-preview*|
|
||||
|Nov 5, 2019|<li>Updated UX design|
|
||||
|Sep 27, 2019|<li>Added *Using Code Coverage with Burst compiler* section<li>Added *Using Code Coverage with Code Optimization* section<li>Matches package version *0.1.0-preview.3*|
|
||||
|Sep 23, 2019|<li>Added *Coverage Recording* section<li>Matches package version *0.1.0-preview.2*|
|
||||
|Aug 16, 2019|<li>Added *How to interpret the results* section<li>Added *How it works* section|
|
||||
|Aug 7, 2019|<li>Added *Clear Coverage Data* setting to *Settings* table<li>Added *Note* in *Using Code Coverage* section about combined coverage of EditMode and PlayMode tests|
|
||||
|Aug 5, 2019|<li>Updated *About Code Coverage* section and *Settings* table|
|
||||
|May 15, 2019|<li>Document created. Matches package version *0.1.0-preview.0*|
|
@ -0,0 +1,13 @@
|
||||
# Document revision history
|
||||
|
||||
|Date|Reason|
|
||||
|:---|:---|
|
||||
|Dec 20, 2023|<li>Matches package version *1.2.5*.|
|
||||
|Jun 02, 2023|<li>Corrected *Settings.json* path in **useProjectSettings** section in *-coverageOptions*.<li>Matches package version *1.2.4*.|
|
||||
|Apr 14, 2023|<li>Added **filtersFromFile** section in *-coverageOptions*.<li>Updated **Using Code Coverage with Burst compiler** section with the correct number of dashes for *--burst-disable-compilation*.<li>Matches package version *1.2.3*.|
|
||||
|Nov 18, 2022|<li>Added **Uncoverable lines** definition in **How to interpret the results** page.<li>Matches package version *1.2.2*.|
|
||||
|Oct 27, 2022|<li>Matches package version *1.2.1*.|
|
||||
|Aug 01, 2022|<li>Updated **What's new** and **Upgrade guide** pages.<li>Added information about the buttons in the **toolbar** in the Code Coverage window.<li>Added information about the **Log Verbosity Level** setting in the Code Coverage window.<li>Added information about the **Additional Reports**, **Test Runner References** and **Auto Open Report** options in the Code Coverage window.<li>Introduced new selection buttons under the **Included Assemblies** dropdown in the Code Coverage window.<li>Added **generateTestReferences**, **generateAdditionalReports**, **pathFiltersFromFile** and **dontClear** sections in *-coverageOptions*.<li>Added **pathReplacePatterns** section in *-coverageOptions*.<li>Removed **pathStrippingPatterns** section from *-coverageOptions* (replaced with **pathReplacePatterns**).<li>Renamed the aliases in the **assemblyFilters** command line option.<li>Updated the examples in the **pathFilters** command line option to follow the standard globbing paradigm.<li>Added **Using relative paths in path filters** section.<li>Added **Coverage by test methods** section.<li>Updated the **Quickstart - Code Coverage Tutorial** page.<li>Matches package version *1.2.0*.|
|
||||
|Dec 17, 2021|<li>Added **What's new** and **Upgrade guide** pages.<li>Matches package version *1.1.1*.|
|
||||
|Jun 09, 2021|<li>Added **Subscribing to Code Coverage session events** section.<li>Added **Ignoring tests for Code Coverage** section.<li>Added **useProjectSettings**, **pathStrippingPatterns** and **sourcePaths** sections in *-coverageOptions*.<li>Updated the **Generate combined report from separate projects** section.<li>Matches package version *1.1.0*.|
|
||||
|Mar 09, 2021|<li>Added **Quickstart** guide.<li>Matches package version *1.0.0*.|
|
@ -0,0 +1,64 @@
|
||||
# How to interpret the results
|
||||
|
||||
This section assumes that you checked **HTML Report** in the [Code Coverage window](CodeCoverageWindow.md) or passed the `generateHtmlReport` option in `-coverageOptions` on the [command line](CoverageBatchmode.md). If you're running the tests in the Editor, a file viewer window opens up containing the coverage report once the test run has been finished. If you're running the tests from the command line, navigate to the `-coverageResultsPath` location with your file viewer. Open the `Report` folder then open `index.htm` in a web browser. This shows a summary of the coverage results from the tests.<br/>
|
||||
|
||||

|
||||
|
||||
## Summary view
|
||||
|
||||
The summary view is divided into several sections: Summary, Coverage History, Risk Hotspots and Coverage.
|
||||
|
||||
### Summary
|
||||
|
||||
This section shows a brief summary of the coverage results including the number of assemblies, classes, files and lines that were processed. The most important value is the **Line Coverage** which shows the current coverage percentage of all coverable lines. Coverable lines only include the lines that can be executed and are colored either green or red in the [File(s)](#files) section depending on whether the line was covered or not. Uncoverable lines are the lines that cannot be executed and are not colored in the [File(s)](#files) section; these include lines containing:
|
||||
- Assemblies, Classes, Constructors, Methods and Structs that are marked to be [excluded from coverage](UsingCodeCoverage.md#excluding-code-from-code-coverage)
|
||||
- Directives
|
||||
- Attributes
|
||||
- Method signatures
|
||||
- Class definitions
|
||||
- Unassigned variable declarations
|
||||
- Constant variable declarations
|
||||
- else/case/default keywords
|
||||
- Lines from other classes, when multiple classes are included in the same file
|
||||
- Blank lines
|
||||
|
||||
### Coverage History
|
||||
|
||||
This section only appears if you checked **Report History** in the [Code Coverage window](CodeCoverageWindow.md) or passed the `generateHtmlReportHistory` option in `-coverageOptions` on the [command line](CoverageBatchmode.md). The Coverage History displays a graph showing the total percentage coverage for every test run for this project. Aim to keep this percentage as high as possible. If it is decreasing, consider writing more tests to improve your coverage.
|
||||
|
||||
### Risk Hotspots
|
||||
|
||||
This section only appears if you checked **Additional Metrics** in the [Code Coverage window](CodeCoverageWindow.md) or passed the `generateAdditionalMetrics` option in `-coverageOptions` on the [command line](CoverageBatchmode.md).
|
||||
|
||||
The Risk Hotspots display information about any methods that have a Cyclomatic Complexity score that is greater than 15. The Cyclomatic Complexity score is a value that is based on the number of paths that can be taken through a method. The score will tend to be higher if a method has a large number of `if` or `switch` statements. For more detailed information see the [Wikipedia entry on Cyclomatic Complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity). You will also see information about any methods with a high Crap Score. CRAP stands for Change Risk Anti-Patterns. For more detailed information see this [article](https://testing.googleblog.com/2011/02/this-code-is-crap.html).
|
||||
|
||||
If there are any methods with a very high Cyclomatic Complexity or Crap Score, consider refactoring the method to reduce its complexity.
|
||||
|
||||
**Note:** NPath Complexity calculation and Branch Coverage are not implemented at present so will always appear as zero.
|
||||
|
||||
### Coverage
|
||||
By default, this shows a list of the assemblies that have been covered together with some stats showing how well covered they are. Select **Expand (+)** next to the assembly name to see a list of the classes or structs within the assembly and their associated coverage data. To see more detailed information for a particular class, select its name in the list.
|
||||
|
||||
## Class/Struct view
|
||||
|
||||
### Summary
|
||||
|
||||
Similar to the Summary section of the previous page, this section shows some brief statistics for the selected class. The most important value is the Line Coverage percentage. Select **Back (<)** in the top left hand corner to return to the previous screen.
|
||||
|
||||
### Coverage History
|
||||
|
||||
This section only appears if you checked **Report History** in the [Code Coverage window](CodeCoverageWindow.md) or passed the `generateHtmlReportHistory` option in `-coverageOptions` on the [command line](CoverageBatchmode.md). It shows a graph of the coverage percentage of the class/struct over time. Try to keep this value as high as you can. Make sure that as you add new code the coverage percentage is maintained by adding more tests.
|
||||
|
||||
### Metrics
|
||||
|
||||
The metrics section displays a list of the methods and properties of the class, along with each method's Cyclomatic Complexity, Crap Score and Sequence Coverage scores. Currently, the NPath Complexity and Branch Coverage aren't calculated, so will always appear as zero.
|
||||
|
||||
### File(s)
|
||||
|
||||
The File(s) section shows the C# source code for the selected class. Each line is colored either green or red depending on whether the line was covered or not. The number in the left column indicates the number of times that the line was executed during a Test Runner or Coverage Recording session.
|
||||
|
||||
### Coverage by test methods
|
||||
|
||||
This section only appears if you checked **Test Runner References** in the [Code Coverage window](CodeCoverageWindow.md) or passed the `generateTestReferences` option in `-coverageOptions` on the [command line](CoverageBatchmode.md).
|
||||
|
||||
It shows a list of test methods allowing you to see how each test contributes to the overall coverage. Select a test method to view the relevant code or hover over the code to see which test method executed it.
|
@ -0,0 +1,43 @@
|
||||
# Installing Code Coverage
|
||||
|
||||
Before you install the package, make sure you have no errors in the [Console](https://docs.unity3d.com/Manual/Console.html) window (in red text).
|
||||
|
||||
## From the Unity Package Manager
|
||||
|
||||
Use the [Unity Package Manager](https://docs.unity3d.com/Packages/com.unity.package-manager-ui@latest) to find and install the Code Coverage package.
|
||||
|
||||

|
||||
|
||||
Alternatively, use the **Add (+)** dropdown and select **Add package from git URL...** or **Add package by name...** and type `com.unity.testtools.codecoverage`.
|
||||
|
||||

|
||||
|
||||
To verify that Code Coverage has been installed correctly, open the Code Coverage window (go to **Window** > **Analysis** > **Code Coverage**). If you don't see the **Code Coverage** menu item, then Code Coverage did not install correctly.
|
||||
|
||||
## Manually from the Package Manifest
|
||||
|
||||
You can also install the Code Coverage package manually. To do this, add a reference to Code Coverage in your project's `Packages/manifest.json` file. There are two ways you can reference a specific version of the Code Coverage package, depending on how you use it.
|
||||
|
||||
### Using a production version of the package
|
||||
|
||||
You can point the Package Manager at a publicly available version. To do this manually, add it to `manifest.json`:
|
||||
|
||||
```json
|
||||
"dependencies": {
|
||||
//...
|
||||
"com.unity.testtools.codecoverage":"<full version number>"
|
||||
}
|
||||
```
|
||||
|
||||
### Using a local clone of the package
|
||||
|
||||
If you want to use a cloned version of the package, you can point the Package Manager at a local folder as the package location:
|
||||
|
||||
```json
|
||||
"dependencies": {
|
||||
//...
|
||||
"com.unity.testtools.codecoverage":"file:path/to/package/root"
|
||||
}
|
||||
```
|
||||
|
||||
To verify that Code Coverage has been installed correctly, open the Code Coverage window (go to **Window** > **Analysis** > **Code Coverage**). If you don't see the **Code Coverage** menu item, then Code Coverage did not install correctly.
|
@ -0,0 +1,201 @@
|
||||
# Quickstart - Code Coverage tutorial
|
||||
|
||||
The Quickstart guide will give you an insight into what Code Coverage is and how you can identify areas of your code that need more testing, even if you haven't written any automated tests. It takes about 30 minutes to complete.
|
||||
|
||||

|
||||
|
||||
## Tasks
|
||||
|
||||
1. [What is Code Coverage](#1-what-is-code-coverage-sub2-minsub) (2 min)
|
||||
2. [Install the Code Coverage package](#2-install-the-code-coverage-package-sub2-minsub) (2 min)
|
||||
3. [Install the Asteroids sample project](#3-install-the-asteroids-sample-project-sub1-minsub) (1 min)
|
||||
4. [Enable Code Coverage](#4-enable-code-coverage-sub1-minsub) (1 min)
|
||||
5. [Understanding the game code: Shoot() function](#5-understanding-the-game-code-shoot-function-sub4-minsub) (4 min)
|
||||
6. [Generate a Coverage report from PlayMode tests](#6-generate-a-coverage-report-from-playmode-tests-sub3-minsub) (3 min)
|
||||
7. [Add Weapon tests to improve coverage](#7-add-weapon-tests-to-improve-coverage-sub3-minsub) (3 min)
|
||||
8. [Add a test for the LaserController](#8-add-a-test-for-the-lasercontroller-sub4-minsub) (4 min)
|
||||
9. [Clear the coverage data](#9-clear-the-coverage-data-sub1-minsub) (1 min)
|
||||
10. [Generate a Coverage report using Coverage Recording](#10-generate-a-coverage-report-using-coverage-recording-sub4-minsub) (4 min)
|
||||
|
||||
**Note:** Estimated times are shown for each task to give you a better understanding of the time required. These times are rough guidelines - it is fine to take as much or as little time as needed.
|
||||
|
||||
## 1. What is Code Coverage <sub>(2 min)</sub>
|
||||
|
||||
[Code Coverage](https://en.wikipedia.org/wiki/Code_coverage) is a measure of how much of your code has been executed. It is normally associated with automated tests, but you can gather coverage data in Unity at any time when the Editor is running.
|
||||
|
||||
It is typically presented as a [report](HowToInterpretResults.md) that shows the percentage of the code that has been executed. For automated testing the report does not measure the quality of tests, only whether your code is executed by PlayMode and EditMode tests. It is especially useful to check that critical or high risk areas of your code are covered, because they should receive the most rigorous testing.
|
||||
|
||||
It is much easier to accidentally introduce bugs into code that is not covered by tests, because those bugs are not detected straight away by the tests and can instead cause problems later — such as after you have published your game or app.
|
||||
|
||||
Additionally, the Code Coverage package offers a [Coverage Recording](CoverageRecording.md) feature which allows capturing coverage data on demand, in case you do not have tests in your project or doing manual testing.
|
||||
|
||||
## 2. Install the Code Coverage package <sub>(2 min)</sub>
|
||||
**Note:** Skip this task if the package is already installed.
|
||||
|
||||
Use the [Unity Package Manager](https://docs.unity3d.com/Packages/com.unity.package-manager-ui@latest) to find and install the **Code Coverage** package.
|
||||
|
||||

|
||||
|
||||
Alternatively, use the **Add (+)** dropdown and select **Add package from git URL...** or **Add package by name...** and type `com.unity.testtools.codecoverage`.
|
||||
|
||||

|
||||
|
||||
To verify that Code Coverage has been installed correctly, open the Code Coverage window (go to **Window** > **Analysis** > **Code Coverage**). If you don't see the **Code Coverage** menu item, then Code Coverage did not install correctly.
|
||||
|
||||
## 3. Install the Asteroids sample project <sub>(1 min)</sub>
|
||||
|
||||
1. In the [Unity Package Manager](https://docs.unity3d.com/Packages/com.unity.package-manager-ui@latest) (**Window** > **Package Manager**) select the **Code Coverage** package, if not already selected.
|
||||
2. Find the **Samples** section in the package details (right hand side) and select **Import** next to **Code Coverage Tutorial**.<br/><br/>
|
||||

|
||||
|
||||
## 4. Enable Code Coverage <sub>(1 min)</sub>
|
||||
|
||||
To enable Code Coverage open the [Code Coverage window](CodeCoverageWindow.md) (go to **Window** > **Analysis** > **Code Coverage**) and select **Enable Code Coverage** if not already selected, to be able to generate Coverage data and reports.
|
||||
|
||||

|
||||
|
||||
**Note:** Enabling Code Coverage adds some overhead to the editor and can affect the performance.
|
||||
|
||||
## 5. Understanding the game code: Shoot() function <sub>(4 min)</sub>
|
||||
|
||||
1. Go to `Asteroids/Scenes` in Project View and open the **Asteroids** scene.<br/>
|
||||
This is located in `Assets/Samples/Code Coverage/<version>/Code Coverage Tutorial`.
|
||||
|
||||
2. Hit **Play** and play the game for a minute or two.<br/>
|
||||

|
||||
Use the arrow keys to move and the spacebar to shoot.
|
||||
|
||||
3. Exit PlayMode.
|
||||
|
||||
4. Open the `Scripts/Controllers/SpaceshipController.cs` script.
|
||||
|
||||
5. Study the **Shoot** function.
|
||||
```
|
||||
If Weapon is Basic, the Prefabs/Weapons/Projectile prefab is instantiated
|
||||
If Weapon is Laser, the Prefabs/Weapons/Laser prefab is instantiated
|
||||
```
|
||||
|
||||
## 6. Generate a Coverage report from PlayMode tests <sub>(3 min)</sub>
|
||||
|
||||
1. Open the [Code Coverage window](CodeCoverageWindow.md) (go to **Window** > **Analysis** > **Code Coverage**).<br/><br/>
|
||||

|
||||
|
||||
2. If you see this warning select **Switch to debug mode**.<br/>
|
||||

|
||||
[Code Optimization](https://docs.unity3d.com/2020.1/Documentation/Manual/ManagedCodeDebugging.html#CodeOptimizationMode) was introduced in Unity 2020.1; in _Release mode_ the code is optimized and therefore not directly represented by the original code. Therefore, _Debug mode_ is required in order to obtain accurate code coverage information.
|
||||
|
||||
3. Click the **Included Assemblies** dropdown to make sure only<br/>
|
||||
`Unity.TestTools.CodeCoverage.Sample.Asteroids` and<br/>
|
||||
`Unity.TestTools.CodeCoverage.Sample.Asteroids.Tests` are selected.<br/><br/>
|
||||

|
||||
|
||||
4. Make sure **HTML Report**, **Report History**, **Auto Generate Report** and **Auto Open Report** are all checked.
|
||||

|
||||
|
||||
5. Switch to the [Test Runner](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/manual/workflow-run-test.html) window, select the **PlayMode** tab and hit **Run All** tests.<br/><br/>
|
||||

|
||||
|
||||
6. When the tests finish running, a file viewer window will open up containing the coverage report. Select `index.htm`.
|
||||
|
||||
7. Look for the classes with low coverage, especially **LaserController**, **BaseProjectile** and **ProjectileController**.
|
||||
|
||||
You can sort the results by _Line coverage_.
|
||||
|
||||

|
||||
|
||||
See also [How to interpret the results](HowToInterpretResults.md).
|
||||
|
||||
## 7. Add Weapon tests to improve coverage <sub>(3 min)</sub>
|
||||
|
||||
1. Open the `Tests/WeaponTests.cs` script.
|
||||
|
||||
2. Uncomment all the tests (from _line 35_ down to _line 237_).
|
||||
|
||||
3. Back in the **Test Runner**, hit **Run All** tests again.
|
||||
|
||||
4. When the tests finish running, a file viewer window will open up containing the coverage report. Select `index.htm`.
|
||||
|
||||
5. Notice that now **BaseProjectile** and **ProjectileController** coverage is considerably higher, but **LaserController** has not improved much.
|
||||
|
||||

|
||||
|
||||
## 8. Add a test for the LaserController <sub>(4 min)</sub>
|
||||
|
||||
1. Open the `Tests/WeaponTests.cs` script.
|
||||
|
||||
2. Go to the **\_18\_LaserFiresSuccessfully** test in line 225.
|
||||
|
||||
3. Uncomment and study the code.
|
||||
|
||||
4. Back in the **Test Runner**, hit **Run All** tests again.
|
||||
|
||||
5. When the tests finish running, a file viewer window will open up containing the coverage report. Select `index.htm`.
|
||||
|
||||
6. Notice how the coverage for **LaserController** has improved.<br/>
|
||||

|
||||
|
||||
7. Select the **LaserController** class to enter the class view and notice that about 2/3 (65%) of the code is now covered (green).
|
||||
|
||||

|
||||
|
||||
Complete the [Bonus Task](#11-bonus-task-sub5-8-minsub) at the end of the tutorial to get 100% coverage!
|
||||
|
||||
## 9. Clear the coverage data <sub>(1 min)</sub>
|
||||
|
||||
1. Open the [Code Coverage window](CodeCoverageWindow.md) (go to **Window** > **Analysis** > **Code Coverage**).
|
||||
|
||||
2. Select **Clear Results** and confirm.
|
||||
|
||||
3. Select **Clear History** and confirm.
|
||||
|
||||
## 10. Generate a Coverage report using Coverage Recording <sub>(4 min)</sub>
|
||||
|
||||
1. Go to `Asteroids/Scenes` in Project View and open the **Asteroids** scene, if not opened already.
|
||||
|
||||
2. Open the [Code Coverage window](CodeCoverageWindow.md). Make sure **HTML Report**, **Report History**, **Auto Generate Report** and **Auto Open Report** all are checked.<br/>
|
||||

|
||||
|
||||
3. Select **Start Recording**.<br/>
|
||||

|
||||
|
||||
4. Hit **Play** to play the game and **exit** PlayMode before you get **8000** points.
|
||||

|
||||
|
||||
5. Select **Stop Recording**.<br/>
|
||||

|
||||
|
||||
6. A file viewer window will open up containing the coverage report. Select `index.htm`.
|
||||
|
||||
7. Notice that **LaserController** has 0% coverage.<br/>
|
||||

|
||||
|
||||
8. Go back to the **Code Coverage window**.
|
||||
|
||||
9. Select **Start Recording**.
|
||||
|
||||
10. Now hit **Play** to play the game again but this time **exit** PlayMode when you get **8000** points.
|
||||
|
||||
11. Select **Stop Recording**.
|
||||
|
||||
12. Notice that **LaserController** coverage is now 100%.<br/>
|
||||

|
||||
|
||||
See also [How to interpret the results](HowToInterpretResults.md).
|
||||
<br/><br/>
|
||||
|
||||
## 11. Bonus task <sub>(5-8 min)</sub>
|
||||
|
||||
Write a new test that checks that the laser gets destroyed after 2 seconds, which will also cover the rest of the code in **LaserController**.
|
||||
|
||||
**Suggested name:** _19_LaserFiresAndIsDestroyedAfterTwoSeconds.
|
||||
**Hint:** You can use `yield return new WaitForSeconds(2f);` to wait for 2 seconds.
|
||||
|
||||
<br/>
|
||||
|
||||
---
|
||||
|
||||
<br/>
|
||||
|
||||
### Well done for finishing the Code Coverage tutorial!
|
||||
|
||||
For questions and feedback please visit the [Testing & Automation](https://forum.unity.com/forums/testing-automation.211/) forum section to browse current conversations or start a new thread. Please use the **code coverage** tag.
|
@ -0,0 +1,41 @@
|
||||
* [About Code Coverage](index)
|
||||
* [What's new](whats-new.md)
|
||||
* [Upgrade guide](upgrade-guide.md)
|
||||
* [Code Coverage package](index#code-coverage-package)
|
||||
* [Coverage HTML Report](index#coverage-html-report)
|
||||
* [Other Reports](index#other-reports)
|
||||
* [Coverage Summary Badges](index#coverage-summary-badges)
|
||||
* [Quickstart](Quickstart)
|
||||
* [What is Code Coverage](Quickstart#1-what-is-code-coverage-sub2-minsub)
|
||||
* [Install the Code Coverage package](Quickstart#2-install-the-code-coverage-package-sub2-minsub)
|
||||
* [Install the Asteroids sample project](Quickstart#3-install-the-asteroids-sample-project-sub1-minsub)
|
||||
* [Enable Code Coverage](Quickstart#4-enable-code-coverage-sub1-minsub)
|
||||
* [Understanding the game code: Shoot() function](Quickstart.html#5-understanding-the-game-code-shoot-function-sub4-minsub)
|
||||
* [Generate a Coverage report from PlayMode tests](Quickstart#6-generate-a-coverage-report-from-playmode-tests-sub3-minsub)
|
||||
* [Add Weapon tests to improve coverage](Quickstart#7-add-weapon-tests-to-improve-coverage-sub3-minsub)
|
||||
* [Add a test for the LaserController](Quickstart#8-add-a-test-for-the-lasercontroller-sub4-minsub)
|
||||
* [Clear the coverage data](Quickstart#9-clear-the-coverage-data-sub1-minsub)
|
||||
* [Generate a Coverage report using Coverage Recording](Quickstart#10-generate-a-coverage-report-using-coverage-recording-sub4-minsub)
|
||||
* [Installing Code Coverage](InstallingCodeCoverage)
|
||||
* [From the Unity Package Manager](InstallingCodeCoverage#from-the-unity-package-manager)
|
||||
* [Manually from the Package Manifest](InstallingCodeCoverage#manually-from-the-package-manifest)
|
||||
* [Using Code Coverage](UsingCodeCoverage)
|
||||
* [Code Coverage window](CodeCoverageWindow)
|
||||
* [Using Code Coverage with Test Runner](CoverageTestRunner)
|
||||
* [On demand coverage recording](CoverageRecording)
|
||||
* [Using Code Coverage in batchmode](CoverageBatchmode)
|
||||
* [Using Code Coverage with Burst compiler](UsingCodeCoverage#using-code-coverage-with-burst-compiler)
|
||||
* [Using Code Coverage with Code Optimization](UsingCodeCoverage#using-code-coverage-with-code-optimization)
|
||||
* [Excluding code from Code Coverage](UsingCodeCoverage#excluding-code-from-code-coverage)
|
||||
* [Ignoring tests for Code Coverage](UsingCodeCoverage#ignoring-tests-for-code-coverage)
|
||||
* [Subscribing to Code Coverage session events](UsingCodeCoverage#subscribing-to-code-coverage-session-events)
|
||||
* [How to interpret the results](HowToInterpretResults)
|
||||
* [Summary View](HowToInterpretResults#summary-view)
|
||||
* [Class/Struct View](HowToInterpretResults#classstruct-view)
|
||||
* [Technical details](TechnicalDetails)
|
||||
* [How it works](TechnicalDetails#how-it-works)
|
||||
* [Requirements](TechnicalDetails#requirements)
|
||||
* [3rd party libraries used](TechnicalDetails#3rd-party-libraries-used)
|
||||
* [Known limitations](TechnicalDetails#known-limitations)
|
||||
* [Document revision history](DocumentRevisionHistory)
|
||||
* [Archive](DocumentArchive)
|
@ -0,0 +1,23 @@
|
||||
# Technical details
|
||||
|
||||
## How it works
|
||||
|
||||
The package is a client of the coverage API. For more information, see the [coverage API's documentation](https://docs.unity3d.com/ScriptReference/TestTools.Coverage.html). The package uses a combination of this API and C# reflection to output the test coverage data in the OpenCover format. Optionally, a third-party report generator will then parse the OpenCover data and generate a report (HTML, SonarQube, Cobertura, LCOV).
|
||||
|
||||
## Requirements
|
||||
|
||||
This version of the Code Coverage package is compatible with the following versions of the Unity Editor:
|
||||
|
||||
* 2019.3 and later
|
||||
|
||||
## Third-party libraries used
|
||||
|
||||
* [ReportGenerator](https://github.com/danielpalme/ReportGenerator) - v5.0.4
|
||||
|
||||
## Known limitations
|
||||
|
||||
Code Coverage includes the following known limitations:
|
||||
|
||||
* Code Coverage currently only supports the [OpenCover](https://github.com/OpenCover/opencover) format.
|
||||
* Code Coverage currently only supports code run in the Editor and not in Standalone/Player.
|
||||
* NPath Complexity calculation and Branch Coverage are not implemented at present so they will always appear as zero in the coverage report.
|
@ -0,0 +1,40 @@
|
||||
# Using Code Coverage
|
||||
|
||||
## Using Code Coverage with Burst compiler
|
||||
|
||||
If you use the [Burst package](https://docs.unity3d.com/Packages/com.unity.burst@latest) and have jobs compiled with Burst, you will need to disable Burst compilation in order to get full coverage. To disable Burst compilation you can do **one** of the following:
|
||||
|
||||
- Uncheck **Enable Compilation** under **Jobs** > **Burst** > **Enable Compilation**.
|
||||
- Pass `--burst-disable-compilation` to the command line.
|
||||
|
||||
## Using Code Coverage with Code Optimization
|
||||
|
||||
Code Optimization was introduced in 2020.1. Code Optimization mode defines whether Unity Editor compiles scripts in Debug or Release mode. Debug mode enables C# debugging and it is required in order to obtain accurate code coverage. To ensure Code optimization is set to Debug mode you can do **one** of the following:
|
||||
|
||||
- Switch to Debug mode in the Editor (bottom right corner, select the **Bug icon** > **Switch to debug mode**).
|
||||
- Using the CompilationPipeline api, set `CompilationPipeline.codeOptimization = CodeOptimization.Debug`.
|
||||
- Pass `-debugCodeOptimization` to the command line.
|
||||
|
||||
## Excluding code from Code Coverage
|
||||
|
||||
Any code that should not be contributing to the Code Coverage calculation can be excluded by adding the [`ExcludeFromCoverage`](https://docs.unity3d.com/ScriptReference/TestTools.ExcludeFromCoverageAttribute.html) attribute. This attribute can be added to Assemblies, Classes, Constructors, Methods and Structs. Note that you can also use the .NET [`ExcludeFromCodeCoverage`](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.excludefromcodecoverageattribute?view=netcore-2.0) attribute.
|
||||
|
||||
## Ignoring tests for Code Coverage
|
||||
|
||||
To ignore tests when running with Code Coverage, use the [ConditionalIgnore](https://docs.unity3d.com/Packages/com.unity.test-framework@latest/index.html?subfolder=/manual/reference-attribute-conditionalignore.html) attribute, passing the `"IgnoreForCoverage"` ID.
|
||||
|
||||
#### Example
|
||||
```
|
||||
public class MyTestClass
|
||||
{
|
||||
[Test, ConditionalIgnore("IgnoreForCoverage", "This test is disabled when ran with code coverage")]
|
||||
public void TestNeverRunningWithCodeCoverage()
|
||||
{
|
||||
Assert.Pass();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Subscribing to Code Coverage session events
|
||||
|
||||
Use the [Events API](https://docs.unity3d.com/Packages/com.unity.testtools.codecoverage@latest/index.html?subfolder=/api/UnityEditor.TestTools.CodeCoverage.Events.html) to subscribe to events invoked during a Code Coverage session.
|
@ -0,0 +1,37 @@
|
||||
apiRules:
|
||||
- exclude:
|
||||
# inherited Object methods
|
||||
uidRegex: ^System\.Object\..*$
|
||||
type: Method
|
||||
- exclude:
|
||||
# mentioning types from System.* namespace
|
||||
uidRegex: ^System\..*$
|
||||
type: Type
|
||||
- exclude:
|
||||
hasAttribute:
|
||||
uid: System.ObsoleteAttribute
|
||||
type: Member
|
||||
- exclude:
|
||||
hasAttribute:
|
||||
uid: System.ObsoleteAttribute
|
||||
type: Type
|
||||
- exclude:
|
||||
hasAttribute:
|
||||
uid: System.ComponentModel.EditorBrowsableAttribute
|
||||
ctorArguments:
|
||||
- System.ComponentModel.EditorBrowsableState.Never
|
||||
- exclude:
|
||||
uidRegex: ^OpenCover\.Framework\.Model
|
||||
type: namespace
|
||||
- exclude:
|
||||
uidRegex: ^Mono\.Reflection
|
||||
type: namespace
|
||||
- exclude:
|
||||
uidRegex: Unity.TestTools.CodeCoverage.Editor.Test*
|
||||
type: namespace
|
||||
- exclude:
|
||||
uidRegex: UnityEditor.TestTools.CodeCoverage.Tests
|
||||
type: namespace
|
||||
- exclude:
|
||||
uidRegex: ^Global Namespace*
|
||||
type: namespace
|
After Width: | Height: | Size: 131 KiB |
After Width: | Height: | Size: 6.4 KiB |
After Width: | Height: | Size: 6.6 KiB |
After Width: | Height: | Size: 114 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 102 KiB |
After Width: | Height: | Size: 122 KiB |
After Width: | Height: | Size: 51 KiB |
After Width: | Height: | Size: 198 KiB |
After Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 103 KiB |
After Width: | Height: | Size: 130 KiB |
After Width: | Height: | Size: 145 KiB |
After Width: | Height: | Size: 1.6 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 8.6 KiB |
After Width: | Height: | Size: 9.5 KiB |
After Width: | Height: | Size: 74 KiB |
After Width: | Height: | Size: 74 KiB |
After Width: | Height: | Size: 67 KiB |
After Width: | Height: | Size: 155 KiB |
After Width: | Height: | Size: 2.9 KiB |
After Width: | Height: | Size: 130 KiB |
After Width: | Height: | Size: 6.5 KiB |
After Width: | Height: | Size: 946 B |
After Width: | Height: | Size: 2.2 KiB |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 4.9 KiB |
After Width: | Height: | Size: 197 KiB |
After Width: | Height: | Size: 117 KiB |
After Width: | Height: | Size: 139 KiB |
@ -0,0 +1,39 @@
|
||||
# About Code Coverage
|
||||
|
||||
[Code Coverage](https://en.wikipedia.org/wiki/Code_coverage) is a measure of how much of your code has been executed. It is normally associated with automated tests, but you can gather coverage data in Unity at any time when the Editor is running.
|
||||
|
||||
It is typically presented as a [report](HowToInterpretResults.md) that shows the percentage of the code that has been executed. For automated testing the report does not measure the quality of tests, only whether your code is executed by `PlayMode` and `EditMode` tests. It is especially useful to check that critical or high risk areas of your code are covered, because they should receive the most rigorous testing.
|
||||
|
||||
## Code Coverage package
|
||||
|
||||
Use the Code Coverage package with the [Test Runner](CoverageTestRunner.md) to gather and present test coverage information. When you run your tests with code coverage enabled you can see exactly which lines of your code are executed when the tests run in addition to whether the tests passed or failed. See [Using Code Coverage with Test Runner](CoverageTestRunner.md).
|
||||
|
||||
Once a test run has completed, the Code Coverage package will generate an [HTML coverage report](HowToInterpretResults.md) showing which lines of your code are covered by tests. Code Coverage currently supports **PlayMode** and **EditMode** tests. It also allows you to track the code coverage changes through time.
|
||||
|
||||
Additionally, the Code Coverage package offers a [Coverage Recording](CoverageRecording.md) feature which allows capturing coverage data on demand, in case you do not have tests in your project or doing manual testing.
|
||||
|
||||
The [Quickstart](Quickstart.md) guide will give you an insight into the package.
|
||||
|
||||
## Coverage HTML report
|
||||
|
||||
Shown below is an example of the top level page of an HTML report generated by the package.
|
||||
|
||||

|
||||
|
||||
### Class view
|
||||
|
||||
This view shows some brief statistics for the selected class as well as the C# source code. Each line will be colored either green or red depending on whether the line was covered or not.
|
||||
|
||||

|
||||
|
||||
For more information see [How to interpret the results](HowToInterpretResults.md).
|
||||
|
||||
## Other reports
|
||||
|
||||
The package can produce [SonarQube](https://docs.sonarqube.org/latest/analysis/generic-test), [Cobertura](https://cobertura.github.io/cobertura) and [LCOV](https://github.com/linux-test-project/lcov) reports.
|
||||
|
||||
## Coverage summary badges
|
||||
|
||||
Additionally, the package can produce simple badges in SVG and PNG format, showing the current percentage of code that is covered.
|
||||
|
||||

|
@ -0,0 +1,22 @@
|
||||
# Upgrading to Code Coverage package version 1.2
|
||||
|
||||
To upgrade to Code Coverage package version 1.2, you need to do the following:
|
||||
- [Update assembly filtering aliases in batchmode](upgrade-guide.md#update-assembly-filtering-aliases-in-batchmode)
|
||||
- [Rename `pathStrippingPatterns` to `pathReplacePatterns` in batchmode](upgrade-guide.md#rename-pathstrippingpatterns-to-pathreplacepatterns-in-batchmode)
|
||||
|
||||
**Note**: If you're upgrading from a version older than 1.1, follow the upgrade guide for version 1.1 first.
|
||||
|
||||
## Update assembly filtering aliases in batchmode
|
||||
- Rename assembly filtering aliases when running in [batchmode](CoverageBatchmode.md). `<user>` alias was renamed to `<assets>` and `<project>` was renamed to `<all>`.
|
||||
|
||||
## Rename `pathStrippingPatterns` to `pathReplacePatterns` in batchmode
|
||||
- Rename `pathStrippingPatterns` to `pathReplacePatterns` in [batchmode](CoverageBatchmode.md).<br/><br/>**Example:**<br/><br/>Change `pathStrippingPatterns:C:/MyProject/` to `pathReplacePatterns:C:/MyProject/,`.<br/>This is equivalent to stripping `C:/MyProject/` by replacing `C:/MyProject/` with an empty string.
|
||||
|
||||
# Upgrading to Code Coverage package version 1.1
|
||||
To upgrade to Code Coverage package version 1.1, you need to do the following:
|
||||
- [Update path filtering globbing rules](upgrade-guide.md#update-path-filtering-globbing-rules)
|
||||
|
||||
## Update path filtering globbing rules
|
||||
- Update the path filtering globbing rules in your batchmode commands and code coverage window. To keep the current behavior when using [globbing](https://en.wikipedia.org/wiki/Glob_%28programming%29) to match any number of folders, the `*` character should be replaced with `**`. A single `*` character can be used to specify a single folder layer.<br/><br/>**Examples:**<br/><br/>`pathFilters:+C:/MyProject/Assets/Scripts/*` will include all files in the `C:/MyProject/Assets/Scripts` folder. Files in subfolders will not be included.<br/>`pathFilters:+C:/MyProject/Assets/Scripts/**` will include all files under the `C:/MyProject/Assets/Scripts` folder and any of its subfolders.
|
||||
|
||||
For a full list of changes and updates in this version, see the [Code Coverage package changelog](../changelog/CHANGELOG.html).
|
@ -0,0 +1,31 @@
|
||||
# What's new in version 1.2
|
||||
|
||||
Summary of changes in Code Coverage package version 1.2
|
||||
|
||||
The main updates in this release include:
|
||||
|
||||
## Added
|
||||
|
||||
- Added `Pause Recording` and `Resume Recording` buttons in the toolbar in the [Code Coverage window](CodeCoverageWindow.md).
|
||||
- Added `Test Runner References` coverage report option in the [Code Coverage window](CodeCoverageWindow.md). When you check this option, the generated coverage results include references to the triggering tests enabling the [Coverage by test methods](HowToInterpretResults.md#coverage-by-test-methods) section in the HTML report. This section allows you to see how each test contributes to the overall coverage. In [batchmode](CoverageBatchmode.md), you can generate test references by adding the `generateTestReferences` option in *-coverageOptions*.
|
||||
- Added `Log Verbosity Level` setting in the [Code Coverage window](CodeCoverageWindow.md) which allows setting the verbosity level for the editor and console logs.
|
||||
- Added `Additional Reports` option in the [Code Coverage window](CodeCoverageWindow.md) which if checked [SonarQube](https://docs.sonarqube.org/latest/analysis/generic-test), [Cobertura](https://cobertura.github.io/cobertura) and [LCOV](https://github.com/linux-test-project/lcov) reports will be generated. Added `generateAdditionalReports` in *-coverageOptions* for [batchmode](CoverageBatchmode.md).
|
||||
- Added `filtersFromFile` in *-coverageOptions* for [batchmode](CoverageBatchmode.md). This allows you to specify an external Json file which contains path and assembly filtering rules.
|
||||
- Added `dontClear` in *-coverageOptions* for [batchmode](https://docs.unity3d.com/Packages/com.unity.testtools.codecoverage@1.2/manual/CoverageBatchmode.html) which allows coverage results to be accumulated after every code coverage session. If not passed the results are cleared before a new session.
|
||||
|
||||
## Updated
|
||||
|
||||
- Updated the UI of the [Code Coverage window](CodeCoverageWindow.md) moving the action buttons into a toolbar at the top.
|
||||

|
||||
- Introduced new selection buttons under the *Included Assemblies* dropdown in the [Code Coverage window](CodeCoverageWindow.md).
|
||||
- Renamed *assemblyFilters* aliases in [batchmode](CoverageBatchmode.md); `<user>` was renamed to `<assets>` and `<project>` was renamed to `<all>`.
|
||||
- Replaced `pathStrippingPatterns` with `pathReplacePatterns` in [batchmode](CoverageBatchmode.md). The `pathReplacePatterns` option allows stripping and replacing specific sections from the paths that are stored in the coverage results xml files.
|
||||
- The size of the coverage result files and the Code Coverage session duration have been optimized.
|
||||
- Improved the editor and console logs.
|
||||
|
||||
## Fixed
|
||||
|
||||
- Ensure assemblies are removed from the Included Assemblies field if they no longer exist (case [1318668](https://issuetracker.unity3d.com/issues/code-coverage-the-included-assemblies-field-shows-assemblies-that-no-longer-exist)).
|
||||
- Ensure hidden sequence points are ignored (case [1372305](https://issuetracker.unity3d.com/issues/class-which-derives-from-methodbase-causes-incorrect-sequence-points-to-be-generated-by-coverage-api)).
|
||||
|
||||
For a full list of changes and updates in this version, see the [Code Coverage package changelog](../changelog/CHANGELOG.html).
|