JUnit 5 What happens to test methods with the same #Order(#)? - junit5

I am using JUnit 5 to test my RESTful application. I have a test class in which test methods are annotated with #Order(#). Any idea what happen when the same order number is used to some test methods? Would they be run in parallel?

They won't be executed in parallel. The ordering ensures to sort the test execution order adjacent to each other depending on their value.
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class OrderingTest {
#Test
void test0() {
assertEquals(2, 1 + 1);
System.out.println("Test0");
}
#Test
#Order(1)
void test1() {
assertEquals(2, 1 + 1);
System.out.println("Test1");
}
#Test
#Order(1)
void test2() {
assertEquals(2, 1 + 1);
System.out.println("Test2");
}
}
In this example, test1 and test2 have the same order number and the test execution will be: test1 -> test2 -> test0.
Internally JUnit 5 does the following for ordering:
#Override
public void orderMethods(MethodOrdererContext context) {
context.getMethodDescriptors().sort(comparingInt(OrderAnnotation::getOrder));
}
while .sort() is from java.util.List.
If you want parallel test execution, you can configure this with JUnit 5 in a different way.

Related

Is there a way to set priorities among classes in TestNG?

Is there a way to set priorities among classes execution in TestNG?
Is there some annotation or xml settings?
Thanks.
It can be achieved in testng.xml file. Order of classes is important in the file, and just run directly this file.
#Test(priority = 1)
public void testMethodA() {
System.out.println("Executing - testMethodA");
}
#Test
public void testMethodB() {
System.out.println("Executing - testMethodB");
}
#Test(priority = 2)
public void testMethodC() {
System.out.println("Executing - testMethodC");
}
Output
Executing - testMethodB
Executing - testMethodA
Executing-testMethodC
testMethodB got executed first as it had a default priority of 0

testng - what is the maximum value for priority for #Test annotation

I have ordered the automated test in a particular sequence by using the priority=xxx in #Test annotation.
For the last class to be tested, the priority values started with 10201 and above. However, this particular class was tested right after the 1st class with priorities from 1-10.
Does any one have any idea? I looked at the TestNG documentaion - but the values are not discussed.
I looked into TestNG source code and looks like priority is an int, so the max value will be 2147483647.
In fact, you can test it easily by running following tests:
import org.testng.annotations.Test;
public class Testing {
#Test(priority = 2147483647)
public void testOne() {
System.out.println("Test One");
}
#Test(priority = 1)
public void testTwo() {
System.out.println("Test Two");
}
}

TestNG: How to Ignore the method in dependsOnMethods if it was skipped and continue execution

I have a TestNG class with the following methods.
#Test(priority=4)
public void Test1()
{
}
#Test(priority=5, dependsOnMethods={"Test1"})
public void Test2()
{
throw new SkipException("SKIP");
}
#Test(priority=6, dependsOnMethods={"Test1"})
public void Test3()
{
}
#Test(priority=7, dependsOnMethods={"Test1"})
public void Test4()
{
}
#Test(priority=8, dependsOnMethods={"Test2","Test3","Test4"})
public void Test5()
{
}
What I would like to achieve is:
Test2, Test3 and Test 4 depends on Test1. So Only if Test1 pass then I need to continue.
And Test5 depends on Test2, Test3 and Test4.
But I can skip any test (i.e Test2, Test3 or Test4) and still want to continue my execution of Test5 if other Test's did not fail.
How can I achieve this.
You can use alwaysRun=true
#Test(alwaysRun = true, priority=8, dependsOnMethods={"Test2","Test3","Test4"})
public void Test5()
{
}
Test5 will be executed if any of the dependent test is skipped and also it will be executed if any one of dependent test fails.

How to print the number of failures for every test case using SoftAssert in TestNG

I have a test case that performs the following:
#Test
public void testNumber() {
SoftAssert softAssert = new SoftAssert();
List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
for (Integer num : nums){
softAssert.assertTrue(num%2==0, String.format("\n Old num : %d", num);
}
softAssert.assertAll();
}
The above test will fail for numbers 1,3,5,7,9
Five statements will be printed in the test report.
If I run the test for a larger data set, I find it difficult to get the count of test data that failed the test case.
Is there any easier way to get the number of test data that failed for a test case using softAssert itself ?
Easy way to find count of test data that failed the test case in SoftAssert itself is not possible.
Alternatively you can think of extending Assertion class. Below is the sample that gives count of test data that failed the test case and prints each failure in new line.
package yourpackage;
import java.util.Map;
import org.testng.asserts.Assertion;
import org.testng.asserts.IAssert;
import org.testng.collections.Maps;
public class AssertionExtn extends Assertion {
private final Map<AssertionError, IAssert> m_errors;
public AssertionExtn(){
this.m_errors = Maps.newLinkedHashMap();
}
protected void doAssert(IAssert a) {
onBeforeAssert(a);
try {
a.doAssert();
onAssertSuccess(a);
} catch (AssertionError ex) {
onAssertFailure(a, ex);
this.m_errors.put(ex, a);
} finally {
onAfterAssert(a);
}
}
public void assertAll() {
if (!(this.m_errors.isEmpty())) {
StringBuilder sb = new StringBuilder("Total assertion failed: "+m_errors.size());
sb.append("\n\t");
sb.append("The following asserts failed:");
boolean first = true;
for (Map.Entry ae : this.m_errors.entrySet()) {
if (first)
first = false;
else {
sb.append(",");
}
sb.append("\n\t");
sb.append(((AssertionError)ae.getKey()).getMessage());
}
throw new AssertionError(sb.toString());
}
}
}
You can use this as:
#Test
public void test()
{
AssertionExtn asert=new AssertionExtn();
asert.assertEquals(false, true,"failed");
asert.assertEquals(false, false,"passed");
asert.assertEquals(0, 1,"brokedown");
asert.assertAll();
}

Grails integration testsuite suite

We have a set of integration test which depend upon same set of static data. Since the amount of data is huge we dont want to set it up per test level. Is it possible to setup data at the start, run group of test and rollback the data at the end of test.
What we effectively want is the rollback at test suite level rather than test case level. We are using grails 1.3.1, any pointers would be highly helpful for us to proceed. Thanks in advance.
-Prakash
for one test case you could use:
#BeforeClass
public static void setUpBeforeClass() throws Exception {
}
#AfterClass
public static void tearDownAfterClass() throws Exception {
}
haven't tried a suite of test cases (yet).
i did have some trouble using findByName in the static methods and had to resort to saving an id and using get.
i did try rolling up a suite, but no joy, getting a: no runnable methods.
You can take control of the transaction/rollback behaviour by marking your test case as non-transactional and managing data, transactions and rollbacks yourself. Example:
class SomeTests extends GrailsUnitTestCase {
static transactional = false
static boolean testDataGenerated = false
protected void setUp() {
if (!testDataGenerated) {
generateTestData()
testDataGenerated = true
}
}
void testSomething() {
...test...
}
void testSomethingTransactionally() {
DomainObject.withTransaction {
...test...
}
}
void testSomethingTransactionallyWithRollback() {
DomainObject.withTransaction { status ->
...test...
status.setRollbackOnly()
}
}
}