Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Creating a game object for Test Runner

Discussion in 'Scripting' started by Krellumdaed, Apr 17, 2020.

  1. Krellumdaed

    Krellumdaed

    Joined:
    Jun 24, 2015
    Posts:
    22
    I have spent a lot of time looking and experimenting and so far it seems like the answer is that it can't be done but maybe someone knows something I do not.

    I have a couple of Monobehavoir child classes that have utility methods, like save to persistent file, get internal state, etc. I would like to be able to set up unit tests for some of these in Test Runner. But I am not finding any way to create a GameObject without a scene. My editor menu test would create on in the currently active scene but they still did to allow me to make calls to the object or really get a solid reference to it.

    Is it just a basic fact that Monobehavor children cannot be tested with unit tests with TesrRunner?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    You can definitely test MonoBehaviours with Unity's Test runner. Here's an example:

    https://www.raywenderlich.com/9454-introduction-to-unity-unit-testing#toc-anchor-008

    The tutorial is using Resources.Load with a prefab but you can just as easily do something like this:
    Code (CSharp):
    1. var testObj = new GameObject();
    2. var myComponent = testObj.AddComponent(typeof(ComponentTypeToTest)) as ComponentTypeToTest;
    3.  
    4. // test stuff
    Does this answer your question or am I misunderstanding?
     
  3. Krellumdaed

    Krellumdaed

    Joined:
    Jun 24, 2015
    Posts:
    22