1

I'm writing my first test in unity3d. My objective is to assert that as the player runs, the Z axis remains consistent and the height doesn't change. In other terms, the Character's z position is consistently a 1 value.

enter image description here

This test continuously fails with the output of:

AssertionException: FloatComparer assertion failed.
Character.Transform.up.z CompareToConstantValue 1 failed. Expected: 1 Actual: 1
Created in
UnityEngine.Debug:LogException(Exception, Object)
UnityTest.ActionBase:Fail(AssertionComponent) (at Assets/UnityTestTools/Assertions/Comparers/ActionBase.cs:93)
UnityTest.Assertions:CheckAssertions(AssertionComponent[]) (at Assets/UnityTestTools/Assertions/Assertions.cs:37)
UnityTest.Assertions:CheckAssertions(AssertionComponent) (at Assets/UnityTestTools/Assertions/Assertions.cs:18)
UnityTest.AssertionComponent:CheckAssertionFor(CheckMethod) (at Assets/UnityTestTools/Assertions/AssertionComponent.cs:251)
UnityTest.AssertionComponent:Update() (at Assets/UnityTestTools/Assertions/AssertionComponent.cs:148)

The expected/actual values are the same so I'm struggling to see what the problem is. Any insight would be appreciated.

Because it's a float, I tried using 1.0 as the constant value with no luck.

Ben
  • 60,438
  • 111
  • 314
  • 488

1 Answers1

0

The value might not be exactly 1, but your assertion tells the test to expect it as EXACTLY 1. But floating points variable are not to safe to test to exact values. So you could try allow a minimum difference of your wanted value using the "Floating Point Error" field in the inspector.

For example if your value is expected to be 1 But in reality your floating point (can be) 0.99998...

Then you can set Floating Point error to 0.0001 So the assertion will pass with values between 0.999900000... and 1.000100000... And your value of 0.99998 woule be in between and pass.

This issues ocure with all floating point numbers so also with the values of vector pistions for example

EDIT: So the exception you see is also having this issue thinking that actual is 1 but it isn't, it's... nearly almoast 1^^