How to use a test class from a jar in TestNG - java

I have a JAR(xyz.jar) with test class test.sample.ClassX. I am trying to use this class in testng.xml shown below,
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteX" verbose="1" >
<test name="TestX" >
<classes>
<class name="test.sample.ClassX" />
</classes>
</test>
</suite>
My pom.xml has dependency(xyz.jar) added. I can execute this suite from eclipse but fails when i try to execute from command-line(mac) with below error,
error: package test.sample does not exist
Any idea what the issue might be ?

My first guess is that you might not have a class defined like:
package test.sample;
public class TestX
{
...

Related

TestNG executes beforesuite method of the last test first always when initializing the multiple test cases declared in a test suite

In summary, I have 2 tests declared in a test suite as below however the testng runner executes the beforesuite method of the last test always. This results in the test reports shows there are 3 tests executed which is not expected. By trailing the code, I found the privateRun() overrides the beforesuite method for the last test because it iterates itselft for the 2 testRunners.
Please advise how I can run the beforesuite always for the first test instance, not the last one of the suite.
Here is the test structure.
Test.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suiteA">
<test name="T1">
<classes>
<class name="C1"/>
<class name="C2"/>
</classes>
</test>
<test name="T2">
<classes>
<class name="C3"/>
<class name="C4"/>
</classes>
</test>
</suite>
Test Run
Suite A
T2 *(this doesn't run any tests other than Beforesuite)*
>T1
>T2

How to run a single test from TestNG.XML file in command line from Gradle Kotlin

I have testNG file which has multiple methods.
In this testNG file I added the parameters to pass the parameter on code from xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="LocalIosTestSuite" parallel="tests" thread-count="1" >
<test name="iphonetest">
<parameter name="platformName" value="iphonetest" />
<classes>
<class name="tests.TestKotlin">
</class>
</classes>
</test>
<test name="androidtest">
<parameter name="platformName" value="androidtest" />
<classes>
<class name="tests.TestKotlin">
</class>
</classes>
</test>
</suite>
Kotlin Class
import org.testng.annotations.Parameters
import org.testng.annotations.Test
class TestKotlin {
#Parameters("platformName")
#Test()
fun Testmethod(platformName:String)
{
println("TestKotlin")
println(platformName)
}
}
Command to run a single test from testng file
./gradlew test -Psuite5 -testnames iphonetest
This is not working for me please help me any solution to run a specific test on gradle

Parallel execution of tests in testNG Selenium

I am not able to invoke the browsers in parallel, which are currently invoking one after another. Need a way to invoke the browsers in parallel tests.
NOTE: In my configuration xml file I have kept the thread count as 2.
Below is my configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd"; >
<suite name="Parallel" parallel="tests" thread-count="4" >
<test verbose="3" name="<name>">
<parameter name="platform" value ="win8"/>
<parameter name="browsername" value ="internet explorer"/>
<classes>
<class name="com.parallel.execution.ParallelExecution">
<methods>
<include name="testmethod1"/>
</methods>
</class>
</classes>
</test>
</suite>
We have to define two attributes 'parallel' and 'thread-count' in simple testng.xml file. See below:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Execution suite" parallel="methods" thread-count="2">
<test name="Regression 2">
<classes>
<class name="com.parallel.TestParallelExecution"/>
</classes>
</test>
</suite>
In above, as we want the test methods to be executed parallel, we have set the parallel attribute as 'methods' and thread-count attribute will control the max number of threads to be created.
Re-installing TestNG solved the above problem.
You have to explicitly write code to invoke the browser as part of a #Before configuration to invoke the browser every time a #Test is run . I will specify one of the many approaches so that you get an idea.
<suite name="Parallel" parallel="tests" thread-count="4" >
<test verbose="3" name="test1">
<classes>
<class name="com.parallel.execution.ParallelExecution1"/>
</classes>
</test>
<test verbose="3" name="test2">
<classes>
<class name="com.parallel.execution.ParallelExecution2"/>
</classes>
</test>
</suite>
Consider a suite file with 2 tests set to run in parallel. What we expect is #Test methods in ParallelExecution1 runs in first browser and #Test methods in ParallelExecution2 runs in second browser. So you need a mechanism by which you can invoke browser sessions and run your test methods. Enter BaseTest class.
public abstract class BaseTest {
protected WebDriver driver;
#BeforeTest
#Parameters({"browser"})
public void init(String browser) {
// Initialize your browser here. Code below is dummy
driver = new FF();
}
#AfterTest
public void end() {
driver.close();
driver.quit();
}
}
Now inherit this 'BaseTest' in both your test classes.
public class ParallelExecution1 extends BaseTest {
#Test
public void test1() {
}
}
public class ParallelExecution2 extends BaseTest {
#Test
public void test2() {
}
}
Now both the tests have #BeforeTest and #AfterTest methods which will invoke the browsers.

Why is my TestNG xml file not targeting my Java Class?

Why is my TestNG xml file not targeting my Runner Class?
My XML File:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Cucumber Framework" verbose="2">
<test name="Cucumber Tests">
<classes>
<class name="runners.MainRunner"></class>
</classes>
</test>
</suite>
location of the runner class:
CucumberFramework\src\test\java\CucumberFramework\runners
CucumberFramework\src\test\java\CucumberFramework\runners\MainRunner.java
Please note: if i execute the runner class directly, everything works accordingly, it seems the xml file is unable to locate the runner class, any ideas?
Runner class code:
package CucumberFramework.runners;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions (
features = {"src/test/java/CucumberFramework/featureFiles/"},
glue = {"CucumberFramework.steps"},
monochrome = true,
tags = {},
plugin = {"pretty", "html:target/cucumber","json:target/cucumber.json", "com.cucumber.listener.ExtentCucumberFormatter:target/report.html"}
)
public class MainRunner {
}
You can execute the junit classes with testng xml file by define the property junit="true" as given below
<suite name="Cucumber Framework" >
<test name="Cucumber Tests" junit="true">
<classes>
<class name="CucumberFramework.runners.MainRunner" />
</classes>
</test>
</suite>

Show all failed tests of multiple test suite in one testNG report

Actually I'm testing multiple website for my company. All the website have the same structure and i'm testing various things on each.
For my test i have a Selenium Java Maven project using TestNG. For each web site, i've created a sub folder and generate a specific testing.xml file to specify each test i'm doing for the website.
Here the structure : http://imgur.com/g0VHfMw
My testing.xml files are like this :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test suite website 1" parallel="tests" thread-count="2">
<test name="Test1">
<classes>
<class name="package.for.mytest1"></class>
</classes>
</test>
<test name="Test2">
<classes>
<class name="package.for.mytest2"></class>
</classes>
</test>
<test name="Test3">
<classes>
<class name="package.for.mytest3"></class>
</classes>
</test>
</suite>
How can I merge this 4 test suite in one testNG report ? And if it's possible, is there a way to only show the unstable or failed test on it ?
Any ideas ? Thanks for your help
Try Allure it could easily generate combined report for several suites and show only failed/broken tests by default.
Either you can Use Suite-XML tag in TestNG.xml file to report all your test in single report or implement a reporter of your own to collect all the results and then at the end flush everything into report of your own format with whatever information you need.
You can create a separate testng.xml file in which you can give the path of other testsuites files.
eg:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name= "Allsuites" verbose="1" preserve-order="true" >
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
</listeners>
<suite-files preserve-order="true">
<suite-file path="testng1.xml" />
<suite-file path="testng2.xml" />
<suite-file path="testng3.xml" />
</suite-files>
</suite>

Categories