7

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?

3

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.");
| improve this answer | |
  • 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
2

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.

| 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

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