*
The moose likes Testing and the fly likes Writing many tests to test one method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Writing many tests to test one method" Watch "Writing many tests to test one method" New topic
Author

Writing many tests to test one method

Swerrgy Smith
Ranch Hand

Joined: Mar 26, 2010
Posts: 88
Hi all,

Suppose I want to test the method doSomething() of the class Biz; I create a test class BizTest.
If I want to write many test methods to test doSomething(), how should I name these methods? I am thinking of some thing like: testDoSomething_1(), testDoSomething_2() ... but it will look like a automatically generated test (and not a test written by a person); furthermore, it will not tell the nature of each test (e.g. what the test method testDoSomething_1() is supposed to do).

What do you think about that? Or should I write only 1 method testDoSomething() and put a lot of assertxxxx inside?

Thanks for your advice.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You should name your test methods based on what you're attempting to assert. For example, suppose your class Biz has a method called calculateTotal(). You might name your test methods as such:



Test methods should be as well defined as your methods being tested. testCalculateTotal_xxx is a bad idea.


GenRocket - Experts at Building Test Data
Swerrgy Smith
Ranch Hand

Joined: Mar 26, 2010
Posts: 88
Gregg Bolinger wrote:You should name your test methods based on what you're attempting to assert. For example, suppose your class Biz has a method called calculateTotal(). You might name your test methods as such:



Test methods should be as well defined as your methods being tested. testCalculateTotal_xxx is a bad idea.


Thanks for your excellent advice, Gregg.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

If you're using JUnit 4 you can make your test names even better by removing "test". I use a convention based on Neal Ford's suggestion for test names (Slide 44, http://www.slideshare.net/guestebde/10-ways-to-improve-your-code-neal-ford):


With a little effort to arrange and name your test methods with some consistency, the test report can read very nicely


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing many tests to test one method
 
Similar Threads
New SCJP mock exam
Best Practice for getters and setters
difficult Construction
UrlyBird 1.3.2 locking with no cookie
How does a Java program run?