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

Question Looking for tips on unit testing Lerps and Tweens.

Discussion in 'Testing & Automation' started by AdamBebko, Dec 21, 2021.

  1. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    164
    I'm currently building a project with unit testing. That's going very smoothly. In the near future, I'm looking to implement some lerps and tweens to make things feel a little more alive.

    Does anyone have tips about unit testing code that uses lerps?

    I assume it doesn't make sense to test that the actual leap and tween functions are doing what they are supposed to, but I feel like I should be unit testing to ensure things are moving the the way I expect them to. How do I balance this? Is there a way to avoid using UnityTests and stick to edit mode tests for this?

    Thanks for any advice.
     
  2. david-wtf

    david-wtf

    Joined:
    Sep 30, 2021
    Posts:
    25
    Hey @AdamBebko, you can (and, in my opinion, should) unit test your lerps, too - there's nothing speaking against this from a testing standpoint. Lerps are very isolated functions, but have a well-defined behavior. As soon as you abstract away the underlying implementation by wrapping your lerps into some common interface (e.g. an interface class, or some delegate), there's chance for messing things up - so tests it is!

    For lerps, which don't involve any looping themselves, you don't need to use PlayMode tests. In any case, a lerp can be represented by:

    Code (CSharp):
    1. /// Interpolates linearly between the two values a and b on the current point i, where f(i=0)->a and f(i=1)->b.
    2. public delegate T LinearInterpolator<T>(T a, T b, float i);
    Therefore, these can be perfectly tested without launching play mode.

    If you want to test animations/tweens that involve interpolating values over the course of some time, you can also test the looping/animating part without running play mode, if you abstract the looping logic (which will probably use async functions or IEnumerator-based coroutines) in some way.
     
    sandolkakos likes this.
  3. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    164
    Thanks so much this is a great idea.