first commit

This commit is contained in:
SimonSayeBabu
2025-01-17 13:10:20 +01:00
commit bd1057cec0
16967 changed files with 1048699 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
# Code Coverage window
![Code Coverage Settings](images/coverage_window.png)
|**Toolbar**|**Description**|
|:---|:---|
|![Start/Stop coverage recording](images/toolbar_record_button.png)|Select **Start Recording** to start recording code coverage data and **Stop Recording** to stop recording.|
|![Pause/Resume coverage recording](images/toolbar_pause_button.png)|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.|
|![Help](images/toolbar_help_button.png)|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.|

File diff suppressed because one or more lines are too long

View File

@@ -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/>
![Code Coverage Window](images/using_coverage/open_coverage_window.png)
2. Select **Enable Code Coverage** if not already selected, to be able to generate Coverage data and reports.<br/>
![Enable Code Coverage](images/using_coverage/enable_code_coverage.png)<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/>
![Select Assemblies](images/using_coverage/select_assemblies.png)
4. Select **Start Recording**.<br/>
![Start Recording](images/coverage_recording/start_recording.png)
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/>
![Stop Recording](images/coverage_recording/stop_recording.png)
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/>
![Report File Viewer](images/using_coverage/index_folder.png)
9. This opens the [HTML coverage report](HowToInterpretResults.md).<br/><br/>
![HTML Coverage Report](images/coverage_recording/report_html.png)

View File

@@ -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/>
![Code Coverage Window](images/using_coverage/open_coverage_window.png)
2. Select **Enable Code Coverage** if not already selected, to be able to generate Coverage data and reports.<br/>
![Enable Code Coverage](images/using_coverage/enable_code_coverage.png)<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/>
![Select Assemblies](images/using_coverage/select_assemblies.png)
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/>
![Run Tests in Test Runner](images/coverage_testrunner/test_runner.png)
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/>
![Report File Viewer](images/using_coverage/index_folder.png)
<br/><br/>
This opens the [HTML coverage report](HowToInterpretResults.md).<br/><br/>
![HTML Coverage Report](images/coverage_testrunner/report_html.png)
<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).

View File

@@ -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*|

File diff suppressed because one or more lines are too long

View File

@@ -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/>
![Code Coverage Report](images/report.png)
## 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.

View File

@@ -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.
![Install Code Coverage package](images/install_package.png)
Alternatively, use the **Add (+)** dropdown and select **Add package from git URL...** or **Add package by name...** and type `com.unity.testtools.codecoverage`.
![Install Code Coverage package from URL](images/install_package_url.png)
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.

View File

@@ -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.
![Code Coverage Workshop](images/quickguide/asteroids.png)
## 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.
![Install Code Coverage package](images/install_package.png)
Alternatively, use the **Add (+)** dropdown and select **Add package from git URL...** or **Add package by name...** and type `com.unity.testtools.codecoverage`.
![Install Code Coverage package from URL](images/install_package_url.png)
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/>
![Import Asteroids project](images/quickguide/import_sample.png)
## 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.
![Enable Code Coverage](images/using_coverage/enable_code_coverage.png)
**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/>
![Enter Play mode](images/quickguide/press_play.png)
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/>
![Code Coverage Window](images/quickguide/coverage_window.png)
2. If you see this warning select **Switch to debug mode**.<br/>
![Code Optimization](images/quickguide/code_optimization.png)
[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/>
![Included Assemblies](images/quickguide/included_assemblies.png)
4. Make sure **HTML Report**, **Report History**, **Auto Generate Report** and **Auto Open Report** are all checked.
![Auto Generate Report](images/quickguide/auto_generate_report.png)
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/>
![Run PlayMode tests](images/quickguide/test_runner.png)
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_.
![Code Coverage Report](images/quickguide/report_tests.png)
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.
![Code Coverage Report](images/quickguide/report_tests_after.png)
## 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/>
![LaserController Coverage](images/quickguide/report_laser_controller.png)
7. Select the **LaserController** class to enter the class view and notice that about 2/3 (65%) of the code is now covered (green).
![LaserController Class Coverage](images/quickguide/report_laser_controller_class.png)
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/>
![Auto Generate Report](images/quickguide/auto_generate_report.png)
3. Select **Start Recording**.<br/>
![Start Recording](images/coverage_recording/start_recording.png)
4. Hit **Play** to play the game and **exit** PlayMode before you get **8000** points.
![Enter Play mode](images/quickguide/press_play.png)
5. Select **Stop Recording**.<br/>
![Stop Recording](images/coverage_recording/stop_recording.png)
6. A file viewer window will open up containing the coverage report. Select `index.htm`.
7. Notice that **LaserController** has 0% coverage.<br/>
![LaserController Coverage](images/quickguide/report_laser_controller_recording.png)
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/>
![LaserController Coverage](images/quickguide/report_laser_controller_recording_after.png)
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.

View File

@@ -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)

View File

@@ -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.

View File

@@ -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.

View File

@@ -0,0 +1,34 @@
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

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 946 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

View File

@@ -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.
![HTML Report](images/report.png)
### 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.
![HTML Report](images/report_code_html.png)
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.
![Summary Badge](images/report_badge.png)

View File

@@ -0,0 +1,3 @@
{
"hideGlobalNamespace": true
}

View File

@@ -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).

View File

@@ -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.
![Toolbar](images/toolbar.png)
- 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).