<p>At work we are currently still using JUnit 3 to run our tests. We have been considering switching over to JUnit 4 for <strong>new</strong> tests being written but I have been keeping an eye on TestNG for a while now. What experiences have you all had with either JUnit 4 or TestNG, and which seems to work better for very large numbers of tests? Having flexibility in writing tests is also important to us since our functional tests cover a wide aspect and need to be written in a variety of ways to get results.</p>

<p>Old tests will not be re-written as they do their job just fine. What I would like to see in new tests though is flexibility in the way the test can be written, natural assertions, grouping, and easily distributed test executions.</p>

[{'answer_id': 6659, 'author': 'Justin Standard', 'author_id': 92, 'author_profile': 'https://Stackoverflow.com/users/92', 'pm_score': 4, 'selected': False, 'text': '<p>First I would say, don\'t rewrite all your tests just to suit the latest fad.  Junit3 works perfectly well, and the introduction of annotations in 4 doesn\'t buy you very much (in my opinion).  It is much more important that you guys <em>write</em> tests, and it sounds like you do.  </p>\n\n<p>Use whatever seems most natural and helps you get your work done.</p>\n\n<p>I can\'t comment on TestNG b/c I haven\'t used it.  But I would recommend <a href="http://www.unitils.org/summary.html" rel="noreferrer">unitils</a>, a great wrapper for JUnit/TestNG/DBUnit/EasyMock, regardless of which route you take. (It supports all the flavors mentioned above)</p>\n'}, {'answer_id': 6671, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 7, 'selected': True, 'text': '<p>I\'ve used both, but I have to agree with Justin Standard that you shouldn\'t really consider rewriting your existing tests to any new format.  Regardless of the decision, it is pretty trivial to run both.  TestNG strives to be much more configurable than JUnit, but in the end they both work equally well.</p>\n\n<p>TestNG has a neat feature where you can mark tests as a particular group, and then easily run all tests of a specific group, or exclude tests of a particular group.  Thus you can mark tests that run slowly as in the "slow" group and then ignore them when you want quick results.  A suggestion from their documentation is to mark some subset as "checkin" tests which should be run whenever you check new files in.  I never saw such a feature in JUnit, but then again, if you don\'t have it, you don\'t REALLY miss it.</p>\n\n<p>For all its claims of high configuration, I did run into a corner case the a couple weeks ago where I couldn\'t do what I wanted to do... I wish I could remember what it is, but I wanted to bring it up so you know that it\'s not perfect.</p>\n\n<p>The biggest advantage TestNG has is annotations... which JUnit added in version 4 anyways.</p>\n'}, {'answer_id': 6713, 'author': 'abyx', 'author_id': 573, 'author_profile': 'https://Stackoverflow.com/users/573', 'pm_score': 4, 'selected': False, 'text': "<p>About a year ago, we had the same problem. I spent sometime considering which move was better, and eventually we realized that TestNG has no 'killer features'. It's nice, and has some features JUnit 4 doesn't have, but we don't need them.<br>\nWe didn't want people to feel uncomfortable writing tests while getting to know TestNG because we wanted them to keep writing a lot of tests.<br>\nAlso, JUnit is pretty much the de-facto standard in the Java world. There's no decent tool that doesn't support it from the box, you can find a lot of help on the web and they added a lot of new features in the past year which shows it's alive.</p>\n\n<p>We decided to stick with JUnit and never looked back.</p>\n"}, {'answer_id': 14412, 'author': 'Mark Derricutt', 'author_id': 1720, 'author_profile': 'https://Stackoverflow.com/users/1720', 'pm_score': 4, 'selected': False, 'text': "<p>TestNG's biggest draw cards for me include its support test groups, and more importantly - test group dependencies (marking a test as being dependent of a group causes the tests to simply skip running when the dependent group fails).</p>\n\n<p>TestNG's other big draw cards for me include test parameters, data providers, annotation transformers, and more than anything - the vibrant and responsive user community.</p>\n\n<p>Whilst on the surface one might not think all of TestNGs features above might not be needed, once you start to understand the flexibility bring to your tests, you'll wonder how you coped with JUnit.</p>\n\n<p>(disclaimer - I've not used JUnit 4.x at all, so am unable to really comment on advances or new features there).</p>\n"}, {'answer_id': 46727, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>A couple of additions to Mike Stone\'s reply:</p>\n\n<p>1) The most frequent thing I use TestNG\'s groups for is when I want to run a single test method in a test suite.  I simply add this test to the group "phil" and then run this group.  When I was using JUnit 3, I would comment out the entries for all methods but the one I wanted to run in the "suite" method, but then would commonly forget to uncomment them before checkin.  With the groups, I no longer have this problem.</p>\n\n<p>2) Depending on the complexity of the tests, migrating tests from JUnit3 to TestNG can be done somewhat automatically with sed and creating a base class to replace TestCase that static imports all of the TestNG assert methods.</p>\n\n<p>I have info on my migration from JUnit to TestNG <a href="http://www.philvarner.com/blog/2008/01/30/testng-migration/" rel="noreferrer">here</a> and <a href="http://www.philvarner.com/blog/2008/02/10/testng-part-2/" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 430893, 'author': 'Denis Bazhenov', 'author_id': 53658, 'author_profile': 'https://Stackoverflow.com/users/53658', 'pm_score': 3, 'selected': False, 'text': "<p>Also one more advantage of TestNG is supporting of parallel testing. In our era of multicores it's important, i think.</p>\n\n<p>I also used both frameworks. But i using hamcrest for assertations. Hamcrest allows you easily write your own assert method. So instead of</p>\n\n<pre><code>assertEquals(operation.getStatus(), Operation.Status.Active);\n</code></pre>\n\n<p>You can write</p>\n\n<pre><code>assertThat(operation, isActive());\n</code></pre>\n\n<p>That gives you opportunity to use higher level of abstraction in your tests. And this makes your tests more robust. </p>\n"}, {'answer_id': 1232919, 'author': 'ravinikam', 'author_id': 136106, 'author_profile': 'https://Stackoverflow.com/users/136106', 'pm_score': 4, 'selected': False, 'text': '<p>I wanted to share the one I encountered today. I found built-in Parameterized runner is quite crude in Junit4 as compare to TestNG (I know each framework has its strengths but still). The Junit4 annotation @parameters is restricted to one set of parameters. I encountered this problem while testing the valid and invalid behavior for functionality in same test class. So the first public, static annotated method that it finds will be used, but it may find them in any order. This causes us to write different classes unnecessarily. However TestNG provides clean way to provide different kind of data providers for each and every method. So we can test the same unit of code with valid and invalid way in same test class putting the valid/invalid data separately. I will go with TestNG.</p>\n'}, {'answer_id': 3522444, 'author': 'mezmo', 'author_id': 310092, 'author_profile': 'https://Stackoverflow.com/users/310092', 'pm_score': 4, 'selected': False, 'text': "<p>Cheers to all the above. Some other things I've personally found I like more in TestNG are:</p>\n\n<ol>\n<li><p>The <code>@BeforeClass</code> for TestNG takes place after class creation, so you aren't constrained by only being able to call static methods of your class in it.</p></li>\n<li><p>Parallel and parameterized tests, maybe I just don't have enough of a life... but I just get a kick writing one set of Selenium tests, accepting a driver name as a parameter. Then defining 3 parallel test groups, 1 each for the IE, FF and Chrome drivers, and watching the race! I originally did 4, but way too many of the pages I've worked on break the <code>HtmlUnit</code> driver for one reason or another.</p></li>\n</ol>\n\n<p>Yeah, probably need to find that life. ;)</p>\n"}, {'answer_id': 17062476, 'author': 'MostWanted', 'author_id': 495307, 'author_profile': 'https://Stackoverflow.com/users/495307', 'pm_score': 2, 'selected': False, 'text': '<p>Your question seems two folded to me. On one had you would like to compare two test frameworks, on the other hand you would like to implement tests easily, have natural assertions, etc...</p>\n\n<p>Ok, firstly JUnit has been playing catchup with TestNG in terms of functionality, they have bridged the gap some what with v4, but not well enough in my opinion. Things like annotations and dataproviders are still much better in TestNG. Also they are more flexible in terms of test execution, since TestNG has test dependency, grouping and ordering.</p>\n\n<p>JUnit still requires certain before/after methods to be static, which limits what you can do prior to the running of tests, TestNG never has this issue.</p>\n\n<p>TBH, mostly the differences between the two frameworks don\'t mean much, unless your focusing on integration/automation testing. JUnit from my experience is built from the ground up for unit testing and is now being pushed towards higher levels of testing, which IMO makes it the wrong tool for the job. TestNG does well at unit testing and due to its robust dataproviding and great test execution abilities, works even better at integration/automation test level.</p>\n\n<p>Now for what I believe is a separate issue, how to write well structured, readable and maintainable tests. Most of this I am sure you know, but things like <strong>Factory Pattern</strong>, <strong>Command Pattern</strong> and <strong>PageObjects</strong> (if your testing websites) are vital, it is very important to have a layer of abstraction between what your testing (SUT) and what the actual test is (assertions of business logic). In order to have much nicer assertions, you can use <a href="https://code.google.com/p/hamcrest/" rel="nofollow" title="hamcrest">Hamcrest</a>. Make use of javas inheritance/interfaces to reduce repetition and enforce commonality.</p>\n\n<p>Almost forgot, also use the <strong>Test Data Builder Pattern</strong>, this coupled with TestNG\'s dataprovider annotation is very useful.</p>\n'}, {'answer_id': 22897063, 'author': 'djangofan', 'author_id': 118228, 'author_profile': 'https://Stackoverflow.com/users/118228', 'pm_score': 2, 'selected': False, 'text': "<p>My opinion about what makes TestNG truly far more powerful:</p>\n\n<pre><code>1.  JUnit still requires the before/after class methods to be static, which limits\n    what you can do prior to the running of tests, TestNG never has this issue.\n\n2.  TestNG @Configuration methods can all take an optional argument to their \n    annotated methods in the form of a ITestResult, XmlTest, Method, or \n    ITestContext.  This allows you to pass things around that JUnit wouldn't \n    provide you.  JUnit only does this in listeners and it is limited in use.\n\n3.  TestNG comes with some pre-made report generation classes that you can copy\n     and edit and make into your own beautiful test output with very little \n     effort. Just copy the report class into your project and add a listener \n     to run it.  Also, ReportNG is available.\n\n4.  TestNG has a handful of nice listeners that you can hook onto so you can do\n     additional AOP style magic at certain phases during testing.\n</code></pre>\n"}, {'answer_id': 38111186, 'author': 'Sundararaj Govindasamy', 'author_id': 1401019, 'author_profile': 'https://Stackoverflow.com/users/1401019', 'pm_score': 3, 'selected': False, 'text': '<p><strong>JUnit 4 Vs TestNG – Comparison by mkyong.com ( updated on 2013).</strong></p>\n\n<p>Conclusion: I suggest to use TestNG as core unit test framework for Java project, because TestNG is <strong>more</strong> advance in parameterize testing, dependency testing and suite testing (Grouping concept). </p>\n\n<p>TestNG is meant for functional, high-level testing and complex integration test. Its flexibility is especially useful with large test suites. </p>\n\n<p>In addition, <strong>TestNG also cover the entire core JUnit4 functionality</strong>. It’s just no reason for me to use JUnit anymore.</p>\n\n<blockquote>\n  <p>In simple terms, <strong>TestNG = JUnit + lot more.</strong> So, Why debate ? go and\n  grab TestNG :-)</p>\n</blockquote>\n\n<p>You can find more detailed comparison <a href="http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 47765625, 'author': 'SkyWalker', 'author_id': 2293534, 'author_profile': 'https://Stackoverflow.com/users/2293534', 'pm_score': 3, 'selected': False, 'text': '<h2><strong>Why we use TestNG instead of JUnit?</strong></h2>\n\n<ol>\n<li><p>The declaration of <code>@BeforeClass</code> and <code>@AfterClass</code> method has to be static in JUnit whereas, there is more flexibility in TestNG in the method declaration, it does not have these constraints.</p></li>\n<li><p><strong>In TestNG, we can parametrize tests using 2 ways</strong>. @Parameter or @DataProvider annotation.</p>\n\n<p>i) <strong>@Parameter</strong> for simple cases, where key value mapping is required.(data is provided through xml file)</p>\n\n<p>ii) <strong>@DataProvider</strong> for complex cases. Using 2 dimensional array, It can provide data.</p></li>\n<li><p>In TestNG, since @DataProvider method need not be static, we can use multiple data provider methods in the same test class.</p></li>\n<li><p><strong>Dependency Testing:</strong> In TestNG, if the initial test fails, then all subsequent dependent tests will be skipped, not marked as failed. But JUnit marked it failed.</p></li>\n<li><p><strong>Grouping:</strong> Single tests can belong to multiple groups and then run in different contexts (like slow or fast tests). A similar feature exists in JUnit Categories but lacks the @BeforeGroups / @AfterGroups TestNG annotations that allow initializing the test / tearing it down.</p></li>\n<li><p><strong>Parallelism:</strong> If you’d like to run the same test in parallel on multiple threads, TestNG has you covered with a simple to use annotation while JUnit doesn’t offer a simple way to do so out of the box.</p></li>\n<li><p><strong>TestNG @DataProvider can also support XML for feeding in data, CSVs, or even plain text files.</strong></p></li>\n<li><p>TestNG allows you to declare dependencies between tests, and skip them if the dependency test didn’t pass. </p></li>\n</ol>\n\n<blockquote>\n  <p>@Test(dependsOnMethods = { "dependOnSomething" })</p>\n</blockquote>\n\n<p>This functionality doesn’t exist in JUnit</p>\n\n<ol start="9">\n<li><strong>Reporting:</strong> </li>\n</ol>\n\n<p>TestNG reports are generated by default to a test-output folder that includes HTML reports with all of the test data, passed/failed/skipped, how long did they run, which input was used and the complete test logs. In addition, it also exports everything to an XML file which can be used to construct your own report template.</p>\n\n<p>On the JUnit front, all of this data is also available via XML, but there’s no out of the box report and you need to rely on plugins.</p>\n\n<h2>Resource Link:</h2>\n\n<ol>\n<li><a href="http://www.baeldung.com/junit-vs-testng" rel="noreferrer">A Quick JUnit vs TestNG Comparison</a></li>\n<li><a href="https://dzone.com/articles/junit-vs-testng-which-testing-framework-should-you" rel="noreferrer">JUnit vs. TestNG: Which Testing Framework Should You Choose?</a></li>\n</ol>\n\n<p>A good difference is given in this tutorial side by side: <a href="https://www.guru99.com/junit-vs-testng.html" rel="noreferrer">TestNG Vs JUnit: What\'s the Difference?</a></p>\n'}]
['https://Stackoverflow.com/questions/6658', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/782/']