TestNG
is a testing framework inspired from JUnit and NUnit but introducing some new
functionality that make it more powerful and easier to use.
It
is an open source automated testing framework; where NG of TestNG means Next
Generation. TestNG is similar to JUnit but it is much more powerful than JUnit
but still it’s inspired by JUnit. It is designed to be better than JUnit,
especially when testing integrated classes.
TestNG
eliminates most of the limitations of the older framework and gives the
developer the ability to write more flexible and powerful tests with help of
easy annotations, grouping, sequencing & parameterizing.
Benefits of TestNG:
There
are number of benefits but from Selenium perspective, major advantages of
TestNG are :
- It gives the ability to produce HTML Reports of execution
- Annotations made testers life easy
- Test cases can be Grouped & Prioritized more easily
- Parallel testing is possible
- Generates Logs
- Data Parameterization is possible
Writing
a test in TestNG is quite simple and basically involves following steps:
Step 1 – Write the business logic of the test
Step 2 – Insert TestNG annotations in the code
Step 3 – Add the information about your test (e.g. the class
names, methods names, groups names etc…) in a testng.xml file
Step 4 – Run TestNG
Annotations in
TestNG:
@BeforeSuite: The annotated method will be run before all tests in
this suite have run.
@AfterSuite: The annotated method will be run after all tests in
this suite have run.
@BeforeTest: The annotated method will be run before any test
method belonging to the classes inside the tag is run.
@AfterTest: The annotated method will be run after all the test
methods belonging to the classes inside the tag have run.
@BeforeGroups: The list of groups that this configuration method
will run before. This method is guaranteed to run shortly before the first test
method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method
will run after. This method is guaranteed to run shortly after the last test
method that belongs to any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first
test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test
methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test
method.
@AfterMethod: The annotated method will be run after each test
method.
@Test: The annotated method is a part of a test case.
Benefits of using
Annotations:
- It identifies the methods it is interested in by looking up annotations. Hence method names are not restricted to any pattern or format.
- We can pass additional parameters to annotations.
- Annotations are strongly typed, so the compiler will flag any mistakes right away.
- Test classes no longer need to extend anything (such as Test Case, for JUnit 3).
1) What is the significance of
<testng.xml> file?
In a Selenium TestNG project, we use
<testng.xml> file to configure the complete test suite into a single
file. This file makes it easy to group all the test suites and their parameters
in one file. It also gives the ability to pull out subsets of your tests or
split several runtime configurations. Few of the tasks which we can group in
the <testng.xml> file are as follows.
1-
Can configure test suite comprising of multiple test cases to run from a single
place.
2-
Can include or exclude test methods test execution.
3-
Can mark a group to include or exclude.
4-
Can pass parameters in test cases.
5-
Can add group dependencies.
6-
Can configure parallel test execution.
7-
Can add listeners.
2) How to pass parameter through <testng.xml> file to a test case?
You
can set the parameter using the below syntax in the <testng.xml> file.
<parameter name="browser"
value="FFX" />
Here,
name attribute represents the parameter name and value signifies the value of
that parameter. Then we can use that parameter in the selenium WebDriver
software automation test case using the bellow syntax.
@Parameters ({"browser"})
3)
How to exclude a @Test method from a test case with two @Test methods? Is it
possible?
Yes,
you need to add @Test method in the exclude tag of <testng.xml> file as
mentioned below.
<!DOCTYPE suite SYSTEM
"http://testng.org/testng-1.0.dtd" >
<suite name="Test
Exclusion Suite">
<test name="Exclusion
Test" >
<classes>
<class
name="Your Test Class Name">
<methods>
<exclude name="Your Test Method Name To Exclude"/>
</methods>
</class>
</classes>
</test>
</suite>
|
4)
How to skip a @Test method from execution?
You
can use the below syntax inside @Test method to skip a test case from test
execution
throw new SkipException("Test
Check_Checkbox Is Skipped");
It will throw skip exception and @Test
method will be ignored immediately from execution.
5)
Can you arrange the below <testng.xml> tags from parent to child?
<test>
<suite>
<class>
<methods>
<classes>
<suite>
<class>
<methods>
<classes>
The <testng.xml> file will have
the following structure.
- The parent tag in the <testng.xml> file is the <suite> tag.
- <suite> tag can include one or more <test> tags.
- <test> tag can include the <classes> tag.
- <classes> tag can include one or more <class> tags.
- <class> tag wraps the <methods> tag where we define the test methods to include or exclude.
Hence, the correct
order of the TestNG tags would be.
<suite>
<test>
<classes>
<class>
<methods>
<test>
<classes>
<class>
<methods>
6)
How to define the priority of @Test method? Also, mention its usage?
In
your Selenium WebDriver project, you can set priority for TestNG @Test
annotated methods as shown in the following example.
@Test(priority=0)
|
Using priority, you
can manage @Test method execution sequence as per your requirement. That means
@Test method with priority = 0 will run 1st and @Test method with priority = 1
will execute 2nd and so on.
7) Can you specify any 6
assertions of TestNG to be used in a Selenium WebDriver software testing tool.
There are multiple assertions
available In TestNG but generally we use the following assertions in out test
cases.
1- assertEquals
2- assertNotEquals
3- assertTrue
4- assertFalse
5- assertNull
6- assertNotNull
2- assertNotEquals
3- assertTrue
4- assertFalse
5- assertNull
6- assertNotNull
8) Why soft assertion is used
in Selenium WebDriver and TestNG automation project?
TestNG soft assertion allows to continue the test execution even
if the assertion is failed. That means once the soft assertion fails, remaining
part of the <@Test> method is executed and the assertion failure is
reported at the end of the <@Test> method.
9)
How to apply regular expression in <testng.xml> file to find @Test
methods containing “product” keyword?
Refer below example,
here we’ve used a regular expression to find @Test methods containing keyword
“product”.
<methods>
<include name=".*product.*"/>
</methods>
|
10) What are the time unit we
specify in test cases and test suites? minutes? seconds? milliseconds? or
hours? Give Example.
Time unit we specify at @Test
method level and test suite level which normally set in milliseconds unit.
11)
List out the benefits of TestNG over Junit?
TestNG framework has
following benefits over JUnit.
- TestNG annotations are more logical and easier to understand.
- Unlike JUnit, TestNG does not require to declare @BeforeClass and @AfterClass.
- There is no method name constraint in Selenium TestNG framework.
- TestNG supports three additional setups:
@Before/AfterTest, and
@Before/AfterGroup.
- In Selenium TestNG projects, there is no need to extend any class.
- In TestNG, it is possible to run Selenium test cases in parallel.
- TestNG supports grouping of test cases which is not possible in JUnit.
- Based on the group, TestNG allows you to execute the test cases.
- TestNG permits you to determine the dependent test cases. Every test case is autonomous to other test cases.
Following are the most common steps for
writing TestNG test cases.
- Write down the business logic of your test.
- Add appropriate TestNG annotations in your code.
- In <build.xml> or <testing.xml>, add the information about your test.
- Run your TestNG project.
13) List out different ways to
run TestNG?
You can run TestNG in the following ways.
- Start directly from the Eclipse IDE, or
- Run using the IntelliJ’s IDEA IDE.
- Run with ant build tool.
- Launch from the command line.
14)
In TestNG how can you disable a test?
To
disable the test case, you can use the following annotation.
@Test(enabled =
false).
15) Explain what does the test timeout
mean in TestNG?
The
timeout test in TestNG is nothing but the time allotted to perform unit
testing. If the unit test fails to finish in that specific time limit, TestNG
will abandon further testing and mark it as a failed.
16) Explain what is parametric
testing?
Parameterized testing lets the
programmer re-run the same test with different values. TestNG allows you to
pass parameters directly to the test methods using the two ways as given below.
1- With testing.xml.
2- With Data Providers.
17) Explain what does
@Test(invocationCount=?) and (threadPoolSize=?) indicates?
- @Test (threadPoolSize=?): The threadPoolSize attribute directs TestNG to create a thread pool to run the test method through multiple threads. With thread pool, the running time of the test method reduces considerably.
- @Test(invocationCount=?): The invocation count refers to the no. of times TestNG should run the test method.
18) What are the different ways
to produce reports for TestNG results?
TestNG
offers following two ways to produce a report.
Listeners: For a listener class, the class has to
implement the org.testng./TestListener interface. TestNG notifies these classes
at runtime when the test enters into any of the below states.
e.g. Test begins, finishes, skips, passes or fails.
Reporters: For a reporting class to implement, the class
has to implement an org.testng/Reporter interface. When the whole suite run
ends, these classes are called. When called, the object consisting the
information of the whole test run is delivered to this class.
19)
How does TestNG allow you to state dependencies?
TestNG
supports two ways to declare the dependencies.
- Using attributes dependsOnMethods in @Test annotations.
- Using attributes dependsOnGroups in @Test annotations.
No comments:
Post a Comment