পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Tuesday, January 24, 2017

TestNG - Run JUnit Tests

Now that you have understood TestNG and its various tests, you must be worried by now as to how to refactor your existing JUnit code. There's no need to worry, as TestNG provides a way to shift from JUnit to TestNG at your own pace. You can execute your existing JUnit test cases using TestNG.

TestNG can automatically recognize and run JUnit tests, so that you can use TestNG as a runner for all your existing tests and write new tests using TestNG. 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 "mixed" 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.
Let us have an example to demonstrate this amazing ability of TestNG.

Create 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";
      assertEquals("Junit testing using TestNG",str);
   }
}
Now, let's write the testng.xml in C:\>TestNG_WORKSPACE, which would contain the <suite> tag as follows:
<?xml version="1.0" encoding="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 execute the JUnit test cases, define the property junit="true" as in the xml above. The JUnit test case class TestJunit is defined in class name.
For JUnit 4, TestNG will use the org.junit.runner.JUnitCore runner to run your tests.
Compile all java classes using javac.
C:\TestNG_WORKSPACE>javac TestJunit.java
Now, run testng.xml, which will run the JUnit test case as TestNG.
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE:C:\TestNG_WORKSPACE\lib\junit-4.11.jar" org.testng.TestNG testng.xml
Here, we have placed the junit-4.11.jar under C:\TestNG_WORKSPACE\lib\junit-4.11.jar.
Verify the output.
===============================================
   Converted JUnit suite

   Total tests run: 1, Failures: 0, Skips: 0
===============================================

No comments:

Post a Comment