testng Flashcards

1
Q

TestNG

A

an easy-to-use testing framework written in Java, designed for unit, functional, and end-to-end integration tests providing many functionalities. Many plugins are integrated with TestNG.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Writing tests with TestNG is very simple

A

Write the business logic inside a method. Then annotate the method with @Test.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

testng.xml

A

a file that contains the test configuration, and it allows us to organize test classes and define test suites and tests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

testng essential dependencies

A

import org.testng.Assert;

import org.testng.annotations.Test;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Java - XML Connection in TESTNG

A
package com.example.tests;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestClass {
    @Test // is an annotation to indicate this is a test method
    public void testA() {
        Assert.assertTrue(true); 
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Testng documentation:

A

https://testng.org/doc/documentation-main.html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

TestNG supports annotations:

A

@Test annotated method is considered as a test method …at class level as well. When given at test method level, the one at method level will take precedence. A test method can be disabled by setting @Test(enabled = false).
@BeforeSuite annotated method will run once per test suite before all the tests.
@AfterSuite annotated method will run once per test suite after all the tests.
@BeforeTest annotated method will run once per test before all the test methods.
@AfterTest annotated method will run once per test after all the test methods.
@BeforeClass annotated method will run once per every test class instance before all the test methods.
@AfterClass annotated method will run once per every test class instance after all the test methods.
@BeforeMethod annotated method will run once per every test method instance before all the test methods.
@AfterMethod annotated method will run once per every test method instance after all the test methods.
@BeforeGroup will run once per every test method instance before all the test methods that belong to the given group.
@AfterGroup will run once per every test method instance after all the test methods that belong to the given group.
@Parameter annotation on test method is to pass parameters to test methods.
@DataProvider to create test methods or test classes at runtime with different parameters. TestNG will create multiple test methods at runtime. It can also be added to the test class constructor in conjunction with @Factory to pass parameters to create test class instances at runtime.
@Factory used on a method that returns instances of test classes or on a test class constructor with @DataProvider.
@Listeners is used at test class level to takes the array of classes that implements a plethora of implementations of ITestNGListener like IAlterSuiteListener, IAnnotationTransformer, IMethodInterceptor, IReporter, etc. for different purposes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

order for the execution of annotated methods:

A
@BeforeSuite
    @BeforeTest
    @BeforeClass
    @BeforeMethod
    @Test
    @AfterMethod
    @AfterClass
    @AfterTest
    @AfterSuite
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to control parallelism?

A

Parallelism and thread count can be set at suite level or test level like below.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Types of parallelism

A

methods: run test methods in parallel in different threads. All dependent methods will be run in different threads, respecting the priority of tests.
tests: run tags in parallel in separate threads.
classes: run test classes in parallel in separate threads, but test methods in those test classes will run in the same thread.
instances: run instances of test methods/classes in parallel in different threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the grouping of tests?

A

TestNG allows to group related tests using tags. TestNG allows having configuration methods that can run before starting or ending of tests belonging to a group.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to group tests?

A

Each test can belong to one or more groups. This grouping can be done to test methods or test classes. When giving at both places, the one given at the test method takes precedence. This becomes Partial test grouping, as all the test methods in the class belong to a group, and this method belongs to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Parameterisation

A

Tests can be parameterized using @Parameter. They can be passed from testng.xml. Parameters can be set at suite level or test level. The same parameter can be overridden at the test level as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

To use parameter in the test method

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

To use parameter in the test method

A

@Parameters(“browser”)
@Test
public void testMethod(String browser) {}

@Parameters(“browser”)
@BeforeMethod
public void initMethod(String browser) {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

@DataProvider

A

for running a test case multiple times by passing different sets of parameters.

17
Q

Creating @DataProvider method

A

@DataProvider( name = “createData” )
public Object[][] createTestData() {

Object[][] data = new Object[2][2];
data[0] = new Object[] { "Sam", 21 };
data[1] = new Object[] { "Smith", 22 };

return data; }
18
Q

If the @DataProvider is not present in the same class than the test method:

A
public class DataProviderFactory {
    @DataProvider( name = "createData" )
    public static Iterator createTestData() {
        return list.iterator();
    }
}
public class TestClass {
    @Test(dataProvider = "createData", dataProviderClass = DataProviderFactory.class)
    public void testMethod(String name, int age) {        
    }
}
19
Q

to ensure the order and type of the parameters list that is given as an argument to the test method.

A
//If the test method and data provider are in the same class.
@Test(dataProvider = "createData") 
public void testMethod(String name, int age) {}
//If the test method and data provider are in different classes
@Test(dataProvider = "createData", dataProviderClass = MyDataProviderClass.class) 
public void testMethod(String name, int age) {}
20
Q

@Factory

A

creating test classes at runtime.

21
Q

a @Factory method that creates two instances of the test class

A
public class TestClass {
    private String browser;
    public TestClass(String browser) {
        this.browser = browser;
    }
    @Test
    public void testMethod() {
    }
    @Factory
    public Object[] createTestClasses() {
        Object[] data = new Object[2];
        data[0] = new TestClass("chrome");
        data[1] = new TestClass("firefox");
        return data;
    }
}
22
Q

The default constructor is required when the data provider method is not static.

A
public class TestClass {
    private String browser;
    public TestClass() {
    }
    @Factory(dataProvider = "data")
    public TestClass(String browser) {
        this.browser = browser;
    }
    @Test
    public void testMethod() {
    }
    @DataProvider
    public Iterator data() {
        List arr = new ArrayList<>();
        arr.add(new Object[] { "firefox" });
        arr.add(new Object[] { "chrome" });
        return arr.iterator();
    }
}
23
Q

if the @DataProvider annotated method is not present in the same class

A

we need to provide @Factory(dataProviderClass = DataProviderFactory.class, dataProvider = “createData”), and the @DataProvider annotated method needs to be public static.

24
Q

TestNG listeners

A

several interfaces that allow us to change the behavior of TestNG at runtime

25
Q

Programmatically re-running failed tests

A
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class SampleRetryAnalyzer implements IRetryAnalyzer {
  private int retryCount = 0;
  private static final int maxRetryCount = 2;
  @Override
  public boolean retry(ITestResult result) {
    if (retryCount < maxRetryCount) {
      retryCount++;
      return true;
    }
    return false;
  }
}
26
Q

To avoid adding @Test(retryAnalyzer = SampleRetryAnalyzer.class) to every test method.

A
public class RetryListener implements IAnnotationTransformer {
    public void transform( ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod ) {
        annotation.setRetryAnalyzer(SampleRetryAnalyzer.class);
    }
}
//testng.xml
27
Q

Writing Custom Annotations

A

create custom annotations and use them using TestNG configurations methods to make annotations more customizable per your needs.

28
Q

Example of custom annotation

A

create a single @DataProvider method that reads from different files with a file to read given in a test method.