13

I'm familiar with jUnit, and heard TestNG might be a solution to some of jUnit's annoyences - e.g. its insistence to create a separate instance of the test class per test, thus forcing me to use static field for objects I want to reuse between tests.

(Let's say you agree with me that this is a drawback, and not turn this question to something it's not)

What I'm asking here, is what drawbacks does TestNG have, compared to jUnit? Why not use TestNG, assuming this is a new project and there isn't any migration cost?

Andrejs
  • 10,803
  • 4
  • 43
  • 48
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • You are aware that reusing objects between tests increases the coupling between the tests? Coupled tests are the utter bane of good unit testing since they allow problems with one test to result in failures (or exceptions) in another… – Donal Fellows Nov 24 '10 at 10:38
  • @Donal - let's not make this question about this. When writing heavy integration tests with lots of components, not reusing is another bane, because it can make test runs take hours. – ripper234 Nov 24 '10 at 12:58
  • Agreed; this is a side issue. – Donal Fellows Nov 24 '10 at 14:33
  • 5
    Reusing objects between tests doesn't necessarily increase coupling between tests especially if these objects are immutable. The advantage of doing that is not having to create expensive objects over and over. TestNG gives you the choice of either approaches while JUnit forces you into one. – Cedric Beust Nov 28 '10 at 03:14

4 Answers4

7

I'm the creator of TestNG. I'm not going to weigh in since I'm obviously biased, but I'm happy to answer any question you might have about TestNG.

Andy: thanks for your comment. FYI (you probably already know that but maybe the original poster doesn't), there is a TestNG Eclipse plug-in (which I develop in parallel to TestNG).

Cedric Beust
  • 15,480
  • 2
  • 55
  • 55
  • Hello :) Nice to "meet" you, I love it that you can meet people on SO. I've been using TestNG for just about 10 minutes now ... Why did you choose not to have an @Ignore attribute? What I like about @Ignore is that you can specify the ignore reason inside the annotation, and AFAIK you can't do that with "enabled=false" – ripper234 Nov 24 '10 at 04:51
  • 3
    In TestNG, you mark a test as ignored with "@Test(enabled = false)". This makes discovery easier for beginners (just type "@Test(" then control-space and you get a list of all the available attributes). – Cedric Beust Nov 28 '10 at 03:11
5

I personally have not encountered any significant drawbacks compared to JUnit.

At the start of a new project, my team switched to TestNG and had no regrets. TestNG is more powerful and supports broader usage than unit tests.

Some tools support JUnit but not TestNG. These are tools that I have not yet needed. For example:

  • Google's CodePro Analytix supports generation of JUnit unit tests.
  • The Eclipse IDE for RCP development supports run/debug configurations for "JUnit plug-in tests."
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
3

Being a huge supporter of TestNG I still see it is treated as being number 2 by tools authors. Many tools and libraries support JUnit from the very beginning, but you have to wait till they also implement support for TestNG. This might be a serious drawback if you plan to use some latest buzzing technology.

Over the years situation improved a lot. For example this used to be an issue with Spring, Gradle, or Maven Surefire but is no longer a problem because they all support TestNG now. Also all IDEs are treating both frameworks equally.

So, make sure the other technologies you plan to use play nicely with TestNG. This is rarely a problem, but still better make sure of it ahead.

1

For me the biggest problem is an integration with spring. I do not like to extend TestNg classes and write code like this:

@Test
@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
public class TestSpring extends AbstractTestNGSpringContextTests {

Because usually I have my own hierarchy of test classes. This was the main reason why I stopped using TestNg.

I must agree that parameterized test was very attractive for me. But they can be easily replaced with junit data-provider.

https://github.com/TNG/junit-dataprovider/wiki/Getting-started

Yurii Bondarenko
  • 3,460
  • 6
  • 28
  • 47