Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unit Testing Help

Discussion in 'Scripting' started by aholla, Oct 13, 2014.

  1. aholla

    aholla

    Joined:
    May 27, 2014
    Posts:
    81
    Hi, I've been playing around with the Unity test tools and I would like some advice.

    I'm focusing primarily on Unit tests with NUnit but I think perhaps I will need to run Intergration tests instead.

    I am trying to test some code that moves my player, the problem is that the code does not update my players value before the test is complete. This makes me think that what I am trying to do is not suited for unity or that I am testing the wrong thing. I dont really want to make a mock for it as its pretty simple.

    Here is my code and what I am trying to do is move my player by adjusting the vector on its rigidbody. I then wan to compare the before and after values of this vector to see if it has been updated.

    Example:

    Code (CSharp):
    1. playerRigidbody.velocity = new Vector2( 0.0f, 0.0f );
    2. float oldVector = playerRigidbody.velocity.x;
    3. playerRigidbody.velocity = new Vector2( 10f, 0.0f );
    4. float newVector = playerRigidbody.velocity.x;
    5.  
    6. //Debug.Log( newVector + " : " + oldVector )
    7.  
    8. Assert.Greater( newVector, oldVector );
    This fails, reporting the vectors are the same, however if i put a log in before the Assert, it give unity enough time to update and the vector gets updated.

    So my question is should I be trying to do this in a unit test, the rigidbody is not my code and therfor perhaps out of scope so perhaps i need to run a Intergration test and use the Assertion component on the player. This is not really what i want to do though, its test after...

    Thanks for any advice.