Vous êtes sur la page 1sur 20

TestNG

What is TestNG?
• TestNG is an automation testing framework in
which NG stands for "Next Generation".
TestNG is inspired from JUnit which uses the
annotations (@).
• Using TestNG you can generate a proper
report, and you can easily come to know how
many test cases are passed, failed and
skipped.
Why Use TestNG with Selenium?

• Generate the report in a proper format including a number


of test cases runs, the number of test cases passed, the
number of test cases failed, and the number of test cases
skipped.
• Multiple test cases can be grouped more easily by
converting them into testng.xml file. In which you can make
priorities which test case should be executed first.
• The same test case can be executed multiple times without
loops just by using keyword called 'invocation count.‘
• Using testng, you can execute multiple test cases on
multiple browsers, i.e., cross browser testing.
Benefits of Using Annotations

• Following are some of the benefits of using


annotations −
• TestNG 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.
• @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.
Parameters
Parameters are keywords that modify the annotation's function.
• Parameters require you to assign a value to them. You do.this by
placing a "=" next to them, and then followed by the value.
• Parameters are enclosed in a pair of parentheses which are placed
right after the annotation like the code snippet shown below.
• TestNG will execute the @Test annotation
with the lowest priority value up to the
largest. There is no need for your priority
values to be consecutive.
@BeforeTest and @AfterTest

@BeforeTest
• methods under this annotation will be
executed prior to the first test case in the
TestNG file.
@AfterTest
• methods under this annotation will be
executed after all test cases in the TestNG file
are executed.
• @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.
• @Test: The annotated method is a part of a
test case .
• DataProvider: A test method that uses dataProvider
will be executed a multiple number of times based
on the data provided by the DataProvider. The test
method will be executed using the same instance of
the test class to which the test method belongs.
• Factory: A factory will execute all the test methods
present inside a test class using a separate instance
of the respective class.
• TestNG factory is used to create instances of test
classes dynamically. This is useful if you want to run the
test class any number of times. For example, if you
have a test to login into a site and you want to run this
test multiple times,then its easy to use TestNG factory
where you create multiple instances of test class and
run the tests (may be to test any memory leak issues).
• Whereas, dataprovider is used to provide parameters
to a test. If you provide dataprovider to a test, the test
will be run taking different sets of value each time. This
is useful for a scenario like where you want to login
into a site with different sets of username and
password each time.
• TestNG @Factory annotation is used to specify
a method as a factory for providing objects to
be used by TestNG for test classes. The
method marked with @Factory annotation
should return Object array.

Vous aimerez peut-être aussi