Library for mocking the UnityEngine.Random.
Required Unity 2019 LTS or later.
The UnityEngine.Random class provides static methods.
You can inject test stubs into your tests by replacing Random with the IRandom interface.
Usage:
1. Insert the code below into your product code to replace UnityEngine.Random with a TestHelper.Random.RandomWrapper instance
#if UNITY_INCLUDE_TESTS
internal IRandom Random { private get; set; } = new RandomWrapper();
#endifTip
RandomWrapper is a wrapper around System.Random that works at runtime, but you can remove the TestHelper.Random assembly from your build using the #if UNITY_INCLUDE_TESTS directive.
public class StubRandom : RandomWrapper
{
private readonly float[] _returnValues;
private int _returnValueIndex;
public StubRandom(params float[] returnValues)
{
Assert.That(returnValues, Is.Not.Empty);
_returnValues = returnValues;
_returnValueIndex = 0;
}
public override float value
{
get
{
return _returnValues[_returnValueIndex++];
}
}
}[Test]
public void Value_ReturnSpecifiedValues()
{
IRandom sut = new StubRandom(0.2f, 0.3f, 0.5f);
Assert.That(sut.value, Is.EqualTo(0.2f), "1st value");
Assert.That(sut.value, Is.EqualTo(0.3f), "2nd value");
Assert.That(sut.value, Is.EqualTo(0.5f), "3rd value");
}RandomExtensions provides extension methods useful for testing.
- Open the Project Settings window (Editor > Project Settings) and select Package Manager tab (figure 1)
- Click + button under the Scoped Registries and enter the following settings:
- Name:
package.openupm.com - URL:
https://package.openupm.com - Scope(s):
com.nowsprinting
- Name:
- Open the Package Manager window (Window > Package Manager) and select My Registries tab (figure 2)
- Select Random Test Helper and click the Install button
Figure 1. Scoped Registries setting in Project Settings window
Figure 2. My Registries in Package Manager window
- Open your product and test assembly definition file (.asmdef) in Inspector window
- Add TestHelper.Random into Assembly Definition References
MIT License
Open an issue or create a pull request.
Be grateful if you could label the PR as enhancement, bug, chore, and documentation.
See PR Labeler settings for automatically labeling from the branch name.
Add this repository as a submodule to the Packages/ directory in your project.
git submodule add [email protected]:nowsprinting/test-helper.random.git Packages/com.nowsprinting.test-helper.randomWarning
Required installation packages for running tests (when embedded package or adding to the testables in manifest.json), as follows:
- Test Helper package v1.2.1 or later
Generate a temporary project and run tests on each Unity version from the command line.
make create_project
UNITY_VERSION=2019.4.40f1 make -k testThe release process is as follows:
- Run Actions > Create release pull request > Run workflow
- Merge created pull request
Then, will do the release process automatically by Release workflow. After tagging, OpenUPM retrieves the tag and updates it.
Caution
Do NOT manually operation the following operations:
- Create a release tag
- Publish draft releases
Caution
You must modify the package name to publish a forked package.
Tip
If you want to specify the version number to be released, change the version number of the draft release before running the "Create release pull request" workflow.



