ArticleslgStudy

computer science

TestNG

TestNG is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand TestNG rather than just read about it. In short: TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities.

Key takeaways

  • TestNG belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect TestNG to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of TestNG from memory before moving on to harder problems.

Reference excerpt

TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities.

Features TestNG's main features include:

Annotation support. Support for data-driven/parameterized testing (with @DataProvider and/or XML configuration). Support for multiple instances of the same test class (with @Factory) Flexible execution model. TestNG can be run either by Ant via build.xml (with or without a test suite defined), or by an IDE plugin with visual results. There isn't a TestSuite class, while test suites, groups and tests selected to run are defined and configured by XML files. Concurrent testing: run tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc.), and test whether the code is multithread safe. Embeds BeanShell for further flexibility. Default JDK functions for runtime and logging (no dependencies). Dependent methods for application server testing. Distributed testing: allows distribution of tests on slave machines.

Data provider A data provider in TestNG is a method in a test class, which provides an array of varied actual values to dependent test methods. Example:

The returned type of a data provider can be one of the following two types:

An array of array of objects (Object[][]) where the first dimension's size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method. An Iterator<Object[]>. The only difference with Object[][] is that an Iterator lets you create your test data lazily. TestNG will invoke the iterator and then the test method with the parameters returned by this iterator one by one. This is particularly useful if you have a lot of parameter sets to pass to the method and you don't want to create all of them upfront.

Tool support TestNG is supported, out-of-the-box or via plug-ins, by each of the three major Java IDEs - Eclipse, IntelliJ IDEA, and NetBeans. It also comes with a custom task for Apache Ant and is supported by the Maven build system. The Hudson continuous integration server has built-in support for TestNG and is able to track and chart test results over time. Most Java code coverage tools, such as Cobertura, work seamlessly with TestNG. Note: TestNG support for Eclipse is only embedded in the Eclipse Marketplace for Eclipse versions up to 2018-09 (4.9). For later versions of Eclipse, TestNG must be manually installed as per instructions in the TestNG site.

Reporting TestNG generates test reports in HTML and XML formats. The XML output can be transformed by the Ant JUnitReport task to generate reports similar to those obtained when using JUnit. Since version 4.6, TestNG also provides a reporter API that permits third-party report generators, such as ReportNG, PDFngreport and TestNG-XSLT, to be used.

Comparison with JUnit TestNG has a longstanding rivalry with the JUnit test automation framework. Each of them has differences and respective advantages. Stack Overflow discussions reflect this controversy.

Annotations In JUnit 5, the @BeforeAll and @AfterAll methods have to be declared as static in most circumstances. TestNG does not have this constraint. TestNG includes four additional setup/teardown annotation pairs for the test suite and groups: @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeGroup and @AfterGroup, @BeforeMethod and @AfterMethod. TestNG also provides support to automate testing an application using selenium.

Parameterized testing Parameterized testing is implemented in both tools, but in quite different ways. TestNG has two ways for providing varying parameter values to a test method: by setting the testng.xml, and by defining a @DataProvider method. In JUnit 5, the @ParameterizedTest annotation allows parameterized testing. This annotation is combined with another annotation declaring the source of parameterized arguments, such as @ValueSource or @EnumSource. Using @ArgumentsSource allows the user to implement a more dynamic ArgumentsProvider. In JUnit 4, @RunWith and @Parameters are used to facilitate parameterized tests, where the @Parameters method has to return a List[] with the parameterized values, which will be fed into the test class constructor.

Conclusion Different users often prefer certain features of one framework or another. JUnit is more widely popular and often shipped with mainstream IDEs by default. TestNG is noted for extra configuration options and capability for different kinds of testing. Which one more suitable depends on the use context and requirements.

See also

List of unit testing frameworks JUnit xUnit

References

External links Official website testng on GitHub

Worked examples

Example 1 — a first encounter with TestNG

Start with the simplest possible case. Write down what TestNG claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to TestNG before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about TestNG ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of TestNG

In research
TestNG appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses TestNG in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
TestNG is common in secondary-school and first-year university syllabi. It links to neighbouring topics Java (software platform), Software using the Apache license, Unit testing frameworks, so understanding it makes those chapters shorter.
In everyday life
Look for TestNG outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “TestNG” →

How to study TestNG in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what TestNG means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain TestNG out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is TestNG in simple terms?

TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities.

Why does TestNG matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study TestNG?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on TestNG.

Tags

  • Java (software platform)
  • Software using the Apache license
  • Unit testing frameworks

Keep exploring