Questions tagged [testng]

TestNG is a testing framework focused on providing both unit and functional testing abilities in the Java programming language. It supports parallel testing, data providers, dependencies, groups and other features.

TestNG is a testing framework inspired from JUnit and NUnit, but introducing some new functionalities that make it more powerful and easier to use, such as:

  • Annotation support.
  • Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc).
  • Test that your code is multi-thread safe.
  • Flexible test configuration.
  • Support for data-driven testing (with @DataProvider).
  • Support for parameters.
  • Powerful execution model (no more TestSuite).
  • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc).
  • Embeds BeanShell for further flexibility.
  • Default JDK functions for run-time and logging (no dependencies).
  • Dependent methods for application server testing.
  • Detailed reports of executed tests.
  • Distributed testing: allows distribution of tests on slave machines.

TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc.

References:

8489 questions
26
votes
5 answers

Possible to pass parameters to TestNG DataProvider?

We would like to run some of our tests each against a set of data values, verifying that the same conditions hold true for each. The data is currently stored in either flat files or in simple Excel spreadsheets. My first thought was to create a…
Benjamin Lee
  • 1,160
  • 3
  • 13
  • 18
26
votes
6 answers

Tests pass when run individually but not when the whole test class run

I have solved a topCoder problem for which all the tests pass when I run them on their own. Nonetheless when I run the whole test class some of them fail. Could you, please, help me identify the reason for this behaviour? Here is my class and my…
Orestis
  • 526
  • 1
  • 5
  • 18
25
votes
2 answers

JUnit 5 vs TestNG

We were using JUnit 4, but we had some problems with test grouping. For integration tests we had problems with running order. We had migrated to TestNG in February 2016. Now we are starting a new independent module for our project and part of our…
Sergii
  • 7,044
  • 14
  • 58
  • 116
25
votes
5 answers

How to test a Validator which implements ConstraintValidator in java?

I have an "AllowedValuesValidator.java" class: public class AllowedValuesValidator implements ConstraintValidator { String[] values; String defaultValue; @Override public void initialize(AllowedValues…
goe
  • 1,153
  • 2
  • 12
  • 24
24
votes
7 answers

Custom test method name in TestNG reports

I am working on a project where I need to invoke TestNG programatically(using data providers). Things are fine except that in the report, we are getting the name of the @Test method, which is a generic one to handle many cases. What we would like is…
rajesh
  • 3,247
  • 5
  • 31
  • 56
23
votes
4 answers

How to get logging working in scala unit tests with testng, slf4s, and logback

I'm new to Scala, and not that familiar with recent developments in Java, so I am having what I assume is a basic problem. I'm writing some Scala code, and testing it with test fixtures using ScalaTest and TestNG. The code under test uses slf4s to…
anelson
  • 2,569
  • 1
  • 19
  • 30
22
votes
5 answers

Retrieve test name on TestNG

Can I retrieve currently running test name like in JUnit (using getName() or rules)? @Test public void fooBar(){ System.out.println(magic()); //should print "fooBar" } P.S. I don't want use some self-written tool based on stack traces.
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
22
votes
5 answers

Spring-Autowiring happens after @BeforeClass when running test with maven-surefire

I have some problems with dependency injection (Spring autowiring) and maven-surefire. The following test works without problems when run in eclipse with TestNG: The service-object is injected, then the @BeforeClass-method is…
thofoer
  • 221
  • 1
  • 2
  • 3
21
votes
3 answers

Is there a way to get method meta data when using @BeforeMethod in TestNG?

I am using TestNG and have a suite of tests. I want to perform an action before every test method that requires information about the method. As a simple example, say I want to print the name of the method before its executed. I can write a method…
Usman Ismail
  • 17,999
  • 14
  • 83
  • 165
21
votes
1 answer

Is it safe to use DateTimeUtils.setCurrentMillisFixed in tests?

In order to test time-dependent code it's good to use Virtual Clock Pattern The idea is that we pull current time not using new Date, but from a clock which can be mocked with virtual clock returning predefined fixed time. Now in Java we have…
Vladimir
  • 4,782
  • 7
  • 35
  • 56
21
votes
7 answers

Intellij not shows test results in console

Howto set the output in test console? When i run test by default the output is replaced by the summary of the test, and System.outs are lost.
user1085746
  • 293
  • 1
  • 2
  • 8
20
votes
2 answers

How does TestNG invoke a test method using multiple threads?

In the TestNG documentation, there is a section describing how to tell TestNG to invoke test methods using multiple threads: You can also specify that a @Test method should be invoked from different threads. You can use the attribute…
Feanor
  • 2,715
  • 4
  • 29
  • 43
20
votes
8 answers

NoClassDefFoundError: org/testng/TestNG

This error NoClassDefFoundError: org/testng/TestNG appears when I'm trying to run my test from Testng.xml file using IntelliJ IDEA. Running my test one by one works perfectly as well as running whole gradle project which points to testng.xml, like…
Anton
  • 1,344
  • 3
  • 15
  • 32
20
votes
14 answers

Eclipse -- Testng references non-existing project XXX, launch configuration

When trying to run a test as a TestNG suite, I would get a very cryptic error message saying that TestNG can't launch because it references a non-existant project. I think my project does exist, I see it in the package explorer!
J-Dizzle
  • 3,176
  • 6
  • 31
  • 49
20
votes
4 answers

Detect test failure in testng @AfterMethod

I want to take a screenshot if a test fails. Rather than wrapping all test methods with try/catch blocks, I would like to add this logic to the method annotated with @AfterMethod. How can I detect in the method annotated with @AfterMethod if the…
Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102