DEV Community

Discussion on: Don't use non-test constants in unit tests

Collapse
 
marcmacgonagle profile image
marcmacgonagle

I think the question for me here is around whether or not the constant is part of the API?

If it was I'd expect a test for the value of the constant, which would alleviate the second problem. And if the API changes shouldn't the tests change too?

For instance, I'd expect Integer.MAX_VALUE to be part of the test suite for Integer.

Collapse
 
scottshipp profile image
scottshipp

Yes agree with this. Constants that are part of the API like Math.PI or Integer.MAX_VALUE should be tested.

Although you'd definitely think other tests would fail if these values were wrong.

I've just seen code recently where the only reason something was made "part of the public API" was actually just so it could be shared into the test code, not that it had a valid reason to be there.

Collapse
 
marcmacgonagle profile image
marcmacgonagle

You're right. If you don't intend to share it with your API users you shouldn't share it with your tests either.

Collapse
 
mentallurg profile image
mentallurg

if the API changes shouldn't the tests change too? - no. Well, the tests should of course change. But the most important thing is, that this change should not be done automatically. The test should be changed manually, so that we have a chance to review the changes needed, to think about desired behaviour, about alternative.