dynamic unit testing

Hello. Well i checked the Unit, E2E, Integration testing and where/what/when they should be used for. The thing that i can't understand is in the Unit testing when i write a test basically i'll hard code every case and that's basically not efficient for me. I may end up with a function that has +50 test cases and idk how to make the process dynamic.
10 Replies
Tilted
Tilted16mo ago
There's a couple of options. Hardcoding is the most frequent one. You could also look into mutation testing, it's less popular tho.
Lumberjack
LumberjackOP16mo ago
So basically there's no dynamic way to do it? If its 50 cases means 50 test
Tilted
Tilted16mo ago
Depending on the unit test technologies, there's different way to do them, but normally you can pass argument to your test methods. You can use test.each in Jest! What are you using?
Lumberjack
LumberjackOP16mo ago
Vitest basically jest
Christoph
Christoph16mo ago
Depends on the test cases. If they are all of the same format, then yes, it.each() is the way to go. If not, feel free to show a few of those test cases.
Rhys
Rhys15mo ago
Hard coding is the point of unit tests so things don’t change 😅 as for a function having 50+ test cases it sounds like that function is too complex and needs to be split up I also don’t bother with unit tests really, I do integration tests against and actual database so I don’t have to mock a bunch of stuff
Rhys
Rhys15mo ago
There’s a test case and a file you can look at for how to structure things
Lumberjack
LumberjackOP15mo ago
Thanks for the help
Benjamin
Benjamin15mo ago
Yup, tests should always be deterministic: always know what your inputs and expected outputs are.
Want results from more Discord servers?
Add your server