Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The API docs for Test::More::ok is

ok($got eq $expected, $test_name);

right now in one of my apps I have $test_name print what the test is testing. So for example in one of my tests I have set this to 'filename exists'. What I realized after I got a bug report recently, and realized that the only time I ever see this message is when the test is failing, if the test is failing that means the file doesn't exist.

In your opinion, do you think these $test_name's should say what the test means if successful? what it means if it failed? or do you think it should say something else? please explain why?

share|improve this question
1  
What language / platform is this? I'm not familiar with this syntax. –  Marcie Jan 4 '11 at 15:37
    
@marcie sorry, it's Perl, but this is intended to be generic, and more about what people think would want to see output as a message. Test::More is now a link to perldoc. –  xenoterracide Jan 4 '11 at 15:43
    

2 Answers 2

up vote 2 down vote accepted

I try to name my tests after the success case (C# syntax below).

[TestMethod]
public void BeginReviewPopulatesReviewer()
{
    Distribution = TestData.SubmittedToReviewerDistribution();
    Distribution.BeginReview(TestData.Marcie);
    Assert.IsNotNull(Distribution.CheckWire.Reviewer);
}

If I need to put a specific message to the console, I put the description of the failure:

Assert.IsNotNull(Distribution.CheckWire.Reviewer, "Reviewer is null.");
share|improve this answer
    
+1 for self-documenting code. –  David Jan 4 '11 at 16:24
    
I would add that the message to be set should probably include the set of parameters that caused that particular failure. It's great to check the logs and spot that a test failed, but much easier if the reason is clearly laid out (although in your test, since it's a binary state, it's not really useful...) –  Matthieu M. Jan 4 '11 at 19:54
    
My goal (not always met!) is to write each test so that there's only one reason for it to fail. –  Marcie Jan 4 '11 at 19:55
    
I think that in Test::More this is really more of Description of failure, than the test name, even though the documentation says otherwise. in other words equivalent to where you put "reviewer is null" –  xenoterracide Jan 6 '11 at 6:31
    
That sounds right to me. –  Marcie Jan 6 '11 at 14:18

Following Roy Osherove's guidelines I use the three part naming convention Method_Setup_ExpectedOutcome. This would mean that the test name says what I expect a certain method to behave like under specified outcomes, i.e. the successful outcome of the test.

However, a lot of unit testing frameworks allow you to add additional messages to an assertion. That message will usually be displayed when the assertion fails. In this case I try to word the message to reflect what made this specific assertion fail.

So... for test names I go for the success case, in messages I go for the fail reason.

share|improve this answer
    
I think that in Test::More this is really more of Description of failure, than the test name, even though the documentation says otherwise. –  xenoterracide Jan 5 '11 at 10:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.