Is the expected results suitable for hard coding when formatting the date and time of unit testing?

I like my unit tests to be fairly simple and easy to use. I often code the expected test results to avoid repeating the code I'm testing. In addition, I often use data-driven tests. For example, if I test the format in which the date / time is presented, I will sometimes hard code the expected string, i.e. "1/1/2000" or "1:00 PM". However, since date and time formats are culture specific and our application is localizable, the actual output may vary. However, my team is based in the USA, and therefore this is usually not a problem. Our continuous integration and build servers work with US culture information.

There is a team member who complained because he changed the date format on his development machine to manually test other date formats, and therefore there are not many tests for him. Should I use current culture information when testing outputs in unit tests or is this acceptable hard coding?

UPDATE: I set a specific locale for specific tests.

+4
source share
3 answers

Unit tests should be 100% repeatable, regardless of the environment in which they are run. The only reason for unit test failure is because the code has changed and aborted the test.

So, yes, you need to take steps so that your tests continue to pass regardless of external factors.

+5
source

Of course, this is good to compile the result, but you need to provide a double key for the locale information to make sure the unit test is isolated from the environment.

You can even add a new unit test with a test dual emulator of other developer locator settings.

Testing of the device must be performed and take place on any machine.

+3
source

Ideally, you should have a separate set of tests that link your localization. As for testing the current culture, my first choice would be to get your specific locale to work in a test run environment, rather than relying on the default value provided by the operating system. If this is not possible, I will parameterize my unit tests with the current locale, similar to how you handle localization in the main system and read the expected values ​​from this localized repository. The important point is that switching settings on the host computer should not be enough to knock down your unit test package.

+3
source

Source: https://habr.com/ru/post/1395145/


All Articles