Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should strings in automated unit tests still be hard-coded when providing internationalization support? [closed]

When writing a (C++) program with internationalization support I'm aware that strings should usually be stored in an external translation database rather than hard-coded. However, I'm wondering if it's still common practice to hard-code strings used in unit test code.

My intuition is that hard-coding strings in unit tests is ok as the effort to translate test code would be wasted on the end user. However, maybe there are situations where it would be useful, such as localization testing / development?

I'm wondering what the common practice is. Thanks.

like image 816
user3853250 Avatar asked Sep 16 '25 00:09

user3853250


1 Answers

You should not worry about locale for output messages (logs) which are destined to be read only by the dev team.

Usually you try to have a test tool which compares the results of your UTs to a baseline. This tool could not care less about the language used in the UT logs. :)
Now of course the comparison with the baseline will fail at times due to bugs or intentional changes.
So the only question you have to ask yourself is: who is going to check the UT logs when this happens? If it's the dev team, and all the dev team speaks English, use English in your UT logs.

As for non-log strings (e.g. an amount in currency hardcoded in your test or read from a file), well,
Since it is frequent to resort to mock objects so as to focus on the main objective of a unit test (and not having it derailed by other things),
Do not hesitate to mock the localization process (e.g. fix the locale to US/English) if the true objective of the UT is something which is entirely different.

Finally, for unit-tests specifically designed to test localization (e.g. make sure that user input is interpreted correctly, the right language is used in displays, etc.. ), it's the opposite situation of course: worry about internationalization, and try to mock anything else that gets in the way.

like image 176
Yohan Danvin Avatar answered Sep 17 '25 14:09

Yohan Danvin