When you are under pressure to ship a feature, every little detail that makes your coding process faster counts. Especially when it’s about writing tests. For example, if you are one of the many people who don’t use TDD, you first write a class, iterate a few times over the initial implementation and when you’ve reached a point where you are satisfied, you are ready to write a test for it.
With TestNG and Eclipse, writing tests just became easier.
The “New TestNG class” wizard has just received a new page that looks at the Java file that’s currently in your editor and then allows you to select public methods in it. Methods selected in this page will be automatically included in the generated test class, with a default implementation that throws an exception, so you don’t forget to implement it:
The wizard is also pretty clever in trying to stay as much out of the way as possible. For example, it will automatically detect the package, come up with a test class name and, more importantly, it will guess what is the most appropriate directory for your test class. For example, if you are using Maven, this directory will be src/test/java:
#1 by Elliotte Rusty Harold on January 19, 2011 - 7:21 am
So you’re generating the tests from the code? That’s backwards. Tests should be written before the model code, and as a nice side benefit Eclipse will then autocorrect the model code to match the tests, no plugin needed.
#2 by Cedric on January 19, 2011 - 7:27 am
Elliotte:
“Backwards”?
Now, that’s harsh. Not everyone uses TDD, and actually, I’ll argue that very few people use TDD. At any rate, suggesting that TDD is the only way to write tests is quite a strong statement that I don’t think is supported by facts nor practicality.
I find that most developers like to iterate over their code at first in order to get to a point where the code is not a total mess, and only then, they decide to write their first test. Before reaching that point, you’re just writing tests for throwaway code, which is a waste.
I find this approach quite reasonable and it doesn’t suffer from all the problems that plague TDD.
#3 by João on January 19, 2011 - 11:12 am
Hi.
Thank you a lot for the new features. Unfortunately they don’t seem to work that well with me.
The methods page only works if the editor has “focus”. For example, if by any chance the package view has focus, and then I invoke the wizard, the methods page will be empty.
When it works, the methods list is filled with the public methods of all the editors.
An alternative to this, that I think would be more intuitive, would be adding a new option, to the TestNG context menu, to generate a test to from the selected java file.
Not sure if it’s because I’m in Linux, but the directory src/test/java isn’t detected in a maven project.
Sorry for the crappy bug report and thank you for the new features.
#4 by Tomek Kaczanowski on January 19, 2011 - 11:40 am
Hi Cedric,
first I have to admit, I never use TestNG plugin to generate test class. So I my comment is a little bit out of topic, sorry 🙂
My question is of more general nature. Don’t you think that such feature promotes “test per method” style, instead of (IMHO better) “test behaviour” style?
—
Cheers,
Tomek Kaczanowski
#5 by Cedric on January 19, 2011 - 12:57 pm
Joao:
I just sent you an email to investigate the issues you are having.
Tomek:
This feature is definitely more for unit testing, but if you are writing a functional test, you can just ignore the method page, press “Finish” right away and you’ll still get a skeleton of a TestNG class.
#6 by Ricky Clarkson on January 19, 2011 - 10:21 pm
Incidentally, IDEA has had this at least for JUnit for some time. Ctrl-alt-A, type Test, press return and it generates a test class allowing you to select methods to generate empty test methods for. It places the class in the right directory, though it would be pleasant if it would create src/test/java if it didn’t exist instead of dumping the file into src/main/java.
#7 by Ricky Clarkson on January 19, 2011 - 10:22 pm
Ctrl-shift-A, sorry.
#8 by Wim Deblauwe on January 19, 2011 - 10:31 pm
@Ricky: You need to use CTRL-SHIFT-T to generate a test. The same shortcut allows you to jump between your class and the unit test for it, I use that all of the time.
#9 by Daniel Zimmerman on January 19, 2011 - 11:52 pm
Eclipse has also had it for JUnit for quite some time; but it’s nice to see it for TestNG as well.
#10 by Tony Bussières on January 30, 2011 - 6:45 am
Hi Cedric,
I also do think that behavioural testing is more interesting and expressive than using method level testing.
Without removing the need of proper documentation, it quickly provides a good impression on what the class should do.
Anyway, it’s great to see tools emerge to help speeding up unit testing with the IDE.