Commit af571a61 authored by BlackAngle233's avatar BlackAngle233
Browse files

212

parent 1d9b5391
# Parameterized tests
For data-driven testing, you may want to have your tests parameterized. You may use both the [NUnit](http://www.nunit.org/) attributes [TestCase](https://github.com/nunit/docs/wiki/TestCase-Attribute) and [ValueSource](https://github.com/nunit/docs/wiki/ValueSource-Attribute) with a unit test.
> **Note**: With `UnityTest` it is recommended to use `ValueSource` since `TestCase` is not supported.
## Example
```c#
static int[] values = new int[] { 1, 5, 6 };
[UnityTest]
public IEnumerator MyTestWithMultipleValues([ValueSource("values")] int value)
{
yield return null;
}
```
# Parameterized tests
For data-driven testing, you may want to have your tests parameterized. You may use both the [NUnit](http://www.nunit.org/) attributes [TestCase](https://github.com/nunit/docs/wiki/TestCase-Attribute) and [ValueSource](https://github.com/nunit/docs/wiki/ValueSource-Attribute) with a unit test.
> **Note**: With `UnityTest` it is recommended to use `ValueSource` since `TestCase` is not supported.
## Example
```c#
static int[] values = new int[] { 1, 5, 6 };
[UnityTest]
public IEnumerator MyTestWithMultipleValues([ValueSource("values")] int value)
{
yield return null;
}
```
# WaitForDomainReload
`WaitForDomainReload` is an [IEditModeTestYieldInstruction](./reference-custom-yield-instructions.md) that you can yield in Edit Mode tests. It delays the execution of scripts until after an incoming domain reload. If the domain reload results in a script compilation failure, then it throws an exception.
## Constructors
| Syntax | Description |
| ---------------------------- | ------------------------------------------------------------ |
| `WaitForDomainReload()` | Create a new instance of the `WaitForDomainReload` yield instruction. |
## Example
``` C@
[UnitySetUp]
public IEnumerator SetUp()
{
File.Copy("Resources/MyDll.dll", @"Assets/MyDll.dll", true); // Trigger a domain reload.
AssetDatabase.Refresh();
yield return new WaitForDomainReload();
}
# WaitForDomainReload
`WaitForDomainReload` is an [IEditModeTestYieldInstruction](./reference-custom-yield-instructions.md) that you can yield in Edit Mode tests. It delays the execution of scripts until after an incoming domain reload. If the domain reload results in a script compilation failure, then it throws an exception.
## Constructors
| Syntax | Description |
| ---------------------------- | ------------------------------------------------------------ |
| `WaitForDomainReload()` | Create a new instance of the `WaitForDomainReload` yield instruction. |
## Example
``` C@
[UnitySetUp]
public IEnumerator SetUp()
{
File.Copy("Resources/MyDll.dll", @"Assets/MyDll.dll", true); // Trigger a domain reload.
AssetDatabase.Refresh();
yield return new WaitForDomainReload();
}
```
\ No newline at end of file
# Resources
Here you can find other related resources to the Unity Test Framework:
* [Performance Benchmarking in Unity: How to Get Started](https://blogs.unity3d.com/2018/09/25/performance-benchmarking-in-unity-how-to-get-started/) [Blog]
# Resources
Here you can find other related resources to the Unity Test Framework:
* [Performance Benchmarking in Unity: How to Get Started](https://blogs.unity3d.com/2018/09/25/performance-benchmarking-in-unity-how-to-get-started/) [Blog]
* [Testing Test-Driven Development with the Unity Test Runner](https://blogs.unity3d.com/2018/11/02/testing-test-driven-development-with-the-unity-test-runner/) [Blog]
\ No newline at end of file
# Workflow: How to create a Play Mode test
To create a **Play Mode** test, you can follow a similar process as when you want to create an **Edit Mode** test.
1. Start with switching to the **PlayMode** tab in the **Test Runner** window.
2. Create a test assembly folder (see [How to create a new test assembly)](./workflow-create-test-assembly.md). The folder name is *Tests* by default (or *Tests 1*, *Tests 2*, etc. if the preceding name is already in use).
![PlayMode tab](./images/playmode-tab.png)
> **Note**: If you don’t see the **Create Play Mode Test Assembly Folder** button enabled, make sure that in the Project window you navigate out of a folder with another .asmdef (such as one for Edit Mode tests).
3. When you have your Play Mode test assembly folder ready, then [create your Play Mode test](./workflow-create-test.md).
> **Note**: [Pre-defined Unity assemblies](https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html) (such as _Assembly-CSharp.dll_) do not reference your new assembly.
## References and builds
Unity Test Framework adds a reference to `TestAssemblies` in the [Assembly Definition](https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html) file, but it won't include any other references (e.g., to other scripting assemblies within the Unity project). So you need to add other assemblies yourself if you want to test them too.
Unity does not include `TestAssemblies` in Player builds, but in the Test Runner window, we have such an option. If you need to test code in pre-defined assemblies, you can reference `TestAssemblies` from other assemblies. You must remove these tests after the test run so that Unity does not add them to the final Player build.
To do this, in the Test Runner window choose **Enable playmode tests for all assemblies** option from the drop-down menu (to expand, click on the small list item in the top right corner). In the dialog box, click **OK** to manually restart the Editor.
![Enable Play Mode tests for all assemblies](./images/playmode-enable-all.png)
> **Note**: **Enabling Play Mode tests for all assemblies** includes additional assemblies in your project build, which can increase the project’s size as well as the build time.
# Workflow: How to create a Play Mode test
To create a **Play Mode** test, you can follow a similar process as when you want to create an **Edit Mode** test.
1. Start with switching to the **PlayMode** tab in the **Test Runner** window.
2. Create a test assembly folder (see [How to create a new test assembly)](./workflow-create-test-assembly.md). The folder name is *Tests* by default (or *Tests 1*, *Tests 2*, etc. if the preceding name is already in use).
![PlayMode tab](./images/playmode-tab.png)
> **Note**: If you don’t see the **Create Play Mode Test Assembly Folder** button enabled, make sure that in the Project window you navigate out of a folder with another .asmdef (such as one for Edit Mode tests).
3. When you have your Play Mode test assembly folder ready, then [create your Play Mode test](./workflow-create-test.md).
> **Note**: [Pre-defined Unity assemblies](https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html) (such as _Assembly-CSharp.dll_) do not reference your new assembly.
## References and builds
Unity Test Framework adds a reference to `TestAssemblies` in the [Assembly Definition](https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html) file, but it won't include any other references (e.g., to other scripting assemblies within the Unity project). So you need to add other assemblies yourself if you want to test them too.
Unity does not include `TestAssemblies` in Player builds, but in the Test Runner window, we have such an option. If you need to test code in pre-defined assemblies, you can reference `TestAssemblies` from other assemblies. You must remove these tests after the test run so that Unity does not add them to the final Player build.
To do this, in the Test Runner window choose **Enable playmode tests for all assemblies** option from the drop-down menu (to expand, click on the small list item in the top right corner). In the dialog box, click **OK** to manually restart the Editor.
![Enable Play Mode tests for all assemblies](./images/playmode-enable-all.png)
> **Note**: **Enabling Play Mode tests for all assemblies** includes additional assemblies in your project build, which can increase the project’s size as well as the build time.
For more information, see [Edit Mode vs. Play Mode tests](./edit-mode-vs-play-mode-tests.md).
\ No newline at end of file
# Workflow: **How to create a new test assembly**
Unity Test Framework looks for a test inside any assembly that references [NUnit](http://www.nunit.org/). We refer to such assemblies as `TestAssemblies`. The [Test Runner](./getting-started.md) UI can help you set up `TestAssemblies`. **Play Mode** and **Edit Mode** tests need to be in separate assemblies.
In the **Test Runner** window, you will see an **EditMode** tab enabled by default, as well as a **Create EditMode Test Assembly Folder** button.
![Test Runner window EditMode tab](./images/editmode-tab.png)
Click the button to create a *Tests* folder with a respective .asmdef file by default. Change the name of the new [Assembly Definition](https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html), if necessary, and press Enter to accept it.
![New Test folder and assembly file](./images/tests-folder-assembly.png)
In the Inspector window, it should have references to **nunit.framework.dll***,* **UnityEngine.TestRunner,** and **UnityEditor.TestRunner** assemblies, as well as **Editor** preselected as a target platform.
> **Note**: The **UnityEditor.TestRunner** reference is only available for [Edit Mode tests](./edit-mode-vs-play-mode-tests.md#edit-mode-tests).
![Assembly definition import settings](./images/import-settings.png)
# Workflow: **How to create a new test assembly**
Unity Test Framework looks for a test inside any assembly that references [NUnit](http://www.nunit.org/). We refer to such assemblies as `TestAssemblies`. The [Test Runner](./getting-started.md) UI can help you set up `TestAssemblies`. **Play Mode** and **Edit Mode** tests need to be in separate assemblies.
In the **Test Runner** window, you will see an **EditMode** tab enabled by default, as well as a **Create EditMode Test Assembly Folder** button.
![Test Runner window EditMode tab](./images/editmode-tab.png)
Click the button to create a *Tests* folder with a respective .asmdef file by default. Change the name of the new [Assembly Definition](https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html), if necessary, and press Enter to accept it.
![New Test folder and assembly file](./images/tests-folder-assembly.png)
In the Inspector window, it should have references to **nunit.framework.dll***,* **UnityEngine.TestRunner,** and **UnityEditor.TestRunner** assemblies, as well as **Editor** preselected as a target platform.
> **Note**: The **UnityEditor.TestRunner** reference is only available for [Edit Mode tests](./edit-mode-vs-play-mode-tests.md#edit-mode-tests).
![Assembly definition import settings](./images/import-settings.png)
# Workflow: How to create a test
To create a test, do the following:
1. Create your *Test* [assembly folder](./workflow-create-test-assembly.md) and select it in the **Project** window.
2. Click the button **Create Test Script in current folder** option in the **Test Runner** window.
![EditMode create test script](./images/editmode-create-test-script.png)
3. It creates a *NewTestScript.cs* file in the *Tests* folder. Change the name of the script, if necessary, and press Enter to accept it.
![NewTestScript.cs](./images/new-test-script.png)
Now you’ll see two sample tests in the Test Runner window:
![Test templates](./images/test-templates.png)
Now you can open the tests in your favorite script editor.
You can also create test scripts by navigating to **Assets** > **Create > Testing** > **C# Test Script**, unless adding a test script would result in a compilation error.
> **Note**: Unity does not include `TestAssemblies` ([NUnit](http://www.nunit.org/), Unity Test Framework, and user script assemblies) when using the normal build pipeline, but does include them when using **Run on <Platform>** in the Test Runner window.
## Filters
If you have a lot of tests, and you only want to view/run a sub-set of them, you can filter them in three ways (see image above):
* Type in the search box in the top left
* Click a test class or fixture (such as **NewTestScript** in the image above)
* Click one of the test result icon buttons in the top right
For more information, see [Edit Mode vs. Play Mode tests](./edit-mode-vs-play-mode-tests.md).
# Workflow: How to create a test
To create a test, do the following:
1. Create your *Test* [assembly folder](./workflow-create-test-assembly.md) and select it in the **Project** window.
2. Click the button **Create Test Script in current folder** option in the **Test Runner** window.
![EditMode create test script](./images/editmode-create-test-script.png)
3. It creates a *NewTestScript.cs* file in the *Tests* folder. Change the name of the script, if necessary, and press Enter to accept it.
![NewTestScript.cs](./images/new-test-script.png)
Now you’ll see two sample tests in the Test Runner window:
![Test templates](./images/test-templates.png)
Now you can open the tests in your favorite script editor.
You can also create test scripts by navigating to **Assets** > **Create > Testing** > **C# Test Script**, unless adding a test script would result in a compilation error.
> **Note**: Unity does not include `TestAssemblies` ([NUnit](http://www.nunit.org/), Unity Test Framework, and user script assemblies) when using the normal build pipeline, but does include them when using **Run on <Platform>** in the Test Runner window.
## Filters
If you have a lot of tests, and you only want to view/run a sub-set of them, you can filter them in three ways (see image above):
* Type in the search box in the top left
* Click a test class or fixture (such as **NewTestScript** in the image above)
* Click one of the test result icon buttons in the top right
For more information, see [Edit Mode vs. Play Mode tests](./edit-mode-vs-play-mode-tests.md).
# Workflow: How to run a Play Mode test in player
If you run a **Play Mode** test in the same way as an [Editor test](./workflow-run-test.md), it runs inside the Unity Editor. You can also run Play Mode tests on specific platforms. Click **Run all in the player** to build and run your tests on the currently active target platform.
![Run PlayMode test in player](./images/playmode-run-standalone.png)
> **Note**: Your current platform displays in brackets on the button. For example, in the image above, the button reads **Run all in player (StandaloneWindows)**, because the current platform is Windows. The target platform is always the current Platform selected in [Build Settings](https://docs.unity3d.com/Manual/BuildSettings.html) (menu: **File** > **Build Settings**).
The test result displays in the build once the test completes:
![Results of PlayMode in player test run](./images/playmode-results-standalone.png)
The application running on the platform reports back the test results to the Editor UI then displays the executed tests and shuts down. To make sure you receive the test results from the Player on your target platform back into the Editor that’s running the test, both should be on the same network.
> **Note:** Some platforms do not support shutting down the application with `Application.Quit`, so it will continue running after reporting the test results.
If Unity cannot instantiate the connection, you can see the tests succeed in the running application. Running tests on platforms with arguments, in this state, does not provide XML test results.
# Workflow: How to run a Play Mode test in player
If you run a **Play Mode** test in the same way as an [Editor test](./workflow-run-test.md), it runs inside the Unity Editor. You can also run Play Mode tests on specific platforms. Click **Run all in the player** to build and run your tests on the currently active target platform.
![Run PlayMode test in player](./images/playmode-run-standalone.png)
> **Note**: Your current platform displays in brackets on the button. For example, in the image above, the button reads **Run all in player (StandaloneWindows)**, because the current platform is Windows. The target platform is always the current Platform selected in [Build Settings](https://docs.unity3d.com/Manual/BuildSettings.html) (menu: **File** > **Build Settings**).
The test result displays in the build once the test completes:
![Results of PlayMode in player test run](./images/playmode-results-standalone.png)
The application running on the platform reports back the test results to the Editor UI then displays the executed tests and shuts down. To make sure you receive the test results from the Player on your target platform back into the Editor that’s running the test, both should be on the same network.
> **Note:** Some platforms do not support shutting down the application with `Application.Quit`, so it will continue running after reporting the test results.
If Unity cannot instantiate the connection, you can see the tests succeed in the running application. Running tests on platforms with arguments, in this state, does not provide XML test results.
For more information, see [Edit Mode vs Play Mode tests](./edit-mode-vs-play-mode-tests.md).
\ No newline at end of file
# Workflow: How to run a test
To run a test, you need to double-click on the test or test fixture name in the **Test Runner** window.
You can also use one of the buttons on the top bar, **Run All** or **Run Selected**. As a result, you’ll see the test status icon changed and a counter in the top right corner updated:
![EditMode Run Test](./images/editmode-run-test.png)
You may also use a context menu option **Run**, right-click on any item in the test tree to have it (with all its children if any) run.
![EditMode Run Tests](./images/editmode-run-tests.png)
## Run tests within Rider
It is possible to run unit tests in the Unity Test Framework directly from [JetBrains Rider](https://www.jetbrains.com/rider/).
# Workflow: How to run a test
To run a test, you need to double-click on the test or test fixture name in the **Test Runner** window.
You can also use one of the buttons on the top bar, **Run All** or **Run Selected**. As a result, you’ll see the test status icon changed and a counter in the top right corner updated:
![EditMode Run Test](./images/editmode-run-test.png)
You may also use a context menu option **Run**, right-click on any item in the test tree to have it (with all its children if any) run.
![EditMode Run Tests](./images/editmode-run-tests.png)
## Run tests within Rider
It is possible to run unit tests in the Unity Test Framework directly from [JetBrains Rider](https://www.jetbrains.com/rider/).
For more information, see the [JetBrains official documentation](https://www.jetbrains.com/help/rider/Running_and_Debugging_Unity_Tests.html) and their blog post [Run Unity tests in Rider 2018.1](https://blog.jetbrains.com/dotnet/2018/04/18/run-unity-tests-rider-2018-1/).
\ No newline at end of file
Test Framework copyright © 2020 Unity Technologies ApS
Licensed under the Unity Companion License for Unity-dependent projects--see [Unity Companion License](http://www.unity3d.com/legal/licenses/Unity_Companion_License).
Unless expressly provided otherwise, the Software under this license is made available strictly on an “AS IS” BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. Please review the license for details on these and other terms and conditions.
Test Framework copyright © 2020 Unity Technologies ApS
Licensed under the Unity Companion License for Unity-dependent projects--see [Unity Companion License](http://www.unity3d.com/legal/licenses/Unity_Companion_License).
Unless expressly provided otherwise, the Software under this license is made available strictly on an “AS IS” BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. Please review the license for details on these and other terms and conditions.
fileFormatVersion: 2
guid: 3ec7596410385054a9e0bc90377fbe63
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 3ec7596410385054a9e0bc90377fbe63
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 95cdf27b47eb82747ba9e51f41e72a35
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 95cdf27b47eb82747ba9e51f41e72a35
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: fa423365b1ce06a4dbdc6fb4a8597bfa
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: fa423365b1ce06a4dbdc6fb4a8597bfa
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Linq;
using System.Text;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using UnityEngine;
using UnityEngine.TestRunner.TestLaunchers;
namespace UnityEditor.TestTools.TestRunner.Api
{
internal class CallbacksDelegator : ICallbacksDelegator
{
private static CallbacksDelegator s_instance;
public static CallbacksDelegator instance
{
get
{
if (s_instance == null)
{
s_instance = new CallbacksDelegator(CallbacksHolder.instance.GetAll, new TestAdaptorFactory());
}
return s_instance;
}
}
private readonly Func<ICallbacks[]> m_CallbacksProvider;
private readonly ITestAdaptorFactory m_AdaptorFactory;
public CallbacksDelegator(Func<ICallbacks[]> callbacksProvider, ITestAdaptorFactory adaptorFactory)
{
m_CallbacksProvider = callbacksProvider;
m_AdaptorFactory = adaptorFactory;
}
public void RunStarted(ITest testsToRun)
{
m_AdaptorFactory.ClearResultsCache();
var testRunnerTestsToRun = m_AdaptorFactory.Create(testsToRun);
TryInvokeAllCallbacks(callbacks => callbacks.RunStarted(testRunnerTestsToRun));
}
public void RunStartedRemotely(byte[] testsToRunData)
{
var testData = Deserialize<RemoteTestResultDataWithTestData>(testsToRunData);
var testsToRun = m_AdaptorFactory.BuildTree(testData);
TryInvokeAllCallbacks(callbacks => callbacks.RunStarted(testsToRun));
}
public void RunFinished(ITestResult testResults)
{
var testResult = m_AdaptorFactory.Create(testResults);
TryInvokeAllCallbacks(callbacks => callbacks.RunFinished(testResult));
}
public void RunFinishedRemotely(byte[] testResultsData)
{
var remoteTestResult = Deserialize<RemoteTestResultDataWithTestData>(testResultsData);
var testResult = m_AdaptorFactory.Create(remoteTestResult.results.First(), remoteTestResult);
TryInvokeAllCallbacks(callbacks => callbacks.RunFinished(testResult));
}
public void RunFailed(string failureMessage)
{
Debug.LogError(failureMessage);
TryInvokeAllCallbacks(callbacks =>
{
var errorCallback = callbacks as IErrorCallbacks;
if (errorCallback != null)
{
errorCallback.OnError(failureMessage);
}
});
}
public void TestStarted(ITest test)
{
var testRunnerTest = m_AdaptorFactory.Create(test);
TryInvokeAllCallbacks(callbacks => callbacks.TestStarted(testRunnerTest));
}
public void TestStartedRemotely(byte[] testStartedData)
{
var testData = Deserialize<RemoteTestResultDataWithTestData>(testStartedData);
var testsToRun = m_AdaptorFactory.BuildTree(testData);
TryInvokeAllCallbacks(callbacks => callbacks.TestStarted(testsToRun));
}
public void TestFinished(ITestResult result)
{
var testResult = m_AdaptorFactory.Create(result);
TryInvokeAllCallbacks(callbacks => callbacks.TestFinished(testResult));
}
public void TestFinishedRemotely(byte[] testResultsData)
{
var remoteTestResult = Deserialize<RemoteTestResultDataWithTestData>(testResultsData);
var testResult = m_AdaptorFactory.Create(remoteTestResult.results.First(), remoteTestResult);
TryInvokeAllCallbacks(callbacks => callbacks.TestFinished(testResult));
}
public void TestTreeRebuild(ITest test)
{
m_AdaptorFactory.ClearTestsCache();
var testAdaptor = m_AdaptorFactory.Create(test);
TryInvokeAllCallbacks(callbacks =>
{
var rebuildCallbacks = callbacks as ITestTreeRebuildCallbacks;
if (rebuildCallbacks != null)
{
rebuildCallbacks.TestTreeRebuild(testAdaptor);
}
});
}
private void TryInvokeAllCallbacks(Action<ICallbacks> callbackAction)
{
foreach (var testRunnerApiCallback in m_CallbacksProvider())
{
try
{
callbackAction(testRunnerApiCallback);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}
}
private static T Deserialize<T>(byte[] data)
{
return JsonUtility.FromJson<T>(Encoding.UTF8.GetString(data));
}
}
}
using System;
using System.Linq;
using System.Text;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using UnityEngine;
using UnityEngine.TestRunner.TestLaunchers;
namespace UnityEditor.TestTools.TestRunner.Api
{
internal class CallbacksDelegator : ICallbacksDelegator
{
private static CallbacksDelegator s_instance;
public static CallbacksDelegator instance
{
get
{
if (s_instance == null)
{
s_instance = new CallbacksDelegator(CallbacksHolder.instance.GetAll, new TestAdaptorFactory());
}
return s_instance;
}
}
private readonly Func<ICallbacks[]> m_CallbacksProvider;
private readonly ITestAdaptorFactory m_AdaptorFactory;
public CallbacksDelegator(Func<ICallbacks[]> callbacksProvider, ITestAdaptorFactory adaptorFactory)
{
m_CallbacksProvider = callbacksProvider;
m_AdaptorFactory = adaptorFactory;
}
public void RunStarted(ITest testsToRun)
{
m_AdaptorFactory.ClearResultsCache();
var testRunnerTestsToRun = m_AdaptorFactory.Create(testsToRun);
TryInvokeAllCallbacks(callbacks => callbacks.RunStarted(testRunnerTestsToRun));
}
public void RunStartedRemotely(byte[] testsToRunData)
{
var testData = Deserialize<RemoteTestResultDataWithTestData>(testsToRunData);
var testsToRun = m_AdaptorFactory.BuildTree(testData);
TryInvokeAllCallbacks(callbacks => callbacks.RunStarted(testsToRun));
}
public void RunFinished(ITestResult testResults)
{
var testResult = m_AdaptorFactory.Create(testResults);
TryInvokeAllCallbacks(callbacks => callbacks.RunFinished(testResult));
}
public void RunFinishedRemotely(byte[] testResultsData)
{
var remoteTestResult = Deserialize<RemoteTestResultDataWithTestData>(testResultsData);
var testResult = m_AdaptorFactory.Create(remoteTestResult.results.First(), remoteTestResult);
TryInvokeAllCallbacks(callbacks => callbacks.RunFinished(testResult));
}
public void RunFailed(string failureMessage)
{
Debug.LogError(failureMessage);
TryInvokeAllCallbacks(callbacks =>
{
var errorCallback = callbacks as IErrorCallbacks;
if (errorCallback != null)
{
errorCallback.OnError(failureMessage);
}
});
}
public void TestStarted(ITest test)
{
var testRunnerTest = m_AdaptorFactory.Create(test);
TryInvokeAllCallbacks(callbacks => callbacks.TestStarted(testRunnerTest));
}
public void TestStartedRemotely(byte[] testStartedData)
{
var testData = Deserialize<RemoteTestResultDataWithTestData>(testStartedData);
var testsToRun = m_AdaptorFactory.BuildTree(testData);
TryInvokeAllCallbacks(callbacks => callbacks.TestStarted(testsToRun));
}
public void TestFinished(ITestResult result)
{
var testResult = m_AdaptorFactory.Create(result);
TryInvokeAllCallbacks(callbacks => callbacks.TestFinished(testResult));
}
public void TestFinishedRemotely(byte[] testResultsData)
{
var remoteTestResult = Deserialize<RemoteTestResultDataWithTestData>(testResultsData);
var testResult = m_AdaptorFactory.Create(remoteTestResult.results.First(), remoteTestResult);
TryInvokeAllCallbacks(callbacks => callbacks.TestFinished(testResult));
}
public void TestTreeRebuild(ITest test)
{
m_AdaptorFactory.ClearTestsCache();
var testAdaptor = m_AdaptorFactory.Create(test);
TryInvokeAllCallbacks(callbacks =>
{
var rebuildCallbacks = callbacks as ITestTreeRebuildCallbacks;
if (rebuildCallbacks != null)
{
rebuildCallbacks.TestTreeRebuild(testAdaptor);
}
});
}
private void TryInvokeAllCallbacks(Action<ICallbacks> callbackAction)
{
foreach (var testRunnerApiCallback in m_CallbacksProvider())
{
try
{
callbackAction(testRunnerApiCallback);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}
}
private static T Deserialize<T>(byte[] data)
{
return JsonUtility.FromJson<T>(Encoding.UTF8.GetString(data));
}
}
}
fileFormatVersion: 2
guid: 0de03ebd74e2b474fa23d05ab42d0cd8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 0de03ebd74e2b474fa23d05ab42d0cd8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEngine.TestTools.TestRunner;
namespace UnityEditor.TestTools.TestRunner.Api
{
internal class CallbacksDelegatorListener : ScriptableObject, ITestRunnerListener
{
public void RunStarted(NUnit.Framework.Interfaces.ITest testsToRun)
{
CallbacksDelegator.instance.RunStarted(testsToRun);
}
public void RunFinished(NUnit.Framework.Interfaces.ITestResult testResults)
{
CallbacksDelegator.instance.RunFinished(testResults);
}
public void TestStarted(NUnit.Framework.Interfaces.ITest test)
{
CallbacksDelegator.instance.TestStarted(test);
}
public void TestFinished(NUnit.Framework.Interfaces.ITestResult result)
{
CallbacksDelegator.instance.TestFinished(result);
}
}
}
using UnityEngine;
using UnityEngine.TestTools.TestRunner;
namespace UnityEditor.TestTools.TestRunner.Api
{
internal class CallbacksDelegatorListener : ScriptableObject, ITestRunnerListener
{
public void RunStarted(NUnit.Framework.Interfaces.ITest testsToRun)
{
CallbacksDelegator.instance.RunStarted(testsToRun);
}
public void RunFinished(NUnit.Framework.Interfaces.ITestResult testResults)
{
CallbacksDelegator.instance.RunFinished(testResults);
}
public void TestStarted(NUnit.Framework.Interfaces.ITest test)
{
CallbacksDelegator.instance.TestStarted(test);
}
public void TestFinished(NUnit.Framework.Interfaces.ITestResult result)
{
CallbacksDelegator.instance.TestFinished(result);
}
}
}
fileFormatVersion: 2
guid: f3e1b3cbf3fac6a459b1a602167ad311
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: f3e1b3cbf3fac6a459b1a602167ad311
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections.Generic;
using System.Linq;
namespace UnityEditor.TestTools.TestRunner.Api
{
internal class CallbacksHolder : ScriptableSingleton<CallbacksHolder>, ICallbacksHolder
{
private List<CallbackWithPriority> m_Callbacks = new List<CallbackWithPriority>();
public void Add(ICallbacks callback, int priority)
{
m_Callbacks.Add(new CallbackWithPriority(callback, priority));
}
public void Remove(ICallbacks callback)
{
m_Callbacks.RemoveAll(callbackWithPriority => callbackWithPriority.Callback == callback);
}
public ICallbacks[] GetAll()
{
return m_Callbacks.OrderByDescending(callback => callback.Priority).Select(callback => callback.Callback).ToArray();
}
public void Clear()
{
m_Callbacks.Clear();
}
private struct CallbackWithPriority
{
public ICallbacks Callback;
public int Priority;
public CallbackWithPriority(ICallbacks callback, int priority)
{
Callback = callback;
Priority = priority;
}
}
// Sometimes - such as when we want to test the test framework itself - it's necessary to launch a test run from
// inside a test. Because callbacks are registered globally, this can cause a lot of confusion (e.g. the in-test
// run will emit UTP messages, utterly confusing UTR). In such circumstances the safest thing to do is to
// temporarily suppress all registered callbacks for the duration of the in-test run. This method can be called
// to set up a using() block which will suppress the callbacks for the scope.
public IDisposable TemporarilySuppressCallbacks()
{
return new Suppressor(this);
}
private sealed class Suppressor : IDisposable
{
private readonly CallbacksHolder _instance;
private readonly List<CallbackWithPriority> _suppressed;
public Suppressor(CallbacksHolder instance)
{
_instance = instance;
_suppressed = new List<CallbackWithPriority>(instance.m_Callbacks);
instance.m_Callbacks.Clear();
}
public void Dispose()
{
_instance.m_Callbacks.AddRange(_suppressed);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace UnityEditor.TestTools.TestRunner.Api
{
internal class CallbacksHolder : ScriptableSingleton<CallbacksHolder>, ICallbacksHolder
{
private List<CallbackWithPriority> m_Callbacks = new List<CallbackWithPriority>();
public void Add(ICallbacks callback, int priority)
{
m_Callbacks.Add(new CallbackWithPriority(callback, priority));
}
public void Remove(ICallbacks callback)
{
m_Callbacks.RemoveAll(callbackWithPriority => callbackWithPriority.Callback == callback);
}
public ICallbacks[] GetAll()
{
return m_Callbacks.OrderByDescending(callback => callback.Priority).Select(callback => callback.Callback).ToArray();
}
public void Clear()
{
m_Callbacks.Clear();
}
private struct CallbackWithPriority
{
public ICallbacks Callback;
public int Priority;
public CallbackWithPriority(ICallbacks callback, int priority)
{
Callback = callback;
Priority = priority;
}
}
// Sometimes - such as when we want to test the test framework itself - it's necessary to launch a test run from
// inside a test. Because callbacks are registered globally, this can cause a lot of confusion (e.g. the in-test
// run will emit UTP messages, utterly confusing UTR). In such circumstances the safest thing to do is to
// temporarily suppress all registered callbacks for the duration of the in-test run. This method can be called
// to set up a using() block which will suppress the callbacks for the scope.
public IDisposable TemporarilySuppressCallbacks()
{
return new Suppressor(this);
}
private sealed class Suppressor : IDisposable
{
private readonly CallbacksHolder _instance;
private readonly List<CallbackWithPriority> _suppressed;
public Suppressor(CallbacksHolder instance)
{
_instance = instance;
_suppressed = new List<CallbackWithPriority>(instance.m_Callbacks);
instance.m_Callbacks.Clear();
}
public void Dispose()
{
_instance.m_Callbacks.AddRange(_suppressed);
}
}
}
}
fileFormatVersion: 2
guid: 4884ccc3528cb2e40a0e6f0a19a2b35b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 4884ccc3528cb2e40a0e6f0a19a2b35b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Linq;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Filters;
using UnityEngine;
namespace UnityEditor.TestTools.TestRunner.Api
{
[Serializable]
public class ExecutionSettings
{
public ExecutionSettings(params Filter[] filtersToExecute)
{
filters = filtersToExecute;
}
[SerializeField]
internal BuildTarget? targetPlatform;
// Note: Is not available after serialization
public ITestRunSettings overloadTestRunSettings;
[SerializeField]
internal Filter filter;
[SerializeField]
public Filter[] filters;
[SerializeField]
public bool runSynchronously;
[SerializeField]
public int playerHeartbeatTimeout = 60*10;
internal bool EditModeIncluded()
{
return filters.Any(f => IncludesTestMode(f.testMode, TestMode.EditMode));
}
internal bool PlayModeInEditorIncluded()
{
return filters.Any(f => IncludesTestMode(f.testMode, TestMode.PlayMode) && targetPlatform == null);
}
internal bool PlayerIncluded()
{
return filters.Any(f => IncludesTestMode(f.testMode, TestMode.PlayMode) && targetPlatform != null);
}
private static bool IncludesTestMode(TestMode testMode, TestMode modeToCheckFor)
{
return (testMode & modeToCheckFor) == modeToCheckFor;
}
internal ITestFilter BuildNUnitFilter()
{
return new OrFilter(filters.Select(f => f.BuildNUnitFilter(runSynchronously)).ToArray());
}
}
}
using System;
using System.Linq;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Filters;
using UnityEngine;
namespace UnityEditor.TestTools.TestRunner.Api
{
[Serializable]
public class ExecutionSettings
{
public ExecutionSettings(params Filter[] filtersToExecute)
{
filters = filtersToExecute;
}
[SerializeField]
internal BuildTarget? targetPlatform;
// Note: Is not available after serialization
public ITestRunSettings overloadTestRunSettings;
[SerializeField]
internal Filter filter;
[SerializeField]
public Filter[] filters;
[SerializeField]
public bool runSynchronously;
[SerializeField]
public int playerHeartbeatTimeout = 60*10;
internal bool EditModeIncluded()
{
return filters.Any(f => IncludesTestMode(f.testMode, TestMode.EditMode));
}
internal bool PlayModeInEditorIncluded()
{
return filters.Any(f => IncludesTestMode(f.testMode, TestMode.PlayMode) && targetPlatform == null);
}
internal bool PlayerIncluded()
{
return filters.Any(f => IncludesTestMode(f.testMode, TestMode.PlayMode) && targetPlatform != null);
}
private static bool IncludesTestMode(TestMode testMode, TestMode modeToCheckFor)
{
return (testMode & modeToCheckFor) == modeToCheckFor;
}
internal ITestFilter BuildNUnitFilter()
{
return new OrFilter(filters.Select(f => f.BuildNUnitFilter(runSynchronously)).ToArray());
}
}
}
fileFormatVersion: 2
guid: eea34a28297f9bc4c9f4c573bc8d5d1c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: eea34a28297f9bc4c9f4c573bc8d5d1c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment