TestNg Flashcards

1
Q

What is the TestNG framework? What is the advantage we get using the TestNG framework? Difference between TestNG and Junit? What annotations are there in TestNG that are not in Junit?

A

TestNG is a testing framework helpful for various tests, including unit and integration testing. It’s open source, making it easier to create test cases systematically. With many annotations, it allows defining test priorities and sequences, supports grouping, generates HTML reports, and enables data parameterization. Notably, it was built on top of JUnit. Unlike JUnit, TestNG supports testing in groups and configuring dependency tests. It also allows test prioritization and parallel testing, providing additional control. TestNG introduces extra annotations like @BeforeSuite, @BeforeTest, @BeforeGroups, @AfterGroups, @AfterTest, and @AfterSuite, enhancing control over test setup and teardown.

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

Explain TestNG annotations? What annotations have you worked on? What is the difference between BeforeMethod and BeforeTest?

A

TestNG annotations, such as @Test, @BeforeMethod, and @BeforeTest, are markers in Java that define the execution order and behavior of test methods, where @BeforeMethod is used for setup before each test method, and @BeforeTest is employed for setup shared across multiple test methods within a <test> tag.</test>

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

How to set test case priority in TestNG?

A

we use the priority attribute within the @Test annotation, assigning lower values for higher priority, defining the order of execution.

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

How To Make A Test Dependent On Another?

A

we use the dependsOnMethods attribute within the @Test annotation, specifying the method on which the current test depends, ensuring its execution follows the dependent test.

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

How to disable a test case in TestNG ?

A

Disable a Test Case To disable the test case we use the parameter enabled = false to the @Test annotation.

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

How to run a group of test cases using TestNG?

A

to run a group of test cases in TestNG, we tag our test methods with @Test(groups = “yourGroupName”), and then in our testng.xml file, we specify which groups to include or exclude. It’s like organizing our tests and choosing which group or groups to execute based on our needs.

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

How to skip/exclude a particular Test method/Class from execution in TestNG?

A

Skip/exclude Test Case Using the Exclude tag in our testng.xml

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

What is a suite file? What is the importance of the testng.xml file? How to create and run testng.xml?

A

A TestNG suite file, often named testng.xml, serves as an XML configuration file crucial for defining test suites, specifying test execution parameters, including or excluding test methods, and configuring various settings to orchestrate the execution of test scenarios. To create and run testng.xml, structure the file to include <suite>, <test>, <classes>, and <methods> tags, define the desired test classes and methods, configure parameters, and execute the file through an IDE or the TestNG command line.

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

What are the verification points available in TestNG? Soft Assert vs Hard Assert in TestNG?

A

we use Hard Assert it’s stops the test immediately on failure, or the Soft Assert, it continues executing the following steps and reports all failures at the end, depending on our specific testing needs.

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

How can we create a data-driven framework using TestNG? What will be the logic behind fetching data from a data provider and inserting it on UI?

A

To make a data-driven framework with TestNG, we use the @DataProvider to provide test data. Then, in our test methods, we use that data to interact with the UI—like entering usernames and passwords. It’s like a way to feed different scenarios to your tests, making them more adaptable and reusable.

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

What is a TestNG Listener? How did you use it? How to get a screenshot on failure?

A

TestNG listeners are mainly used to generate the report for the test. Also, we can capture a screenshot when there is a test failure. TestNG events are like on Test Failures, onTestSkipped, onTestSuccess, etc. Using ITestListener interface and method onTestFailure we can get a screenshot of our failed test case.

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

What are the different ways to produce reports for TestNG results?

A

TestNG’s reporting feature creates detailed and summary reports; the detailed report, located in the <index.html> file, includes information on errors, test groups, execution time, logs, and the TestNG XML file. The summary report, found in <emailable-report.html>, provides a condensed view of "Passed," "Failed," and "Skipped" cases, suitable for email sharing with stakeholders. Listeners, acting as interfaces, modify TestNG behavior in Selenium, allowing customization of reports or logs, ExtendReports, an HTML reporting library for Selenium WebDriver in Java, enhances automation frameworks by generating excellent execution reports.</emailable-report.html></index.html>

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

How do you run multiple TestNG Suite Files?

A

To run several TestNG Suite Files, we can use the command line, specifying paths with the java command, create a TestNG XML configuration file with <suite-file> elements, or, in our IDE, we click on the XML files or use a main XML file with multiple <suite-file> elements.

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

How to run test cases in parallel using TestNG?

A

we utilize the “parallel” attribute in the testng.xml file. Set it to “tests” for parallel execution of all test cases within the <test> tag, "classes" for parallel execution of test cases within a Java class, and "methods" for parallel execution of all methods annotated with @Test. For example, in the <suite> tag of the testng.xml file, set "parallel" to "methods" as shown: `<suite>`. Example from and interview: Suppose there are class A and Class B and class B extends class A Class A Class B
@beforemethod @beformethod
@aftermethod @aftermethod
@beforclass @beforeclass
@afterclass @afterclass
@beforetest @beforetest
@aftertest @aftertest
What is the order of execution of TestNG annotations in the above case? In the given scenario where Class B extends Class A, TestNG annotations are executed in the following order: @BeforeTest, @BeforeClass (Class A), @BeforeMethod (Class A), Test Method (Class A), @AfterMethod (Class A), @AfterClass (Class A), @BeforeTest, @BeforeClass (Class B), @BeforeMethod (Class B), Test Method (Class B), @AfterMethod (Class B), @AfterClass (Class B), @AfterTest.</suite></suite></test>

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