DEV Community

Discussion on: What is a Unit in unit testing

Collapse
 
quii profile image
Chris James • Edited

I like to think of "units" as lego bricks. We like to create useful lego bricks that we can stick together to create systems.

These lego bricks could be a single class/function with a few lines of code but it might have a bunch of private stuff inside and maybe creates instances of other classes to do its job.

Unit test the boundaries of your lego bricks.

For me the acid test with unit tests is i ask myself if something is clearly implementation detail. If it is, you're testing too low level, move up the abstraction tree a bit.

If you're testing something that expresses an important business rule, that's probably a good unit test.

In respect to TDD a lot of people make a mistake in thinking that you must write a new test for each new class/function you write. That was never the intention. You write a new test for each new captured behaviour. If you stick to this rule your unit tests will document and verify real things... or units (e.g when I deposit £10 into an empty account the balance should be £10)

I speak about this here youtube.com/watch?v=Kwtit8ZEK7U (or you can read here quii.dev/The_Tests_Talk)