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
There's a couple of options. Hardcoding is the most frequent one. You could also look into mutation testing, it's less popular tho.
So basically there's no dynamic way to do it? If its 50 cases means 50 test
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?
Vitest basically jest
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.
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
https://github.com/AnswerOverflow/AnswerOverflow/blob/main/apps/discord-bot/src/domains/indexing.test.ts
https://github.com/AnswerOverflow/AnswerOverflow/blob/main/apps/discord-bot/src/domains/indexing.ts
GitHub
AnswerOverflow/apps/discord-bot/src/domains/indexing.test.ts at mai...
Indexing Discord Help Channel Questions into Google - AnswerOverflow/AnswerOverflow
GitHub
AnswerOverflow/apps/discord-bot/src/domains/indexing.ts at main · A...
Indexing Discord Help Channel Questions into Google - AnswerOverflow/AnswerOverflow
There’s a test case and a file you can look at for how to structure things
Thanks for the help
Yup, tests should always be deterministic: always know what your inputs and expected outputs are.