DEV Community

Discussion on: Where Should Tests Live?

Collapse
 
billperegoy profile image
billperegoy

I've always worked on projects where the tests were in a separate parallel tree from the code. This makes it easy to run the tests but figuring out where a test for a particular method lives can be a challenge. We always try to make the two trees as similar as possible, but they always diverge and that makes working on legacy code difficult.

I've recently seen some JavaScript projects putting the in the same directory as the code. I liked this as it's really clear what test file goes with what code file. I imagine after a while, this would encourage one to add more levels of hierarchy to the code base to avoid too much clutter. So I wonder if this would solve one problem but create another for large codebases.

I've never actually put code and tests in the same file except for code katas and demos. One one hand, it feels like it might make files too big. On the other hand, maybe it would also encourage breaking up the code into more reasonable module sizes. I probably wouldn't know how it felt until I tried it on something large.

In the end, I'm all for getting the tests closer to the code rather than in a separate parallel tree. Most of the test frameworks out there seem to encourage a separate tree, so I find myself still working in that way.