Timeline for Should one test the values of an enum using unit tests?
Current License: CC BY-SA 3.0
28 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 5, 2017 at 8:29 | history | tweeted | twitter.com/StackSoftEng/status/915856526484656129 | ||
| Sep 28, 2017 at 19:56 | vote | accept | IS1_SO | ||
| Sep 28, 2017 at 16:03 | comment | added | Agent_L | IMHO it's not much more useful than testing if 2+2=4 in order to check if the Universe is right. You test the code using that enum, not the enum itself. | |
| Sep 28, 2017 at 14:36 | answer | added | digimunk | timeline score: 1 | |
| Sep 28, 2017 at 13:19 | comment | added | Machado | @jonrsharpe, I like to think of unit tests also as "definitions" or "requirements" written in code. A unit test of an enum would be as simple as checking the number of items on the enum and their values. Specially in C#, where the enums are not classes, but can be directly mapped to integers, guaranteeing their values and not programming by coincidence may prove useful for serialization purposes. | |
| Sep 28, 2017 at 13:15 | comment | added | Machado | It depends. If the enum is used for serialization and/or input interfaces and you want to keep backwards compatibility, then yes, they should at least be consistent between versions. Ian's asnswer below states a good case for this and also bitwise comparison and storage. | |
| Sep 28, 2017 at 12:59 | answer | added | Warbo | timeline score: 1 | |
| Sep 27, 2017 at 22:12 | answer | added | gnasher729 | timeline score: 3 | |
| Sep 27, 2017 at 20:29 | comment | added | RandomUs1r | Only reason to write unit test for an enum is to defensively prevent changes to it, otherwise the correct application of unit testing is to test the invariant(s) where it's being used. | |
| Sep 27, 2017 at 20:18 | answer | added | Paŭlo Ebermann | timeline score: 1 | |
| Sep 27, 2017 at 20:11 | comment | added | anotherdave | "Here the language construct (enum) is the one used to specify the behavior from a business standpoint." — could you expand on the behaviour that the business expects? If the business says that if the Enum is X, then "foo" should happen, but if the Enum is Y then "bar" should happen — where do "foo" and "bar" live? And what is setting the value of the Enum in the first place? This seems to be the crux of the question, especially as you mentioned that the Enum doesn't have methods of its own. | |
| Sep 27, 2017 at 20:07 | history | edited | Robert Harvey | CC BY-SA 3.0 | deleted 138 characters in body |
| Sep 27, 2017 at 20:07 | history | rollback | Robert Harvey | Rollback to Revision 3 | |
| Sep 27, 2017 at 18:37 | answer | added | Stop harming Monica | timeline score: 0 | |
| Sep 27, 2017 at 17:41 | comment | added | Frank Hileman | Developer education sounds like a better way to go. Any change to any line can potentially be catastrophic; you must be able to trust developers to some degree. | |
| Sep 27, 2017 at 16:49 | history | edited | Robert Harvey | CC BY-SA 3.0 | deleted 589 characters in body |
| Sep 27, 2017 at 15:03 | answer | added | jesm00 | timeline score: 12 | |
| Sep 27, 2017 at 15:02 | history | edited | IS1_SO | CC BY-SA 3.0 | added 604 characters in body |
| Sep 27, 2017 at 14:53 | answer | added | JacquesB | timeline score: 20 | |
| Sep 27, 2017 at 14:27 | answer | added | VoiceOfUnreason | timeline score: 43 | |
| Sep 27, 2017 at 14:19 | answer | added | Ian | timeline score: 13 | |
| Sep 27, 2017 at 14:18 | review | Close votes | |||
| Oct 5, 2017 at 3:04 | |||||
| Sep 27, 2017 at 14:16 | history | edited | IS1_SO | CC BY-SA 3.0 | added 616 characters in body |
| Sep 27, 2017 at 13:56 | comment | added | IS1_SO | @jonrsharpe It would assert that the values that are inside the enum are those that you expect. I would do it by iterating over the values of the enum, adding them to a set, for example as strings. Order that set. The compare that set against an ordered list of values written by hand in the test. They should match. | |
| Sep 27, 2017 at 13:55 | comment | added | gnat | Possible duplicate of Where is the line between unit testing application logic and distrusting language constructs? | |
| Sep 27, 2017 at 13:52 | comment | added | jonrsharpe | What precisely would a unit test of an enum look like? | |
| Sep 27, 2017 at 13:49 | review | First posts | |||
| Sep 27, 2017 at 23:36 | |||||
| Sep 27, 2017 at 13:47 | history | asked | IS1_SO | CC BY-SA 3.0 |