Skip to content

Instantly share code, notes, and snippets.

@ryo-ma
Last active June 10, 2017 01:28
Show Gist options
  • Select an option

  • Save ryo-ma/db5ee4bf9945ec92748c9596639f3fa7 to your computer and use it in GitHub Desktop.

Select an option

Save ryo-ma/db5ee4bf9945ec92748c9596639f3fa7 to your computer and use it in GitHub Desktop.
This script is test to fail when MonobehaviourTest is timeout.

TimeoutMonobehaviourTest

Description

  • This script is test to fail when MonobehaviourTest is timeout.

Environment

  • Unity Version 5.6.0

How to use

  • Please inherit your own class instead of Monobehaviour.
public class TimeoutMonobehaviourTest : SomethingMonobehaviour, IMonoBehaviourTest
  • Write the conditions for completing the test.
    public bool Condition
    {
        get
        {
            return obj.IsActive();
        }
    }
  • Set timeout value(timeout < 30).
private int timeout = 10;
  • Write CheckTestFinished() in Update().
    void Update()
    {
        // Wait for condition is met or timeout
        this.CheckTestFinished();
    }
  • Assert in test class.
public class SomethingTest
{
    [UnityTest]
    public IEnumerator MoveTest()
    {
        yield return new MonoBehaviourTest<TimeoutMonobehaviourTest>();
        TimeoutMonobehaviour timeOutMonobehaviourTest = Object.FindObjectOfType<TimeOutMonobehaviour>();
        Assert.That(timeoutMonobehaviourTest.Condition, Is.True);
    }

}
using UnityEngine;
using UnityEngine.TestTools;
using System;
// Please inherit your own class instead of Monobehaviour
public class TimeoutMonobehaviourTest : MonoBehaviour, IMonoBehaviourTest
{
// Assert this property in the test class
public bool Condition
{
get
{
// Write the conditions for completing the test
return false;
}
}
public bool IsTestFinished
{
get;
set;
}
public DateTime StartTime
{
get;
set;
}
// Set timeout value
private int timeout = 10;
void Update()
{
// Wait for condition is met or timeout
this.CheckTestFinished();
}
private void CheckTestFinished()
{
if (this.StartTime == null)
{
this.StartTime = DateTime.Now;
}
if (this.Condition)
{
this.IsTestFinished = true;
}
float spanSecond = DateTime.Now.Second - this.StartTime.Second;
if (spanSecond >= this.timeout)
{
this.IsTestFinished = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment