Double precision #611
-
On my PC, a unit test that uses VerifyTests, succeeds. But when I upload the same code to our build server, the unit test fails because one of the double values is being serialized slightly different. Is it somehow possible to specify the precision when doubles are compared? In both cases the double is set to 1225.84: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
yeah thats not ideal. happy to accept a PR that fixes this |
Beta Was this translation helpful? Give feedback.
-
This is an issue in other c# snapshot testing libraries also, eg https://github.com/theramis/Snapper There seems to be some non-determinism in the runtime when converting decimals, doubles and floats (eg sometimes a number will be 13.0, the next test run it will be 13.00) This frequently causes my CI tests to fail, and there's no clear solution. A re-run of the same build will deterministically exhibit the same non-determinism (aka running the same build produces consistent results, but the results change between builds), so the solution is often to completely rebuild the application (dotnet clean, dotnet build) and run CI again. Another solution is to write a custom converter (eg map it to a DTO) for each type you want to snapshot, within which you manually set the precision for these data types (probably by converting them to a string). This is a pain honestly. I have tried solving this by forcing a default culture in the application, eg
But it doesn't fix the issue. Does anyone have a better solution? |
Beta Was this translation helpful? Give feedback.
-
As an alternative to mapping properties to strings, you could tell Verify to write all floats and doubles with a specified precision. Add these classes:
... and then tell Verify to use them:
|
Beta Was this translation helpful? Give feedback.
yeah thats not ideal. happy to accept a PR that fixes this