Skip to content

nowsprinting/test-helper.random

Repository files navigation

Random Test Helper

Meta file check Test openupm

Wrapper and test stubs reference implementation for Random class.

Required Unity 2019 LTS or later.

Features

Wrapper interface for System.Random and UnityEngine.Random

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");
}

Extensions useful for testing

RandomExtensions provides extension methods useful for testing.

Installation

You can choose from two typical installation methods.

Install via Package Manager window

  1. Open the Package Manager tab in Project Settings window (Editor > Project Settings)
  2. Click + button under the Scoped Registries and enter the following settings (figure 1.):
    1. Name: package.openupm.com
    2. URL: https://package.openupm.com
    3. Scope(s): com.nowsprinting
  3. Open the Package Manager window (Window > Package Manager) and select My Registries in registries drop-down list (figure 2.)
  4. 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.

Install via OpenUPM-CLI

If you installed openupm-cli, run the command below:

openupm add com.nowsprinting.test-helper.random

Add assembly reference

  1. Open your product and test assembly definition file (.asmdef) in Inspector window
  2. Add TestHelper.Random into Assembly Definition References

License

MIT License

How to contribute

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.

How to development

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

Release workflow

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