Looking for tips on unit testing Lerps and Tweens.

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.

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:

/// Interpolates linearly between the two values a and b on the current point i, where f(i=0)->a and f(i=1)->b.
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.

1 Like

Thanks so much this is a great idea.