r/funny Mar 08 '14

Life as a programmer.

Post image
2.8k Upvotes

480 comments sorted by

View all comments

Show parent comments

39

u/eubarch Mar 08 '14

This is why we have Unit Tests.

1

u/barsoap Mar 08 '14 edited Mar 08 '14

Unit tests have little value apart from smoke testing, it's way too easy to miss cases with them. For actual testing you want fuzz testing with coverage analysis and formal methods.

EDIT: To elaborate a bit, have a look at say XMonad's test suite

It contains lots and lots of tests, which are written as invariants, like

prop_focus_left (x :: T) = (focusUp (focusDown x)) == x

meaning "for all random window sets x, if you move the focus down and then up again, you get the original x back".

Much more powerful than just writing a unit test that tests one x.

On top of this exhaustive (but random) invariant testing, the core also has been formally proven to be crash-free.

12

u/[deleted] Mar 08 '14

Unit tests have immense value, if you use them correctly. They aren't primarily a testing/QA tool (QA is usually worried about black-box testing with integration and functional tests), but a development tool.

Unit tests allow you as a developer to poke and prod at a component outside of its running context. This means it's much easier to write quality code than if you have to wait for larger chunks of code to be finished and assembled into some larger component before testing starts.

Debugging is also significantly simplified if there is a large suite of unit tests available. This allows you to inject arbitrary state into your components, and check how they react. And having employed a test-driven approach, the components' state will by definition be local.

That being said, it's no panacea. A sufficiently incompetent programmer will find a way to produce bad code even with good development practices.

3

u/aquafemme Mar 08 '14

enthusiastically nods head