Run TestNG from Command Line

Ashish Anand
Updated on 12-Jan-2022 13:02:59

12K+ Views

TestNG allows to run the test suites from the command line (cmd). Here's a set of prerequisites that must be fulfilled in order to run a test suite from the command line −testng.xml file should be created to define the test suites and the testing classes to execute.All dependent jars should be available inside a project folder. It includes testing.jar, jcommander.jar and any other jars used in the test cases.Path of bin or out folder where the .class files are stored after the compilation.Approach/Algorithm to solve this problemStep 1 − Create different testing classes having different @Test methodsStep 2 − ... Read More

Obtain Time Taken for Method Execution in TestNG

Ashish Anand
Updated on 12-Jan-2022 12:58:57

1K+ Views

TestNG supports native dependency injection. It allows to declare additional parameters in methods. At runtime, TestNG automatically fills these parameters with the correct values. Here is a set of native dependencies in TestNG −ITestContextXmlTestMethodITestResultThese dependencies help to retrieve the time taken by a Test method to execute. The time taken to execute a Test method can be retrieved only after the execution of the test.If the user wants to get the time taken by the method after its execution, then @AfterMethod can be useful to retrieve it. @AfterMethod supports all these native dependencies. The full access of these dependencies is ... Read More

Pass Variable from BeforeTest to Test Annotation in TestNG

Ashish Anand
Updated on 12-Jan-2022 12:56:32

2K+ Views

A TestNG class can have various TestNG methods such as @BeforeTest, @AfterTest, @BeforeSuite, @BeforeClass, @BeforeMethod, @test, etc. Thereare various scenarios where we need to carry forward a few variables from thesemethods to the main @Test method. Since none of these methods support return type, the best way to pass a variable is using class /instance variable rather thanlocal variable.The scope of class/instance variable is within the entire class. As a result, whatevervalue is set up at @BeforeTest or @BeforeMethod can be utilized at @Test method.In this article, we will see how to pass a variable from @BeforeTest to @Test annotation ... Read More

Retrieve All Test Method Names in TestNG Suite

Ashish Anand
Updated on 12-Jan-2022 12:53:16

1K+ Views

TestNG supports native dependency injection. It allows to declare additional parameters in methods. At runtime, TestNG automatically fills these parameters with the correct values. Here is a set of native dependencies in TestNG −ITestContextXmlTestMethodITestResultThese dependencies help to retrieve the name of the Test methods. If the user wants to retrieve the names of all the Test methods that are going to get executed, then the best place is @BeforeSuite or @AfterSuite.@BeforeSuite and @AfterSuite support only ITestContext. The full access of these dependencies is shown below −AnnotationITestContextXmlTestMethodITestResultBeforeSuiteYesNoNoNoBeforeTestYesYesNoNoBeforeGroupsYesYesNoNoBeforeClassYesYesNoNoBeforeMethodYesYesYesYesTestYesNoNoNoAfterMethodYesYesYesYesAfterClassYesYesNoNoAfterGroupsYesYesNoNoAfterTestYesYesNoNoAfterSuiteYesNoNoNoIn this article, we will use Method dependency to show how to retrieve the names ... Read More

Set Thread Name in TestNG

Ashish Anand
Updated on 12-Jan-2022 12:42:37

835 Views

TestNG supports multithreading, i.e., a @Test method can be invoked multiple times in parallel. TestNG by default assigns the integer ID to the thread. Sometimes, it is required to debug for a specific thread or create custom report for a user-provided thread name. In such a scenario, setting up the thread name is good before execution to easily identify the executed tests/steps.In this article, we will illustrate how to set thread name as user input.Approach/Algorithm to solve this problemStep 1 − Create a TestNG class, NewTestngClass.Step 2 − Write a @Test method in the class NewTestngClass as shown in the ... Read More

Get Test Group Name in TestNG Before and After Execution

Ashish Anand
Updated on 12-Jan-2022 12:37:30

2K+ Views

TestNG supports native dependency injection. It allows to declare additional parameters in methods. At runtime, TestNG automatically fills these parameters with the correct values. Here's a set of native dependencies in TestNGITestContextXmlTestMethodITestResultThese dependencies help to retrieve the description of a Test method, if written. Group name of a Test method can be retrieved before or after the execution of a test.If the user wants to get the group name of a test method prior to its execution, then @BeforeMethod can be useful to retrieve it.On the other hand, if the user wants to know the group of test method is ... Read More

Difference Between @BeforeClass and @BeforeTest Methods in TestNG

Ashish Anand
Updated on 12-Jan-2022 12:31:14

5K+ Views

A TestNG class can have various TestNG methods such as @BeforeTest, @AfterTest, @BeforeSuite, @BeforeClass, @BeforeMethod, @test, etc. As per execution order, @BeforeTest executes first and after that, @BeforeClass does. However, if there are multiple TestNG classes and multiple Tests inside each class, then the behaviour of these methods is noticeable.@BeforeTestThis method will execute only once in the entire execution before calling the first @Test method. It doesn’t matter how many @Test tags are present or how many classes are there having @Test tags, or multiple classes have multiple test tags. Based on the testing.xml file, once the execution starts, @BeforeTest ... Read More

Get Current Invocation Count in TestNG

Ashish Anand
Updated on 12-Jan-2022 12:20:09

5K+ Views

TestNG supports multi-invocation of a test method, i.e., a @Test method can be invoked multiple times sequentially or in parallel. If we want to run single @Test 10 times at a single thread, then invocationCount can be used.To invoke a method multiple times, the keyword invocationCount = is required. For example −@Test(invocationCount = 10)In this example, the @Test method will execute for 10 times each on a single thread.In this article, we will illustrate how to get the current invocation count.Approach/Algorithm to solve this problemStep 1 − Create a TestNG class, NewTestngClass.Step 2 − Write two @Test methods in ... Read More

Add Custom Messages to TestNG Failure

Ashish Anand
Updated on 12-Jan-2022 12:16:38

1K+ Views

TestNG supports a lot of assertions. It has the org.testng.Assert class, which extends the Java object class java.lang.object. Whenever there is a failure, the user wants to get a customized failure message so that the root-cause analysis could be easy. TestNG supports assertion with customized failure message. However, message is completely optional.The syntax is −Assert.(expected, actual, message)If the user doesn't provide a message, TestNG prints a default error message; but if the user sets a message, then TestNG throws the error along with the customized message set by the user.In this article, we will see how to set a custom ... Read More

Retrieve Test Method Name in TestNG Before and After Execution

Ashish Anand
Updated on 12-Jan-2022 11:28:10

4K+ Views

TestNG supports native dependency injection. It allows to declare additional parameters in methods. At runtime, TestNG automatically fills these parameters with the correct values. Here is a set of native dependencies in TestNG:ITestContextXmlTestMethodITestResultThese dependencies help to retrieve the Test method name. A Test method name can be retrieved before or after the execution of the test.If the user wants to get the name of a Test method prior to its execution, @BeforeMethod can be useful to retrieve it.If the user wants to know which Test method is just executed, @AfterMethod can be used.The actual code can be written in either ... Read More

Advertisements