They are not so bad if you are doing modular functional programming and write your test functions as pure boolean functions accepting no arguments in the same file as the code they are testing. Then you can do a small amount of meta programming by writing a program to parse the function signatures of each module, build a list of your unit test signatures, and write them out to a new code file which prints their name and calls all of them in order, raising an error if any of them do not return True or 1. This reduces a lot of overhead associated with maintaining working unit tests. If you write your tests in the same file as the code they are testing, you won't accidentally loose any of them when a file gets renamed or moved.
[–] sparkybear ago
I've never heard of writing unit tests in the same file either. I've always seen them done in a separate file. Usually you're working on "person" and have "persontests". Maybe that's my inexperience though.
[–] boater 0 points 1 point 1 point (+1|-0) ago (edited ago)
It depends upon your language and setup. When programming in a language like C there are no costs to including tests as static inline functions inside of the header file because the compiler won't bother to include this code in the final binary of anything which doesn't reference them.
The primary benefit is that, if you need to make a copy to test multiple versions of an API concurrently without branching the code repository, then you won't need to also copy and rename the corresponding unit test file as well.
This is just a strategy I use to increase the probability that I will maintain complete test coverage when experimenting with the design of an API during early development, but like all strategies it won't be universally applicable for all scenarios.
[–] TheGuyWithFace ago
Oh certainly, I don't think unit tests belong everywhere. I mainly see them as useful for logic-heavy business rules that you know are going to be used for years to come. I can't really comment on TDD, I haven't done much of that either.
[–] sparkybear 0 points 1 point 1 point (+1|-0) ago
Yeah. Where I'm working we have one dev who is adamant about it, but he's come from some really old school code shops and worked in large corporations. it seems like these huge enterprise solutions would require a lot of testing to ensure that things are functioning properly.