Skip to main content
edited body
Source Link
user3797758
  • 3.7k
  • 2
  • 31
  • 53

I've just started using unity tests with the built in editor tests. After making my test and finding out that it constantly failed I debugged the code and found that a static object was set to a new game object when running the test while it is set to null when playing in the editor.

Is there a way to force unity to treat the test the in the same way that it treats scripts at run time?

Edit

After changing some code around i get another strange error: Editor Tests window showing error

does anyone know why "null" isn't "null". The null objects are stored in an array, but that shouldn't make a difference right?

Edit Edit (Edit 2?)

Here is the code that has the issue

public class StatCollector : MonoBehaviour { public static StatCollector running; //ensure that there is only one object running at the same time public void Awake () { if(running == null) { DontDestroyOnLoad(gameObject); running = this; } else if(running != this) { DestroyImmediate(gameObject); } } } 

and here is the test:

[Test] public void singleTestSingleTest() { //create gameobjects GameObject[] collect = new GameObject[2]; collect[0] = new GameObject("one"); collect[1] = new GameObject("two"); collect[0].AddComponent<StatCollector>(); //see if gameobjects were created correctly if (collect[0] == null || collect[1] == null) Assert.Fail("failed to create game objects"); //test what happens when the second stat collector is added to the scene collect[1].AddComponent<StatCollector>(); collect[0].GetComponent<StatCollector>().Awake(); collect[1].GetComponent<StatCollector>().Awake(); Assert.IsNull(collect[1]); } 

I'm assuming that there is no way to keep Awake private which is why it is public (just so that the test case has access to it)

I've just started using unity tests with the built in editor tests. After making my test and finding out that it constantly failed I debugged the code and found that a static object was set to a new game object when running the test while it is set to null when playing in the editor.

Is there a way to force unity to treat the test the in the same way that it treats scripts at run time?

Edit

After changing some code around i get another strange error: Editor Tests window showing error

does anyone know why "null" isn't "null". The null objects are stored in an array, but that shouldn't make a difference right?

Edit Edit (Edit 2?)

Here is the code that has the issue

public class StatCollector : MonoBehaviour { public static StatCollector running; //ensure that there is only one object running at the same time public void Awake () { if(running == null) { DontDestroyOnLoad(gameObject); running = this; } else if(running != this) { DestroyImmediate(gameObject); } } } 

and here is the test:

[Test] public void singleTest() { //create gameobjects GameObject[] collect = new GameObject[2]; collect[0] = new GameObject("one"); collect[1] = new GameObject("two"); collect[0].AddComponent<StatCollector>(); //see if gameobjects were created correctly if (collect[0] == null || collect[1] == null) Assert.Fail("failed to create game objects"); //test what happens when the second stat collector is added to the scene collect[1].AddComponent<StatCollector>(); collect[0].GetComponent<StatCollector>().Awake(); collect[1].GetComponent<StatCollector>().Awake(); Assert.IsNull(collect[1]); } 

I'm assuming that there is no way to keep Awake private which is why it is public (just so that the test case has access to it)

I've just started using unity tests with the built in editor tests. After making my test and finding out that it constantly failed I debugged the code and found that a static object was set to a new game object when running the test while it is set to null when playing in the editor.

Is there a way to force unity to treat the test the in the same way that it treats scripts at run time?

Edit

After changing some code around i get another strange error: Editor Tests window showing error

does anyone know why "null" isn't "null". The null objects are stored in an array, but that shouldn't make a difference right?

Edit Edit (Edit 2?)

Here is the code that has the issue

public class StatCollector : MonoBehaviour { public static StatCollector running; //ensure that there is only one object running at the same time public void Awake () { if(running == null) { DontDestroyOnLoad(gameObject); running = this; } else if(running != this) { DestroyImmediate(gameObject); } } } 

and here is the test:

[Test] public void SingleTest() { //create gameobjects GameObject[] collect = new GameObject[2]; collect[0] = new GameObject("one"); collect[1] = new GameObject("two"); collect[0].AddComponent<StatCollector>(); //see if gameobjects were created correctly if (collect[0] == null || collect[1] == null) Assert.Fail("failed to create game objects"); //test what happens when the second stat collector is added to the scene collect[1].AddComponent<StatCollector>(); collect[0].GetComponent<StatCollector>().Awake(); collect[1].GetComponent<StatCollector>().Awake(); Assert.IsNull(collect[1]); } 

I'm assuming that there is no way to keep Awake private which is why it is public (just so that the test case has access to it)

improved grammer
Source Link
user3797758
  • 3.7k
  • 2
  • 31
  • 53

I've just started using unity tests with the built in editor tests. After making my test and finding out that it constantly failed I debugged the code and found that a static object was set to a new game object when running the test while it is set to null when playing in the editor.

Is there a way to force unity to treat the test the in the same way that it treats scripts at run time?

Edit AfterEdit

After changing some code around i get another strange error: enter image description hereEditor Tests window showing error

does anyone know why "null" isn't "null". theThe null objects are stored in an array, but that shouldn't make a difference right?

Edit Edit (Edit 2?) hereEdit Edit (Edit 2?)

Here is the code that has the issue

public class StatCollector : MonoBehaviour { public static StatCollector running; //ensure that there is only one object running at the same time public void Awake () { if(running == null) { DontDestroyOnLoad(gameObject); running = this; } else if(running != this) { DestroyImmediate(gameObject); } } } 

and here is the test:

[Test] public void singleTest() { //create gameobjects GameObject[] collect = new GameObject[2]; collect[0] = new GameObject("one"); collect[1] = new GameObject("two"); collect[0].AddComponent<StatCollector>(); //see if gameobjects were created correctly if (collect[0] == null || collect[1] == null) Assert.Fail("failed to create game objects"); //test what happens when the second stat collector is added to the scene collect[1].AddComponent<StatCollector>(); collect[0].GetComponent<StatCollector>().Awake(); collect[1].GetComponent<StatCollector>().Awake(); Assert.IsNull(collect[1]); } 

I'm assuming that there is no way to keep Awake private which is why it is public (just so that the test case has access to it)

I've just started using unity tests with the built in editor tests. After making my test and finding out that it constantly failed I debugged the code and found that a static object was set to a new game object when running the test while it is set to null when playing in the editor.

Is there a way to force unity to treat the test the in the same way that it treats scripts at run time?

Edit After changing some code around i get another strange error: enter image description here

does anyone know why "null" isn't "null". the null objects are stored in an array, but that shouldn't make a difference right?

Edit Edit (Edit 2?) here is the code that has the issue

public class StatCollector : MonoBehaviour { public static StatCollector running; //ensure that there is only one object running at the same time public void Awake () { if(running == null) { DontDestroyOnLoad(gameObject); running = this; } else if(running != this) { DestroyImmediate(gameObject); } } } 

and here is the test:

[Test] public void singleTest() { //create gameobjects GameObject[] collect = new GameObject[2]; collect[0] = new GameObject("one"); collect[1] = new GameObject("two"); collect[0].AddComponent<StatCollector>(); //see if gameobjects were created correctly if (collect[0] == null || collect[1] == null) Assert.Fail("failed to create game objects"); //test what happens when the second stat collector is added to the scene collect[1].AddComponent<StatCollector>(); collect[0].GetComponent<StatCollector>().Awake(); collect[1].GetComponent<StatCollector>().Awake(); Assert.IsNull(collect[1]); } 

I'm assuming that there is no way to keep Awake private which is why it is public (just so that the test case has access to it)

I've just started using unity tests with the built in editor tests. After making my test and finding out that it constantly failed I debugged the code and found that a static object was set to a new game object when running the test while it is set to null when playing in the editor.

Is there a way to force unity to treat the test the in the same way that it treats scripts at run time?

Edit

After changing some code around i get another strange error: Editor Tests window showing error

does anyone know why "null" isn't "null". The null objects are stored in an array, but that shouldn't make a difference right?

Edit Edit (Edit 2?)

Here is the code that has the issue

public class StatCollector : MonoBehaviour { public static StatCollector running; //ensure that there is only one object running at the same time public void Awake () { if(running == null) { DontDestroyOnLoad(gameObject); running = this; } else if(running != this) { DestroyImmediate(gameObject); } } } 

and here is the test:

[Test] public void singleTest() { //create gameobjects GameObject[] collect = new GameObject[2]; collect[0] = new GameObject("one"); collect[1] = new GameObject("two"); collect[0].AddComponent<StatCollector>(); //see if gameobjects were created correctly if (collect[0] == null || collect[1] == null) Assert.Fail("failed to create game objects"); //test what happens when the second stat collector is added to the scene collect[1].AddComponent<StatCollector>(); collect[0].GetComponent<StatCollector>().Awake(); collect[1].GetComponent<StatCollector>().Awake(); Assert.IsNull(collect[1]); } 

I'm assuming that there is no way to keep Awake private which is why it is public (just so that the test case has access to it)

Making question title more specific to the issue of nulls & asserts
Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Getting unity How to act the same way in amake an Assert.IsNull test pass when the value is reported as it does during runtime<null>?

Tweeted twitter.com/StackGameDev/status/693400391107608577
Added code
Source Link
user3797758
  • 3.7k
  • 2
  • 31
  • 53
Loading
added second problem
Source Link
user3797758
  • 3.7k
  • 2
  • 31
  • 53
Loading
Source Link
user3797758
  • 3.7k
  • 2
  • 31
  • 53
Loading