views:

427

answers:

7

For writing unit tests, I know it's very popular to write test methods that look like

public void Can_User_Authenticate_With_Bad_Password()
{
...
}

While this makes it easy to see what the test is testing for, I think it looks ugly and it doesn't display well in auto-generated documentation (like sandcastle or javadoc).

I'm interested to see what people think about using a naming schema that is the method being tested and underscore test and then the test number. Then using the XML code document(.net) or the javadoc comments to describe what is being tested.

/// <summary>
/// Tests for user authentication with a bad password.
/// </summary>
public void AuthenticateUser_Test1()
{
...
}

by doing this I can easily group my tests together by what methods they are testing, I can see how may test I have for a given method, and I still have a full description of what is being tested.

we have some regression tests that run vs a data source (an xml file), and these file may be updated by someone without access to the source code (QA monkey) and they need to be able to read what is being tested and where, to update the data sources.

+2  A: 

Personally I prefer using the long method names. Note you can also have the method name inside the expression, as:

Can_AuthenticateUser_With_Bad_Password()
eglasius
+17  A: 

I prefer the "long names" version - although only to describe what happens. If the test needs a description of why it happens, I'll put that in a comment (with a bug number if appropriate).

With the long name, it's much clearer what's gone wrong when you get a mail (or whatever) telling you which tests have failed.

I would write it in terms of what it should do though:

LogInSucceedsWithValidCredentials

LogInFailsWithIncorrectPassword

LogInFailsForUnknownUser

I don't buy the argument that it looks bad in autogenerated documentation - why are you running JavaDoc over the tests in the first place? I can't say I've ever done that, or wanted generated documentation. Given that test methods typically have no parameters and don't return anything, if the method name can describe them reasonably that's all the information you need. The test runner should be capable of listing the tests it runs, or the IDE can show you what's available. I find that more convenient than navigating via HTML - the browser doesn't have a "Find Type" which lets me type just the first letters of each word of the name, for example...

Jon Skeet
we comment all of our code
Bob The Janitor
we have some regression tests that run vs a data source (an xml file), and these file may be updated by someone without access to the source code(QA monkey) so they need to be able to read what is being tested and where, to update the data sources.
Bob The Janitor
So make the test name reflect what's being tested :) Surely that's better than requiring a whole extra set of documentation to be generated.
Jon Skeet
(Although to be honest, it doesn't really sound like it's a good idea to let people who don't have access to the test *source* change the test *data* in the first place.)
Jon Skeet
DBAs change the production data with out access to the production source, the idea is you create a validation method test, and the QA people add data to see if the validation method handles it correctly
Bob The Janitor
the Developers add the basic expected constant and then the QA people try and break it
Bob The Janitor
So why not let them have access to the source? That would make it easier for them to try to break it. Why rely on documentation which can so easily be wrong?
Jon Skeet
Agree with Jon. If you give the QA team access to the source code and if they understand it, then they can find additional flaws in your code (edge cases, spelling, unused methods, ...). Source control will show if they have modified some file.
Petar Repac
White box testing vs black box testing, Black box Testing is where they are testing vs the spec with no knowledge of the how, if x goes in y should come out everything in the middle is a black box, if the QA people see/understand the code then they become tainted, and it no longer back box testing
Bob The Janitor
I'd argue that seeing the descriptions of the developer tests (rather than just the documentation for the API calls themselves) already has that effect. You talk about testing against the spec - are you saying the docs for the tests (not the tests themselves, nor the docs for the API) is the spec?
Jon Skeet
Take the use case that specifies a validation method for zip/postal code in the US and Canada, the methods accepts a string of 5 chars, that are all numeric, or 6 chars that are letter and numbers in the correct format. so you have a test that loops though a collection of good then bad zip codes.
Bob The Janitor
And that's absolutely fine - but real black box testing should consist of the QA department seeing that spec and generating test data themselves. Why should they be influenced by what unit tests the devs have written? (Continued)
Jon Skeet
Jon Skeet
+2  A: 

I suggest smaller, more focussed (test) classes.

Why would you want to javadoc tests?

Tom Hawtin - tackline
it makes it a lot easier to read though what it being tested, we code comment all of our code.
Bob The Janitor
+1  A: 

What about changing

Can_User_Authenticate_With_Bad_Password

to

AuthenticateDenieTest
AuthenticateAcceptTest

and name suit something like User

Mykola Golubyev
+5  A: 

Does the documentation show up in your test runner? If not that's a good reason for using long, descriptive names instead.

Personally I prefer long names and rarely see the need to add comments to tests.

Brian Rasmussen
A: 

As a Group how do we feel about doing a hybrid Naming schema like this

/// <summary>
/// Tests for user authentication with a bad password.
/// </summary>
public void AuthenticateUser_Test1_With_Bad_Password()
{
...
}

and we get the best of both.

Bob The Janitor
+3  A: 

I've done my dissertation on a related topic, so here are my two cents: Any time you rely on documentation to convey something that is not in your method signature, you are taking the huge risk that nobody would read the documentation.

When developers are looking for something specific (e.g., scanning a long list of methods in a class to see if what they're looking for is already there), most of them are not going to bother to read the documentation. They want to deal with one type of information that they can easily see and compare (e.g., names), rather than have to start redirecting to other materials (e.g., hover long enough to see the JavaDocs).

I would strongly recommend conveying everything relevant in your signature.

Uri