TestNG Running JUnit Tests

Source: Internet
Author: User
Tags testng

Now that you've learned about testng and its various tests, if you're worried about refactoring existing JUnit code, it's not necessary to use testng to provide a way to follow your own rhythm from JUnit and testng. You can also use TestNG to execute an existing JUnit test case.
TestNG can automatically identify and run JUnit tests, so you can use testng to run all the tests and write new tests using testng. All you have to do is put the JUnit library testng on the classpath, it can find and use the JUnit class, change the test run from JUnit and testng ant, and then run the testng "mixed" mode. This way you can test all the tests in the same project, even in the same package, and start using testng. This method can also convert your existing JUnit tests to testng.
Let's take a look at the following example and try out the above features:
To create a JUnit test case class
Create a Java class, which is a JUnit test class, Testjunit.java in C \ > Testng_workspace
Import Org.junit.Test;
Import static org.testng.AssertJUnit.assertEquals;


public class Testjunit {
@Test
public void Testadd () {
String str= "Junit testing using TestNG";
as Sertequals ("Junit Testing using TestNG", str);
}
}
Now, let's write testng.xml in C: \ > Testng_workspace should include the following <suite> tag as follows.
<?xml version= "1.0" Encodi Ng= "UTF-8"?
<! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd",
<suite name= "converted JUnit Suite";
< Test Name= "Junittests" junit= "true";
<classes>
<class name= "Testjunit"/>
</classes>
</test>
</suite>

to perform the JUnit test Case definition Property junit= "True" as in the XML file above. The JUnit test case class testjunit is defined in the class name. In
JUnit 4, TestNG will run the test using Org.junit.runner.JUnitCore.
All Java class compilations use Javac.
C:\testng_workspace>javac Testjunit.java
now runs Testng.xml, which runs the JUnit test case for TestNG.

C:\TESTNG_WORKSPACE>JAVA-CP "C:\testng_workspace:c:\testng_workspace\lib\junit-4.11.jar" Org.testng.TestNG Testng.xml
Here I have placed the Junit-4.11.jar under the C:\TestNG_WORKSPACE\lib\junit-4.11.jar.
Verify the output.
===============================================
Converted JUnit Suite

Total tests Run:1, failures:0, skips:0
===============================================

TestNG Running JUnit Tests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.