Wrapper and test stubs reference implementation for Random class.
Required Unity 2019 LTS or later.
IRandom
interface is a wrapper interface for System.Random
and UnityEngine.Random
class.
You can inject a test stub in your tests by replacing it with a RandomWrapper
instance.
Usage:
1.Insert the code below into your product code, so replace UnityEngine.Random
to TestHelper.Random.RandomWrapper
instance.
internal IRandom Random { private get; set; } = new RandomWrapper();
Note
RandomWrapper
also works at runtime, but if you want to strip the extra assembly (TestHelper.Input
) from your IL2CPP build, you can use the #if UNITY_INCLUDE_TESTS
directive.
2.Create test stub in your test.
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++];
}
}
}
3.Write test using test stub.
[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.
You can choose from two typical installation methods.
- Open the Package Manager tab in Project Settings window (Editor > Project Settings)
- Click + button under the Scoped Registries and enter the following settings (figure 1.):
- 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 in registries drop-down list (figure 2.)
- Click Install button on the
com.nowsprinting.test-helper.random
package
Figure 1. Package Manager tab in Project Settings window.
Figure 2. Select registries drop-down list in Package Manager window.
If you installed openupm-cli, run the command below:
openupm add com.nowsprinting.test-helper.random
- 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 https://github.com/nowsprinting/test-helper.random.git Packages/com.nowsprinting.test-helper.random
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 test
Run Actions > Create release pull request > Run workflow and merge created pull request. (Or bump version in package.json on default branch)
Then, Will do the release process automatically by Release workflow. And after tagging, OpenUPM retrieves the tag and updates it.
Do NOT manually operation the following operations:
- Create a release tag
- Publish draft releases