Test Review #1 - NerdDinner

I’ve decided to start doing some test reviews of tests that I see in the wild. I figured it’s the best way to show people what I mean when I say they do not have readable, maintainable or trustworthy tests.

The first episode is the review of the tests from the NerdDinner MVC source code. It’s 30 minutes long. and it was shot at 2AM, so I’m quite cranky. But the tests sure don’t try to ease my mind, and only make me crankier.

If you have tests you’d like me to review send an email to Roy AT osherove.com with the subject “Test Review [project name]”

problems that are dealt with in this video:

  • Naming conventions
  • Magic strings in tests
  • Hard coded values in hand written stubs
  • Magic assertions (expecting a value that no one understand why it should be that value)
  • Lack of tests
Published Friday, March 20, 2009 7:45 AM by RoyOsherove

Comments

Friday, March 20, 2009 9:03 AM by Joshua Flanagan

# re: Test Review #1 - NerdDinner

I think you would get your message across better if you turned this into a blog post that focused on the real issues. The video is too long, with too much fluff. The discussion of the AAA comments is silly - yes I know its late and your cranky - so why dump that on the rest of the world? And then complaining about the test naming because it follows the JP (as you called it) style instead of your own personal style seemed petty, especially since you have little company with that opinion.

I didnt think the content had any value until you got to the ChangePasswordGetReturnsView test, and that was about 10 minutes in. By that point, my spare time was used up, so I turned it off. You had legitimate complaints that someone could learn from - but theyre buried. A quick blog post that takes a few of the bad tests and discusses the issues would be much more valuable.

Friday, March 20, 2009 9:26 AM by ruairiSpain

# re: Test Review #1 - NerdDinner

This is great...please continue.  Unit Test are a hot topic but most people don't have a formal process.  I'll buy your book!

Friday, March 20, 2009 9:28 AM by chrissie1

# re: Test Review #1 - NerdDinner

Thanks, nice review. We want more. Well, I do anyway.

Friday, March 20, 2009 9:34 AM by Kenny

# re: Test Review #1 - NerdDinner

Great video, especially for a test-newbie like me. I think there's need to be more videos/podcasts/whatever that leads people like me that i've started with TDD and grasps the basic but has a hard time implementing it correctly.

Friday, March 20, 2009 9:38 AM by Jim Holmes

# re: Test Review #1 - NerdDinner

Awesome practical walk through of your philosophy around unit testing. It helped me realize I've got areas I can work to improve.

Thanks!

Friday, March 20, 2009 10:09 AM by Chris

# re: Test Review #1 - NerdDinner

This was fun to watch and educational. I think you've stumbled upon a great form of geek entertainment.  Next time, give yourself 100 paper cuts before reviewing the tests.

Friday, March 20, 2009 10:15 AM by Frank Halltorp

# re: Test Review #1 - NerdDinner

Awesome video! It was really good and I hope you will create more of these!

Friday, March 20, 2009 10:25 AM by Duncan Butler

# re: Test Review #1 - NerdDinner

I have to agree with Joshua, you would have been better off going to bed and sleeping, before making the video.

Which is a shame because there are things you show where people can learn a better way, writing tests is hard, and mistakes are common, I have made them in the past and probably will in the future, but please remember that pointing out an error is one thing, shooting someone for an error, even with words, is another.

As for the naming, I actually quite like JPs naming conventions, but If you were to look at his tests the class provides the context, which means that you don't get repeating contexts in the names of the tests, each test name is read with the class name as the start of a sentence, its not incorrect, but it is different to your style of naming, both are valid, it is a matter of preference, like the test framework to use.

Friday, March 20, 2009 11:25 AM by Gavin Miller

# re: Test Review #1 - NerdDinner

Enjoyed the video and learned quite a bit.

Friday, March 20, 2009 11:30 AM by Taswar Bhatti

# re: Test Review #1 - NerdDinner

I am not sure if I agree with Joshua comments, sometimes a video creates a more visual way of describing things and while going through it than a blog. And I appreciate Roy creating one the effort of it counts, even if he is cranky :) Yes dime cast might have 10 mins cast but to do code review you don't want 10 mins, might as well just use Resharper clean code, if you want something quick.

Also one has to remember that just having a blog or being loud does not make someone authoritative in it. Roy had some very valid points in the naming of the test, one form or the other (JP or RO) consistency is the main thing, as you can see some with _ (underscore) and other test name without _

One should really think about the written test code quality, its not just if the test runs everything is good, if you care about your craft one should care about the unit test they write. I have seen test that says ThisShouldBeTrue, and you just wonder about naming, what is true, why its true, etc etc.

Just my 2 cents :)

Friday, March 20, 2009 11:32 AM by Willie T

# re: Test Review #1 - NerdDinner

...and that sucks.

Lol!  I thought it was a good video, some good tips on how to write tests.

From the comments I thought I was going to see you screaming or something.  I don't think you were that harsh.  If anything I'd say stop apologizing in the video.  I'd bet that Scott and team have a thick enough skin to view it as well. I think it's their way of a peer review.  Why on earth they don't ask (or pay) for you (or others that are experts) to do review a beforehand is way beyond me as their stuff is kind of the "authority" and you calling them out can only be good not only for them but the community as well.

Friday, March 20, 2009 11:36 AM by DotNetShoutout

# Test Review #1 - NerdDinner - Roy Osherove's Blog

Thank you for submitting this cool story - Trackback from DotNetShoutout

Friday, March 20, 2009 12:03 PM by Andy

# re: Test Review #1 - NerdDinner

Great review Roy, ashamed it was only half hour long but I guess you do have to sleep inevitably. Looking forward to you doing alot more of these!

I disagree with Joshua. You're entitled to say what you feel, and for however long you feel. And it kept me interested to the end.

Friday, March 20, 2009 12:08 PM by Arielr

# re: Test Review #1 - NerdDinner

Good stuff. Do get some sleep beforehand.

And I'd also agree (not with you, with the test writer) with the whole AAA comments there: it provides structure, just like the naming of the test.

Also - regarding the double assertions - I agree that a test should test only one thing, but if that one thing has several valid and invalid properties? Like, if a string must be over 4 chars long and also begin with a uppercase letter. Would you not test both assertions in one test?

Friday, March 20, 2009 12:27 PM by Aaron Jensen

# re: Test Review #1 - NerdDinner

Some comments while watching:

2:02 - DinnerTest vs DinnerTests - a bit pedantic eh?

2:15 - usign R# - I use R# and sometimes have unused namespaces. Silly thing to point out.

2:50 - Again, DinnerTest vs DinnerTests, I prefer Tests as well between the two, but it's a style thing.

3:09 - Naming_Like_This - No, the only thing that naming has in common w/ JP is the fact that it uses underscores. Personally I think Pascal_Boxcar is an abomination, but that's a style thing again. Also, JP and many others are currently using context/specification naming, which this is *similar* to, but still quite different. Comparing the two only shows a lack of understanding for c/s and how much of the community is doing testing.

3:46 - Naming_ShouldAlways_LookLikeThis - We used to use your naming style, I think it helped us reach the understanding that led us to appreciate context/spec. I think your naming style is a step up from what we did 4 years ago, but it is a large step down from the naming that was already evident in DinnerTest and an even larger step from proper context/spec naming. I think replacing it with your own style makes the test read worse and attempts takes us back a few years in learning.

5:50 - AAA cynicism - Eh, I agree that the comments are silly, but I think/hope it's a teaching thing and it provides some structure. If only there were a way to test where that structure was enforced...

6:50 - Good point about which are invalid and which aren't, but I think this is a naming issue. If the test name pointed out the ContactPhone was invalid this wouldn't be an issue.

9:36 - Magic strings in tests - Oh no. Your explanation of why you shouldn't use "magic strings" in tests is completely irrational. "Is that the only logical phone there is?" What? You think that if you hard code strings you'll have all tests using the same values? Isn't that an issue with using a GoodPhoneNumber constant? I think you have your argument reversed. There is no issue with magic strings in tests. They increase readability and lead to a quicker understanding while scanning tests. Replacing things with "a" is silly as well. What does that buy you over usint "Test title"? I would argue that using a more realistic value is better especially while test driving.

10:47 - DateTime.Minimum vs DateTime.Now - I wouldn't get people in the habit of using DateTime.Minimum or they might try to use it in a test that hits the database and have SQL throw up in their face. I agree that DateTime.Now should be avoided, especially in tests that have multiple DateTime values that may be related. In that case I would use DateTime.Parse with a "magic string" date.

12:00 - ChangePasswordGetReturnsView - Yeah, the naming diverged as did the sanity of this test. This is a horrendous test.

15:00 - stopped watching, gotta get stuff done.

Friday, March 20, 2009 1:55 PM by Jeff

# re: Test Review #1 - NerdDinner

Thanks for the review. I learned several things.

I am curious on what software you were using to do the focus and zooming.

Friday, March 20, 2009 2:02 PM by Roger

# re: Test Review #1 - NerdDinner

LOL, Harsh but educational.  I think this is an excellent format for presenting this much detail.  Keep it up!

Friday, March 20, 2009 3:05 PM by csharptest

# re: Test Review #1 - NerdDinner

LOL, harsh but educational.  I think video is a great format for diving into this much detail.

Clearly we write tests in very different ways, some of your suggestions I like.  One I'm having a lot of difficulty with:

"Use only one Assert per method."

I just can't see myself ever doing that, my test code tends to walk through several uses asserting anything and everything before during and after...  Is this really so bad?

Friday, March 20, 2009 3:57 PM by James Bach

# re: Test Review #1 - NerdDinner

I like that you criticize obscure magic strings. But you lost me when you criticized the use of values that change.

Using changing values is a way to inexpensively increase test data coverage. I would much rather have a test that sometimes fails and sometimes passes, than to have a test that is highly likely to not discover the same problems it didn't discover the first time I ran it.

Automated testing already suffers from the poverty that comes from using narrow automated oracles. Let's not compound that by reducing data coverage based on the principle of "don't discover anything new or unexpected."

If you are worried about intermittency, then use logging to preserve the exact values used in each test run.

Friday, March 20, 2009 6:53 PM by Ekus

# re: Test Review #1 - NerdDinner

GREAT INTRO.

Liked:

+ stubs vs. mocks

+ suggestions for naming conventions

+ warning about assumptions, setting stub's properties during "arrange" phase and testing it later (versus magic values)

+ good quality video, speech, webcam

Not so much:

- _forcing_ own naming convention

- removing AAA comments (not everybody writes and reads unit tests every day, I personally will start adding AAA comments so my mates can follow better)

- repeating complaints and a bit too harsh towards end. Surely good attitude, just a tiny bit too much.

Overall - very valuable. Funny how it's impossible to find flawless code and unit tests, even from pros.

Keep up the good work!

Friday, March 20, 2009 10:31 PM by Arielr

# re: Test Review #1 - NerdDinner

James -

Regarding tests which sometimes fail - I believe you're wrong. I seem to remember most of the tests I wrote (or saw) which sometimes fail as "ignore if possible" (if the test doesn't break the build - ignore it, if it does - remove it).

I have a big issue with the claim that a "sometimes" test can help you find bugs. That's BS - classic unittests are meant to describe one test scenario and provide you with the safety (regression) net of having it run all the time. You should never expect it to do more, unless you have the specific intent of generating "random" inputs ("fuzz testing").

The problem with having "sometimes" tests is their irreproducibility. You NEED to be able to re-run a failed test and have it fail again in order to fix the bug, and that claim stands true for fuzz tests - you need to keep a failed input as a regression input for later tests.

Friday, March 20, 2009 11:31 PM by Review of Roy Osherove’s review. « Vadim’s Weblog

# Review of Roy Osherove’s review. « Vadim’s Weblog

Pingback from  Review of Roy Osherove’s review. « Vadim’s Weblog

Saturday, March 21, 2009 12:39 AM by Jason Perry

# re: Test Review #1 - NerdDinner

I loved the video.  Don't listen to the "blog about it" comments.  There are tons of blogs, but how many video code reviews are there?

Makes me want to get the book.

I say, just keep picking out things from the top projects on CodePlex and go for it.  Awesome.

Saturday, March 21, 2009 2:13 AM by Ryan Anderson

# re: Test Review #1 - NerdDinner

Nicely done. I think @Chris nailed it, pure geek entertainment for me...

Saturday, March 21, 2009 3:57 AM by Jeff Brown

# re: Test Review #1 - NerdDinner

Good idea; poor execution.

There were some laughs "Check something for god's sake", and some good ideas, but overall I think your message got lost in a sea of sleep-deprived irritation.

A project like NerdDinner that aims to teach others should be held to the highest standards of quality.  Likewise those who comment on it ought to show the highest levels of professionalism, respect and courtesy if they wish their contribution (of critique / of advice) to have a positive impact.

I do think that test reviews are worth doing.  Certainly they can be entertaining!  But I think you should spend a little more time upfront preparing and be careful not to let your emotions and initial reactions get in the way of the lesson.

Perhaps you could put together a series of timeboxed video reviews.  Make them no more than 5 minutes each.  Spend 30 minutes preparing beforehand so you can really focus on just a few concerns here and there.  Be sure to highlight good or distinctive practices just as you recommend improvements.

One thing that would be really cool in a review would be to actually try to run a couple of tests on supposedly well-covered code.  Then make a few reasonable and unreasonable changes to the implementation and see what kinds of things they catch.  You might explore simple refactorings, fault injection and feature addition.  What happens to the tests?  Do they still compile and run properly?  Any false positives?  How about false negatives?  Permissive tests are often bad.  Brittle tests are usually worse.  It's a tough balance.

BTW, you might be interested to know that MbUnit has two mechanisms for tolerating multple asserts in a test.

#1. Put [MultipleAsserts] on the test.

#2. Use Assert.Multiple(() => { .... }); in the test body.

In each case, the test will report assertion failures, keep running until they finish or throw some other kind of exception, then fail.

This is good for AAA-style tests where the Action part yields a composite result that requires multiple Asserts to verify completely.  Of course it's important that the test still remains focused on just one scenario in a context.  We don't want multiple asserts on completely unrelated behaviors!

Saturday, March 21, 2009 9:50 AM by Steve

# re: Test Review #1 - NerdDinner

Excellent video for test newbie!

I found this site from a link on Scott Hanselmans facebook profile - did you get any feedback from the guys that wrote the code?

Saturday, March 21, 2009 11:43 PM by ISerializable - Roy Osherove's Blog

# Test Review #2 – ASP.NET MVC Unit Tests

See other reviews: Review #1: NerdDinner Here’s the second video review of Unit Tests. This is another

Sunday, March 22, 2009 1:37 AM by ASP.NET MVC Archived Blog Posts, Page 1

# ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

Sunday, March 22, 2009 3:38 PM by Helper Code

# Test Reviews at ISerializable

It’s hard to start TDD and using Unit Test without a good mentor.  Although writing unit tests seems

Monday, March 23, 2009 1:58 AM by ISerializable - Roy Osherove's Blog

# Test Review #3 – Unity

Watch previous videos: Test Review #1 – NerdDinner Test Review #2 – ASP.NET MVC     In this

# Reflective Perspective - Chris Alcock » The Morning Brew #312

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #312

Tuesday, March 24, 2009 11:37 AM by cowgaR

# re: Test Review #1 - NerdDinner

your naming of tests really sux man, you can't be serious...

otherwise, nice video

Wednesday, March 25, 2009 7:06 AM by Dariusz quatscht

# Test Reviews by Roy Osherove

Roy Osherove hat auf seinem Blog angefangen Projekte, genauer gesagt deren Unit Tests, zu analysieren

Wednesday, March 25, 2009 11:23 AM by Tests should last forever | Tigraine

# Tests should last forever | Tigraine

Pingback from  Tests should last forever | Tigraine

Thursday, March 26, 2009 7:44 AM by Thomas

# re: Test Review #1 - NerdDinner

Thanks for pointing out these issues. I am starting to work the test driven way now and just downloaded the NerdDinner sample application and would have made some stupid errors I am now aware of. Thanks.

Friday, March 27, 2009 3:39 PM by Kevin Jones

# re: Test Review #1 - NerdDinner

Not sure if it's exactly the same as you meant but VSTS testing does let you run one test multiple times with different parameters (row test I think you called it). Check out data driven testing. In this you specify a database (which can be a CSV file), for each row in the database the test is called. This is all set up with attributes on the test and is reasonably straightforward to do

Monday, March 30, 2009 7:07 PM by peitor

# re: Test Review #1 - NerdDinner

Hi Roy,

love your work.

I miss the RowTests in Visual Studio too!!

We did RowTests using Data-Driven Tests; DataSource attribute with an Input file (our rows) and then reading TestContext.DataRow

   [TestMethod()]

   [DeploymentItem(@"righeInvalid.txt")]

   [DeploymentItem(@"schema.ini")]

   [DataSource("System.Data.OleDb", @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.;Extended Properties='text;FMT=TabDelimited;HDR=YES'", @"righeInvalid.txt", DataAccessMethod.Sequential)]

   public void IsInValid_LoadRows()

   {

   //snip snip

   }

Monday, April 13, 2009 7:16 AM by Lisa-Marie

# re: Test Review #1 - NerdDinner

Keep it up! I loved it... informal down to earth what your opinion are.. then it is up the eachone to decide to agree or not.

I am glad you mentioned it was late and that you were talking silently because your family is a sleep... I was actually wondering if this would be the case. Now I have seen a few presentations from you and kind of got the feeling on your humor and how you think and speak so I found this video entertaining and educational. I can understand why some here got irritated and annoyed... but that is there choice. I choose to see it in a different light and a very glad someone even bother to make this type of videos so that I don't have to read yet another blog post... there are enough blogs around anyway.

Monday, April 13, 2009 10:04 AM by Radenko Zec

# re: Test Review #1 - NerdDinner

Nice review.Your naming conventions are great!!!

I think I will buy your book.

Tuesday, May 19, 2009 4:21 PM by Neal

# re: Test Review #1 - NerdDinner

Thank you for the great web site - a true resource, and one many people clearly enjoy.

Thursday, July 09, 2009 2:49 AM by Scott Muc

# Book Review - The Art of Unit Testing by Roy Osherove

In a recent splurge I purchased 4 books from Manning Pres s with one of them being The Art of Unit Testing by Roy Osherove. I would categorize myself as relatively new to unit testing. My first hands on testing was when I took the Nothing but .Net bootcamp

Wednesday, September 02, 2009 11:48 AM by Unit Test Videos « Legalize Adulthood!

# Unit Test Videos « Legalize Adulthood!

Pingback from  Unit Test Videos « Legalize Adulthood!

# Learning ASP.NET MVS through NerdDinner. « Implementation Details

Pingback from  Learning ASP.NET MVS through NerdDinner. « Implementation Details