4 ms·
I have been trying to add unit tests to my app, but almost all the tutorials you find are useless. They show you how to test 1 + 1, or how to test the framework
by collyw 13y ago
I have been trying to add unit tests to my app, but almost all the tutorials you find are useless. They show you how to test 1 + 1, or how to test the framework I am using. The parts of the code I am almost certain will work. None of them show you how to actually test anything useful.
- Anderkent 13y agoLook for TDD tutorials for less trivial examples of what you should test. You want a general 'how do I know what to test' tutorial, rather than 'how do I use testing framework X' (which is what you were probably reading, if they show you how to test 1+1). Other than that, just be explicit about your api contracts and test those.
- skrebbel 13y agoYou might like http://blog.testdouble.com/posts/2014-01-25-the-failures-of-intro-to-tdd.html http://blog.testdouble.com/posts/2014-01-25-the-failures-of-... It's a tad long, but worth it. tl;dr: the first half agrees with you, and the second half describes a process that does help you test useful stuff (and design your software decently along with it)
- collyw 13y agoI have been adding integration tests. Its an in house app, so constantly changing, and constantly in beta. I wanted to start adding unit tests a couple of years back, but looking back, I don't think it would have gained very much, and cost a lot of time (creating the data to test the parts). If there is a functional unit that does one specific thing, I could see it being useful to test. But most of my app basically queries a database and displays the results. The bugs I do get would rarely have been caught by unit tests.
- mattgreenrocks 13y agoTestability falls out of good design. Testability is not worth optimizing for on it's own, though. It's just a nice side effect. Read Growing Object-Oriented Software Guided By Tests. The authors walk through the development of an auction sniper.
- Iftheshoefits 13y agoThe "testability" of a piece of software doesn't really have much to do with the discussion about the quantity or kinds of tests that should be written for it. Every test is another thing that has the potential to require debugging.
- camus2 13y agoSo true, debugging "tests" is infernal. Peaple always tell you testing should be easy and quick,it is neither of them.
- michaelfeathers 13y agoI wrote a book called 'Working Effectively with Legacy Code' that's really all about testing code that is not easy to test - real apps beyond toy examples. There are techniques in the book but really you should try to write tests for code as you write it. It's the best way to make sure that your code is decoupled enough for testing. If you do that, you don't need the techniques in the book.
- ollysb 13y agoI've heard many good things about this book, definitely on the reading list!
- earlz 13y agoYou're the author of that!? I love that book, and even though I stick to it's practices less than I probably should, it was incredibly helpful
- n1ghtmare_ 13y agoThis book is legendary! This and Uncle Bob's books Clean Code. A must read for every programmer !
- ollysb 13y agoI read many tutorials and several books on TDD but never quite got it until I read Kent Beck's Test Driven Development By Example[1]. I also highly recommend his Screencast series on pragprog.com[2] which gives you a good flavour of the workflow he uses with TDD. Edit: For those that don't know Kent Beck is basically the godfather of TDD. [1] http://www.amazon.com/Test-Driven-Development-By-Example/dp/0321146530 http://www.amazon.com/Test-Driven-Development-By-Example/dp/... [2] http://pragprog.com/screencasts/v-kbtdd/test-driven-development http://pragprog.com/screencasts/v-kbtdd/test-driven-developm...
- Chupachupski 13y agoPersonally I can relate to Kent Beck's approach to testing rather than "test everything": "I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence (I suspect this level of confidence is high compared to industry standards, but that could just be hubris). If I don't typically make a kind of mistake (like setting the wrong variables in a constructor), I don't test for it. I do tend to make sense of test errors, so I'm extra careful when I have logic with complicated conditionals. When coding on a team, I modify my strategy to carefully test code that we, collectively, tend to get wrong." http://stackoverflow.com/questions/153234/how-deep-are-your-unit-tests/153565#153565 http://stackoverflow.com/questions/153234/how-deep-are-your-...
- ollysb 13y agoYeah, I think this comes with experience. I started off testing everything but over the years you get a feel for which tests aren't going to add value over the lifetime of the project.
- lgunsch 13y agoI have also read Test Driven Development By Example. It really opened my eye's to how TDD is suppose to work. Its quite well written. Most of what I have seen on the web does not explain it to any sufficient level.
- gaius 13y agoWell, you have missed the point of TDD. It exists to sell books, seminars, training and consulting. Not to help in the efficient creation of useful software.