Slashdot Mirror


Pragmatic JUnit Testing

Will Gwaltney writes "The Pragmatic Programmers have done it again. This time it's with volume 2 of their Pragmatic Starter Kit series, Pragmatic Unit Testing in Java with JUnit. In the friendly, accessible style we've come to expect from Andy Hunt and Dave Thomas, the book provides a gentle introduction to unit testing in general and the JUnit testing package in particular. Of course, by the time you've read through the introduction you're hooked, at which point Andy and Dave give you enough information to make you not just a good unit tester, but a better developer to boot." Gwaltney offers this disclaimer: "I know both authors and was one of the proofreaders for this book. I have no financial stake in either the book or their company." With that grain of salt, read on for the rest. Pragmatic Unit Testing in Java with JUnit author Andrew Hunt and David Thomas pages 159 publisher The Pragmatic Bookshelf rating 10 reviewer Will Gwaltney ISBN 0974514012 summary Unit Testing introduction, tutorial, and developer's guide

What's the Approach? You can go a long way in the unit testing world with this book. It's a great starter guide for learning about unit testing; the first chapter has several small sections which answer many of the questions a novice might have about the subject, including some of the most common objections to unit testing. Then the book launches into a detailed user guide to the JUnit package, including some useful but lesser-known topics like writing your own custom asserts.

After you've got the basics down, the book spends several chapters drilling into the concepts, considerations, and best practices for writing good tests. This section of the book is quite practical, and is where the true worth of the book comes out. Any developer who reads this section will come away better for it even if they never consistently write unit tests. It's amazing how many testing issues turn out to be development issues at their core; this section of the book points some of them out. Simply being aware of these issues can help you to avoid a number of common pitfalls.

Finally, the book discusses common unit testing problems and their solutions. This section is a lot of fun because of the anecdotes that Andy and Dave include; unfortunately some of them I recognized from my own experiences as a developer. (What, me do that? No, uh, of course not!)

Who's it For? This book is primarily for developers (and testers who are intimately involved in the development process), since unit testing per se is primarily a developer task. Depending on the organization of the software shop, by the time code gets to a formal testing or QA group it's probably too late to apply unit testing methods effectively. (Note I said unit testing methods here.) The book is ideal for the unit testing novice, and there's a lot of good information here for more advanced developers to ponder.

The book is targeted at those who want to learn "how to think about tests, how testing affects design, and how to handle certain team-wide issues you may be having" (from the Preface). In particular the book is an excellent source of information for those developers who have recently moved into the Java area and are looking for a good testing framework.

No matter what your role in the development process, this book can help give you a fresh perspective into unit testing and related issues. It prods you to think about issues that perhaps you hadn't considered before.

Pros As you can probably tell, I like the Pragmatic Programmer writing style. It's informal and friendly without being chatty or wordy, and the main text moves along well. More technical discussions are reserved for sidebars, and there are "Joe Asks..." sections throughout the book that anticipate questions that Joe Programmer might ask about the current topic.

The examples in the book are excellent. Many books err on the side of code snippets that don't have enough context to be understandable, or multi-page code extravaganzas that bury the details of the topic at hand under mountains of cruft. I found the examples here to give just the right amount of detail to understand what's going on. And the source for all the examples is available from the author's web site.

Cons What this book is not is a reference manual for JUnit. The book doesn't include a javadoc listing for the JUnit package, nor does it have a section that digs into JUnit's internals. This is not necessarily a Bad Thing because that information is available from the JUnit web site, but it does bear mentioning.

If there was anything I found to complain about it was the use of acronyms in the concepts section of the book. I'll admit right here that I'm no lover of acronyms, so I felt that using things like Right-BICEP, CONNECT, and A-TRIP as mnemonics for the concepts was a bit over the top. (Read the book to see what I mean.) However, I have to admit that they help me remember the concepts discussed, so I guess they do their job (grumble).

Why Should I Read this Book?

If you hate testing, this book will very probably change your mind, and you'll have fun in the process (the stories are great!). And even if you still hate testing after you've finished the book, you will have enhanced your development skills enough to be able to delude yourself that testing isn't necessary for you (well, maybe not that much). At the very least, this book will make you aware of the various issues that proper testing can expose and maybe help you try to avoid them. By shining a light into these areas, it can still make you a more effective developer... maybe effective enough to realize how much more solid your work could be with this type of testing coverage.

If you love testing, this book will remind you why you began the love affair. Your love will be strengthened and deepened, and the tests you write will undoubtedly get better, too.

For Java developers specifically, this book will get you started writing JUnit tests painlessly and effectively and will take you a long way down the road to unit test proficiency. It will give developers in any language quite a bit to take "back to the terminal" regarding design and process as well as testing. An excellent and much needed book.

Like volume 1 of the Pragmatic Starter Kit series ( Slashdot Review), this book is available from the Pragmatic Programmer website. The printed version is $29.95 and there's a PDF version available for $20.00. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

10 of 118 comments (clear)

  1. Pragmatic? by Anonymous Coward · · Score: -1, Troll

    Slashcode uses FAG-matic testing.

    Thanks for listening to this first post attempt.

  2. Wonderful book by grub · · Score: 1, Troll


    Pragmatic JUnit Testing was the quantum leap I needed. Because of Pragmatic JUnit Testing, the lateral shift to JUnit was perfect. It wasn't a complete shock thanks to Pragmatic JUnit Testing. Overall I'd say it wasn't so much a complete paradigm shift but thanks to Pragmatic JUnit Testing, it helped me think outside the box.

    Three cheers for Pragmatic JUnit Testing!!

    Hip hip.. HOORAY!

    --
    Trolling is a art,
    1. Re:Wonderful book by donnyspi · · Score: 1, Troll

      If I had mod points left, i'd give you -1 for using the phrase "paradigm shift". That is the most tired, overused phrase in the book.

    2. Re:Wonderful book by r.jimenezz · · Score: 0, Troll

      Searched the web for pragmatic junit testing. Results 1 - 10 of about 2,540. Search took 0.25 seconds 1. The Pragmatic Programmers, LLC Sorry dude, will have to repeat "Pragmatic JUnit Testing" more in your next post...

      --
      The revolution will not be televised.
  3. YOU ARE WORTHLESS SHIT by Anonymous Coward · · Score: -1, Troll

    Moderators, express your consent by modding this offtopic

  4. Java by Tyler+Eaves · · Score: -1, Troll

    So what does a JUnit call look like

    JUnit unit = new JUnit.TestFramework.SimpleTest()
    unit.setExpected Result(4)
    unit.setPassMessage('Okay, exponention works...')
    unit.setFailMessage('Uh oh...')
    NumberFormatter format = new NumberFormatter()
    format.setDecimalDigits(1)
    for mat.setLocaleFractionalSeperator('.')
    unit.setFor matter(format)
    unit.runTest(Math.pow(2))

    Seriously, kids, java is bad for you...

    --
    TODO: Something witty here...
  5. Unit tests are a bad idea by Anonymous Coward · · Score: -1, Troll
    I'm posting anonymously for obvious reasons.

    I'm a teaching assistant (TA) at Harvard University, and I mark a lot of Java code as part of a job that supports my PhD studies. Code submitted to me with unit tests face an automatic 5 mark penalty, before i even begin marking. There are several reasons for this.

    1. Extra code == extra bloat. The unit test code is submitted as an extra class file, which means there is one more thing that I have to read over before I can judge their code on its merits. I find that unit tests are a pain to compile, normally found deep in a directory structure where they make little sense.
    2. Too much documentation. People who submit unit tests generally document the interfaces using javadoc, which just increases the amount of reading I have to do before I can assign a mark. HTML as a documentation format is stupid. There is nothing wrong with submitting code with comments at the start of a function or method and leaving it with that. If the code needs documentation because it is doing something non-obvious, it is time to rethink the code.
    3. Unit tests are written with no extra skill than the code they test. Unit tests induce a level of confidence in the programmer that their code works as a black box. This is a bad idea. Programmers should understand why each and everyone of their modules works the way it does, not assume that everything is well because it passes some rudimentary test that was created with the same skill level as the rest of their code.
    As you can see, I'm against unit testing. I've tried my hardest to get the profs to teach i386 assembly and then C so that students understand how a computer works, but to no avail. If we are to regain our jobs from the Indians, we must be more professional coders.
    1. Re:Unit tests are a bad idea by Pfhreakaz0id · · Score: 0, Troll

      My thoughts exactly. Before that was my first thought "who the hell, exactly, has time for this in a real job?"

  6. huh by Anonymous Coward · · Score: -1, Troll

    junk characters? what the fuck is your problem? are you some sort of character bigot? i mean, really. what the fuck is wrong with - and ( and all their friends? are they somehow inferior to e and a and such? do you hate niggers, boy? is that your goddamned problem?

  7. MOD PARENT DOWN - TROLL by Anonymous Coward · · Score: -1, Troll

    It's one huge, stupid hyperbole until you get to "And why your application is significantly slower then non unit tested apps.", which is a straight-out lie.