I would say that 90% of the grief I have experienced building applications is the validation part - that you are actually building the right thing.
For me the right amount of automated testing is all about exploring the cost vs benefit boundary, which shifts with tooling and circumstance. But my firm rule now is: code must either have a firm expiration date or full test coverage. Nothing between is allowed.
So if you're going to cut back on testing, make sure your customers have agreed in writing on a date after which they should throw the code away.
Or when refactoring a piece of code that renders UI, write some tests to verify a couple of common cases and then refactor. Run the tests and I'll know whether the output is the same.
I've always said that RSpec is about documentation, not testing (hence the name - it's a specification for your code).
However, recently, I've been using Minitest-spec; not quite as readable but less magic and it feels faster.
1. Write your code so that independent units of functionality (algorithms) can be tested. 2. Make sure people understand, when changing code, either a) change the tests first, or b) recognize what tests should be failing because of the change and which shouldn't.
For good, testable code combined with good tests, it should be rare that the tests start failing when you change the implementation. Admittedly, that's true in theory and can vary widely in practice.
The appropriate practices there are short iterations, frequent releases, acceptance tests, and having product people sitting next to developers.
For those wondering how unit testing relates to other practices, go read Kent Beck's Extreme Programming Explained. He's one of the people who led the current wave of unit testing adoption via jUnit. Years before the term "Agile" existed, he was on a team that found a highly productive way of working, one for which unit testing was an important foundational element. But it was just one of 12 practices they saw as necessary.
Once you have that working well and you're building what the businesspeople expect, the next step is to make the businesspeople start testing their assumptions. That's basically what the whole Lean Startup thing, minus the fad, is about: let's not just go build whatever the guy with the tie says. Let's all get evidence about what's really worth building.
It's sort of like "the first dose is free", but applied for good.
1: small print - writing good tests is hard
I could likewise argue that "writing code is simple and easy, but writing good code is hard", but this statement has little value in itself. It seems more an argument against using complex testing frameworks and going for simpler syntax. As a fan of Nose, I absolutely agree.
The more complex your code, the more coupling there is, the more sources of bugs there are potentially... and the harder it is to write tests for that code.
Break down your code into smaller methods and more focused objects and just watch your unit tests get simpler.
Basically though, if you find your test difficult to write, there is a problem, and it's not with the test. And that should be your sign that you either don't understand the problem, or you are trying to do too much.
Now, this is hard. It's hard to accept that way of thinking. But the end result has always been cleaner, better code in my experience. It's not always the obvious way, but it's the best way.
The second problem is that whenever you're faced with a large, older codebase, it's often difficult or impossible to refactor away complexity in core components.
Currebtly I am only toying with it in one of my pet projects but it seems promising.
If your unit tests are hard, that's a non-SOLID smell.
- this article only deals with unit testing
- it does not help you write more maintainable tests
For the record, I don't think (unit) testing is simple. Tests are, in my experience, the least maintainable and readable part of a codebase. You often end up with many lines of setting up complex business objects. And usually, the only help wrt to what the hell is being tested is the method name. And that's just unit tests. Integration tests are a major pain, and often even harder to understand and maintain.