Unit tests are automated software tests that cover one unit of code. One unit could for example be a single function.

Since these tests shouldn’t test more than the unit itself, dependencies should be mocked.

1 Advantages

1.1 Speed

Unit tests are extremely fast to execute, since they cover so little code and all slow dependencies are mocked away. This is great for an extremely fast feedback loop. I tend to have unit tests execute whenever I save a file in the current project, because this allows me see very quickly when I broke something and know what exactly it was that broke it.

1.2 Pinpointing errors

Since unit tests are so small in scope, they are a great way to show which specific function has a bug, and therefore it isn’t necessary to look all over the codebase and do a long debugging session to figure out why something broke. Instead, the unit test/s for the function at fault will simply break.

1.3 Easy to write

Unit tests are very small in scope and as long as the unit under test is written well enough, it is very easy to add new unit tests. This also makes test-driven-development easy.

2 Disadvantages

2.1 Small scope might miss errors

Since only singular units are tested, errors might still occur when multiple units are combined. However, this is mitigated by a good integration test suite.

3 Backlinks   backlinks