| <html> |
| <head> |
| <title>TestNG - Migrating from JUnit</title> |
| |
| <link rel="stylesheet" href="testng.css" type="text/css" /> |
| <link type="text/css" rel="stylesheet" href="http://beust.com/beust.css" /> |
| <script type="text/javascript" src="http://beust.com/prettify.js"></script> |
| <script type="text/javascript" src="banner.js"></script> |
| |
| <script type="text/javascript" src="http://beust.com/scripts/shCore.js"></script> |
| <script type="text/javascript" src="http://beust.com/scripts/shBrushJava.js"></script> |
| <script type="text/javascript" src="http://beust.com/scripts/shBrushXml.js"></script> |
| <script type="text/javascript" src="http://beust.com/scripts/shBrushBash.js"></script> |
| <script type="text/javascript" src="http://beust.com/scripts/shBrushPlain.js"></script> |
| <link type="text/css" rel="stylesheet" href="http://beust.com/styles/shCore.css"/> |
| <link type="text/css" rel="stylesheet" href="http://beust.com/styles/shThemeCedric.css"/> |
| <script type="text/javascript"> |
| SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf'; |
| SyntaxHighlighter.defaults['gutter'] = false; |
| SyntaxHighlighter.all(); |
| </script> |
| |
| </head> |
| <body onLoad="prettyPrint()"> |
| |
| <script type="text/javascript"> |
| displayMenu("migrating.html") |
| </script> |
| |
| |
| <h2 align="center">Migrating from JUnit</h2> |
| |
| <h3>Using Eclipse</h3> |
| |
| The easiest way to convert your JUnit tests to TestNG is to use the Eclipse TestNG plug-in refactoring support. You will find a full description of its features in the <a href="eclipse.html#eclipse-quickfix">Eclipse section</a>. |
| |
| <h3>Asserts</h3> |
| Note that the class <tt>org.testng.Assert</tt> uses a different argument ordering than the ones used by JUnit. If you are porting code that uses JUnit's asserts, you might want to us a static import of that class: |
| |
| <pre class="brush: java"> |
| import static org.testng.AssertJUnit.*; |
| </pre> |
| |
| <h3>Running JUnit Tests</h3> |
| |
| <p>TestNG can automatically recognize and run JUnit tests, so you can use TestNG as a runner for all your existing tests and write new tests using TestNG.</p> |
| |
| <p>All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes, |
| change your test runner from JUnit to TestNG in Ant and then run TestNG in <tt>"mixed"</tt> mode. |
| This way you can have all your tests in the same project, even in the same package, and start using TestNG. |
| This approach also allows you to convert your existing JUnit tests to TestNG incrementally.</p> |
| |
| <h4>Example - replacing JUnit Ant task with TestNG one</h4> |
| |
| JUnit version: |
| <pre class="brush: xml"> |
| <junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true"> |
| <batchtest todir="${build.test.results.dir}"> |
| <fileset dir="${test.src.dir}"> |
| <include name="**/*Test.*"/> |
| </batchtest> |
| <classpath> |
| <path path="${run.test.classpath}"/> |
| </classpath> |
| <syspropertyset> |
| <propertyref prefix="test-sys-prop."/> |
| <mapper from="test-sys-prop.*" to="*" type="glob"/> |
| </syspropertyset> |
| <formatter type="xml"/> |
| <jvmarg value="-ea"/> |
| <jvmarg line="${run.jvmargs}"/> |
| </junit> |
| </pre> |
| |
| TestNG version: |
| <pre class="brush: xml"> |
| <taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}"/> |
| |
| <fileset id="mixed.tests" dir="${test.src.dir}"> |
| <include name="**/*Test.*"/> |
| </fileset> |
| |
| <testng mode="mixed" classfilesetref="mixed.tests" workingDir="${work.dir}" failureProperty="tests.failed" outputdir="${build.test.results.dir}"> |
| <classpath> |
| <pathelement path="${build.test.classes.dir}"/> |
| <pathelement path="${run.test.classpath}"/> |
| <pathelement path="${junit.lib}"/> |
| </classpath> |
| <propertyset> |
| <propertyref prefix="test-sys-prop."/> |
| <mapper from="test-sys-prop.*" to="*" type="glob"/> |
| </propertyset> |
| <jvmarg line="${run.jvmargs}"/> |
| </testng> |
| </pre> |
| |
| |
| <h3>Related reading</h3> |
| |
| <ul> |
| <li><a href="http://www.opengamma.com/blog/2011/04/04/converting-opengamma-junit-testng">Here is the detailed report of a company that successfully converted a large codebase of JUnit 4 tests over to TestNG</a>.</li> |
| <li><a href="http://wiki.netbeans.org/TestNG_MixedMode">Mixed mode in TestNG</a>.</li> |
| </ul> |
| |
| <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> |
| </script> |
| <script type="text/javascript"> |
| _uacct = "UA-238215-2"; |
| urchinTracker(); |
| </script> |
| |
| |
| </body> |
| |