Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Writing many tests to test one method

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Swerrgy Smith
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 16921
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I think she's lovely. It's this tiny ad that called her crazy:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic