~ubuntu-branches/ubuntu/utopic/testng/utopic

« back to all changes in this revision

Viewing changes to doc/migrating.html

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-10-21 15:53:15 UTC
  • mfrom: (7.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20131021155315-u5rltd1grg53e3n6
Tags: 6.8.7-2
* Team upload
* Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
<h2 align="center">Migrating from JUnit</h2>
32
32
 
33
 
<h3>Using JUnitConverter</h3>
34
 
<p>You can easily convert your JUnit tests with <tt>org.testng.JUnitConverter,</tt>&nbsp; 
35
 
which you can invoke as follows:</p><ul>
36
 
        <li><tt>-annotation | -javadoc</tt> (required)<br>
37
 
        If <tt>-annotation</tt>, <tt>
38
 
        JUnitConverter</tt> will insert JDK5 annotations, and if you specify <tt>-javadoc</tt>, 
39
 
        the tool will insert JavaDoc annotations.<br>
40
 
&nbsp;</li><li><tt>-srcdir | -source &lt;fileName&gt;</tt> (required)<br>
41
 
        <tt>-srcdir</tt>, the directory 
42
 
        will be recursively traversed and any file that ends in .java will be 
43
 
        processed.&nbsp; If you specify <tt>-source</tt>, only that specific Java 
44
 
        file will be processed.<br>
45
 
&nbsp;</li><li><tt>-d | -overwrite</tt> (mandatory)<br>
46
 
        By default, <tt>JUnitConverter </tt>will modify the sources it finds.&nbsp; 
47
 
        If you specify a directory with this option, this is where the modified 
48
 
        files will be created.&nbsp; If you specify <tt>-overwrite</tt>, the source 
49
 
        files will be directly modified.<br>
50
 
&nbsp;</li><li><tt>-quiet</tt><br>
51
 
        <tt>JUnitConverter</tt> will not print anything on the console.</li></ul>
52
 
 
53
 
        <p>Here is a sample use that will convert all the JUnit tests in the <tt>src/</tt> 
54
 
directory to TestNG:</p>
55
 
 
56
 
<pre class="brush: text">
57
 
java org.testng.JUnitConverter -overwrite -annotation -srcdir src
58
 
</pre>
59
 
Notes:<blockquote>
60
 
        <ul>
61
 
                <li><i><tt>JUnitConverter</tt> uses classes from <tt>tools.jar</tt>, which is located in 
62
 
                <tt>$JAVA_HOME/lib/tools.jar</tt>, 
63
 
so make sure you have this jar file in your classpath.<br>
64
 
&nbsp;</i></li>
65
 
                <li><i><tt>JUnitConverter</tt> 
66
 
only runs with the JDK5, so if you receive an error (such as &quot;unknown -quiet 
67
 
parameter&quot;), make sure your classpath only contains JDK5 jar files.</i></li>
68
 
        </ul>
69
 
</blockquote><p><tt>JUnitConverter</tt> also has an ant task, which you can invoke as follows: </p>
70
 
 
71
 
<pre class="brush: xml">
72
 
&lt;project name="test" default="init"&gt;
73
 
  &lt;target name="init"&gt;
74
 
    &lt;taskdef resource="testngtasks" /&gt;
75
 
  &lt;/target&gt;
76
 
     
77
 
  &lt;target name="junitconvert" depends="init"&gt;
78
 
    &lt;junit-converter sourcedir="C:\dev\projects\test\java" outputdir="C:\dev\projects\temp" annotations="false" /&gt;
79
 
  &lt;/target&gt;
80
 
&lt;/project&gt;
81
 
</pre>
 
33
<h3>Using Eclipse</h3>
 
34
 
 
35
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>.
82
36
 
83
37
<h3>Asserts</h3>
84
38
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:
86
40
<pre class="brush: java">
87
41
import static org.testng.AssertJUnit.*;
88
42
</pre>
89
 
        
 
43
 
 
44
<h3>Running JUnit Tests</h3>
 
45
 
 
46
<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>
 
47
 
 
48
<p>All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes,
 
49
change your test runner from JUnit to TestNG in Ant and then run TestNG in <tt>"mixed"</tt> mode.
 
50
This way you can have all your tests in the same project, even in the same package, and start using TestNG.
 
51
This approach also allows you to convert your existing JUnit tests to TestNG incrementally.</p>
 
52
 
 
53
<h4>Example - replacing JUnit Ant task with TestNG one</h4>
 
54
 
 
55
JUnit version:
 
56
<pre class="brush: xml">
 
57
&lt;junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true"&gt;
 
58
    &lt;batchtest todir="${build.test.results.dir}"&gt;
 
59
        &lt;fileset dir="${test.src.dir}"&gt;
 
60
            &lt;include name="**/*Test.*"/&gt;
 
61
    &lt;/batchtest&gt;
 
62
    &lt;classpath&gt;
 
63
        &lt;path path="${run.test.classpath}"/&gt;
 
64
    &lt;/classpath&gt;
 
65
    &lt;syspropertyset&gt;
 
66
        &lt;propertyref prefix="test-sys-prop."/&gt;
 
67
        &lt;mapper from="test-sys-prop.*" to="*" type="glob"/&gt;
 
68
    &lt;/syspropertyset&gt;
 
69
    &lt;formatter type="xml"/&gt;
 
70
    &lt;jvmarg value="-ea"/&gt;
 
71
    &lt;jvmarg line="${run.jvmargs}"/&gt;
 
72
&lt;/junit&gt;
 
73
</pre>
 
74
 
 
75
TestNG version:
 
76
<pre class="brush: xml">
 
77
&lt;taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}"/&gt;
 
78
 
 
79
&lt;fileset id="mixed.tests" dir="${test.src.dir}"&gt;
 
80
    &lt;include name="**/*Test.*"/&gt;
 
81
&lt;/fileset&gt;
 
82
 
 
83
&lt;testng mode="mixed" classfilesetref="mixed.tests" workingDir="${work.dir}" failureProperty="tests.failed" outputdir="${build.test.results.dir}"&gt;
 
84
    &lt;classpath&gt;
 
85
        &lt;pathelement path="${build.test.classes.dir}"/&gt;
 
86
        &lt;pathelement path="${run.test.classpath}"/&gt;
 
87
        &lt;pathelement path="${junit.lib}"/&gt;
 
88
    &lt;/classpath&gt;
 
89
    &lt;propertyset&gt;
 
90
        &lt;propertyref prefix="test-sys-prop."/&gt;
 
91
        &lt;mapper from="test-sys-prop.*" to="*" type="glob"/&gt;
 
92
    &lt;/propertyset&gt;
 
93
    &lt;jvmarg line="${run.jvmargs}"/&gt;
 
94
&lt;/testng&gt;
 
95
</pre>
 
96
 
 
97
 
 
98
<h3>Related reading</h3>
 
99
 
 
100
<ul>
 
101
    <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>
 
102
    <li><a href="http://wiki.netbeans.org/TestNG_MixedMode">Mixed mode in TestNG</a>.</li>
 
103
</ul>
90
104
 
91
105
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
92
106
</script>