Say I have the following XML:
<suite name="MATS">
<test name="mats_test">
<groups>
<run>
<include name="mats" />
</run>
</groups>
<packages>
<package name="com.tests" />
</packages>
</test>
</suite>
And each test class in the com.tests package has only one test method with varying group annotations. Will the beforeClass() and afterClass() methods of classes not in the "mats" group be executed?
Before/After methods not in the specified group(s) will not run unless they set alwaysRun to true.
alwaysRun
For before methods (beforeSuite, beforeTest, beforeTestClass and
beforeTestMethod, but not beforeGroups): If set to true, this
configuration method will be run regardless of what groups it belongs
to.
For after methods (afterSuite, afterClass, ...): If set to true, this
configuration method will be run even if one or more methods invoked
previously failed or was skipped.
e.g. Given the following classes:
public class AMatsTest {
#BeforeSuite(groups = {"mats"})
public void beforeSuite() {}
}
public class NotAMatsTest {
#BeforeSuite
public void beforeSuite() {}
}
#Test(groups = {"mats"})
public class AnotherMatsTest {
#BeforeSuite public void beforeSuite() {}
}
public class AlwaysTest {
#BeforeSuite(alwaysRun = true)
public void beforeSuite() {}
}
AMatsTest.beforeSuite(), AnotherMatsTest.beforeSuite(), and AlwaysTest.beforeSuite() will be executed.
NotAMatsTest.beforeSuite() will not be executed.
Related
I am trying to run Selenium tests with testNG and have run into a problem when using dependsOnMethods and class factory.
I have a series of test methods in a class which depend on each other in one chain.
public class MyTest {
private String factoryParam1;
private String factoryParam2;
public MyTest(String factoryParam1, String factoryParam2){...do stuff}
#Test
public void test1(){...do Stuff}
#Test(dependsOnMethods={"test1"})
public void test2(){...do stuff}
#Test(dependsOnMethods={"test2"})
public void test3(){...do stuff}
}
I have a factory class like this.
public class MyTestFactory{
#Factory(dataProvider = "argList")
public Object[] createInstances(String arg1, String arg2){
return new Objcect[] {new MyTest(arg1, arg2)};
}
#DataProvider(name="argList")
static public Object[][] argList(){
return new Object[][] {
{"instance1arg1", "instance1arg2"},
{"instance2arg1", "instance2arg2"},
}
}
}
And, finally an xml like this.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0dtd">
<suite name="My test suite">
<test name="My test" group-by-instances="true">
<classes>
<class name ="test.testfactory.MyTestFactory"></class>
</classes>
</test>
</suite>
The first time the class is run with first set of data, everything runs in the correct order. On the second run however, with the second set of data, the test methods run out of order. I assume this is because the methods have already been marked as 'successful' in the first run, so all method dependencies are satisfied for each subsequent run. For me, the desired behavior is for the order to maintained on each run of the test class. How can I achieve this?
I have searched for this but I think I'm not using the correct terminology or something because I haven't found anything.
Here my intention is , Based on the before method condition the #Test method needs to enabled or disabled But in the below code even though I am passing the test case name its not getting skipped ? Can anyone suggest me solution?
I need the BeforeTestMethod to check some logic in my actual code and based on that I have to enable the #Test in the class file
public class ListnerClass implements IAnnotationTransformer {
public static String testName;
public void transform(ITestAnnotation iTest, Class testClass, Constructor testConstructor, Method method) {
if(method.getName().equalsIgnoreCase(testName)) {
iTest.setEnabled(false);
}
}
public class TestNGTest3 {
#BeforeMethod
public void setUp(Method result) {
System.out.println("This is the before Method getting name "+result.getName());
if(result.getName().contains("3"))
{
ListnerClass.testName=result.getName();
}
}
#Test
public void testMethod3() {
System.out.println("This is the Method of Method");
}
#Test
public void testMethod4() {
System.out.println("Hi");
}
}
TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name=" Regression Suite">
<listeners>
<listener class-name="com.listners.ListnerClass" />
</listeners>
<test thread-count="1" name="Test">
<classes>
<class name="com.test.TestNGTest3" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Output:
This is the before Method getting name testMethod3
This is the Method of Method
This is the before Method getting name testMethod4
Hi
Finally I have achieve my intention with the help of the below post.
Why is throw new SkipException() skipping all my methods?
Here is an working sample:
public class TestClassTest {
static String name;
static int i=0;
`public static String testmethod() {
List<String> list= new ArrayList<String>();
list.add("Raja");
list.add("Raju");
list.add("Raj");
name=list.get(i);
i++;
return name;
}
public class DependsOnMethodClass extends TestClassTest {
#BeforeMethod()
public void beforecaller(Method m) {
if(!testmethod().equals("Raju")) {
System.out.println("This is the method going to be skipped
"+m.getName());
throw new SkipException("Data provided is not matching");
}
else {
System.out.println("This is the method not skipped"+m.getName());
}
}
#Test
public void getCall() {
System.out.println("This is the getCall method");
}
#Test()
public void getCall1() {
System.out.println("This is the getCall1 method");
}
}
Update of TestNG xml: (Important Change)
Adding the parameter configfailurepolicy="continue" will continue the test even we
are throwing skip exception.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" configfailurepolicy="continue">
<test thread-count="5" name="Test">
<classes>
<class name="com.testng.newsuite.DependsOnMethodClass"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Output:
This is the method going to be skipped getCall
This is the method not skipped getCall1
This is the getCall1 method
I have two separate packages in my project, one for integration tests and one for unit tests, my testng.xml looks as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="All test cases" verbose="1" parallel="classes">
<test name="Integration Tests">
<classes>
<class name="com.sample.integration.ClassC"/>
<class name="com.sample.integration.ClassD"/>
</classes>
</test>
<test name="Unit tests">
<classes>
<class name="com.sample.unit.ClassA"/>
<class name="com.sample.unit.ClassB"/>
</classes>
</test>
</suite>
Class C:
public class ClassC {
#BeforeTest
public void beforeIntegrationTests() {
System.out.println("Before Integration tests");
}
#Test
public void classCMethod() {
System.out.println("Executing class C method");
}
}
Class D:
public class ClassD {
#Test
public void classDMethod() {
System.out.println("Executing class D method");
}
}
Class A:
public class ClassA {
#BeforeTest
public void beforeUnitTests() {
System.out.println("Before unit tests");
}
#Test
public void classAMethod() {
System.out.println("Executing class A method");
}
}
Class B:
public class ClassB {
#Test
public void classBMethod() {
System.out.println("Executing class B method");
}
}
If I run the entire test suite it works as expected as follows:
Before Integration tests
Executing class C method
Executing class D method
Before unit tests
Executing class A method
Executing class B method
However, if I try to either run/debug just classAMethod() from ClassA, it runs beforeUnitTests() [expected] and classAMethod() [expected], however it also runs beforeIntegrationTests() which is not expected. As per the official documentation: #BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
How do I configure TestNG and/or IntelliJ to run this correctly?
Side Note: Although I can see that the beforeIntegrationTests() is getting run either by adding a breakpoint in the debug mode or by adding a Thread.sleep in the run mode, the output from this method does not get printed in final console output.
Firstly, is the expectation valid, as in I expect only beforeUnitTests() and classAMethod() to run if I run just the classAMethod().
No, all methods with #BeforeTest annotation will run before execution of method with #Test annotation.
Ideal way to handle this scenario is with groups.
Class A:
public class ClassA {
#BeforeTest(groups="unitTest")
public void beforeUnitTests() {
System.out.println("Before unit tests");
}
#Test(groups="unitTest")
public void classAMethod() {
System.out.println("Executing class A method");
}
}
Class B:
public class ClassB {
#Test(groups="unitTest")
public void classBMethod() {
System.out.println("Executing class B method");
}
}
Class C:
public class ClassC {
#BeforeTest(groups="integrationTest")
public void beforeIntegrationTests() {
System.out.println("Before Integration tests");
}
#Test(groups="integrationTest")
public void classCMethod() {
System.out.println("Executing class C method");
}
}
Class D:
public class ClassD {
#Test(groups="integrationTest")
public void classDMethod() {
System.out.println("Executing class D method");
}
}
testNG XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="All test cases" verbose="1" parallel="classes">
<groups>
<run>
<include name="unitTest"></include>
</run>
</groups>
<test name="Test Suite">
<classes>
<class name="com.sample.integration.ClassC"/>
<class name="com.sample.integration.ClassD"/>
<class name="com.sample.unit.ClassA"/>
<class name="com.sample.unit.ClassB"/>
</classes>
</test>
</suite>
You can make further configuration to this testNg XML as per your needs.
I have a scenario like two different test classes, second test class result depends on the first one
public class class1
{
#test
public void sometest1()
{
// some functionality
}
}
public class class2
{
#test
public void sometest2()
{
// some different functionality
}
}
Here class2 need to be executed if class1 result is pass else class2 needs to be failed.(if no provision for the same i need to execute the test1 in class1 to be executed always first in selenium grid parallel run)
As said avoid please refer :http://testng.org/doc/documentation-main.html#annotations.
As per above comments, i hope you are trying use depends on for method which is in another class. In such a case you need to provide complete path ie package.class.method.
class1:
public class testingcode {
#Test
public void test1(){
System.out.println("test1");
}
}
Class2:
public class testcase1{
#Test(dependsOnMethods={"com.test.testingcode.test1"})
public void testcase1result() throws InterruptedException{
System.out.println("test");
}
}
Run from TestNG.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none" preserve-order="true">
<test name="Test">
<classes>
<class name="com.test.testingcode"/>
<class name="com.test.testcase1"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Let me know, if it is not working..
Thank You,
Murali
There are some annotations in testng that allows you the flow that you want.
I have never worked on testng but I think you can find the suitable annotation here :http://testng.org/doc/documentation-main.html#annotations
might be dependsOnGroups help you
You can use groups and dependency in testng to run a test after some test have executed. There is no way(atleast to my knowledge) to fail the Class2 test if Class1 test failed.
public class Class1Test {
#Test(groups="RunFirst")
public void sometest1() {
Assert.fail("just to skip the other test");
}
}
public class Class2Test {
#Test(dependsOnGroups="RunFirst")
public void sometest2() {
System.out.println("I am running");
}
}
Here the Class2Test will run after Class1Test has run and will be skipped if Class1Test failed.
You can use dependsOnMethods like below
#Test
public void test1() {
// Instantiate another case class
// Call your testcase
}
#Test(dependsOnMethods="test1")
public void test2() {
}
I am experimenting with testng. My goal is to have test methods in several classes and "supporting" methods for preparation and wrap-up of a bunch of tests in a separate class.
Another requirement is that in a test suite the supporting methods have to be called for multiple test parts. E.g. a first part containing testA and testB, and a second part containing testC and testD. This would result in the following steps:
support1, testA, testB, support2, support1, testC, testD, support2
My first approach that (partly) worked was to annotate all methods with #Test, use groups and define dependencies between groups, e.g. the test methods depend on a group "setUp", which is a group of one supporting method "support1" in the above example.
The problem with this approach is that the supporting methods count as tests, so the generated report shows the wrong number of "real" tests.
The next idea was to use #BeforeGroups and #AfterGroups, put the supporting methods in a group, and use group dependencies. The supporting methods should not be counted as tests any more. But I am stuck at the very beginning.
For example I tried
#BeforeGroups (groups = {"setUp"})
for a setup method in class Support, and
#Test(groups = { "TestA" }, dependsOnGroups = { "setUp" })
in a "real" test class. This results in the following (simplyfied) error:
[testng] DependencyMap::Method "TestClass.testSomething()[...]" depends on nonexistent group "setUp"
Why is group "setUp" nonexistent? Did I overlook something?
Or is there another approach which works?
Thanks for your help!
Edit:
The tests are started with Ant and I use a testng.xml like this:
<test name="TestA">
<groups>
<run>
<include name="setUp" />
<include name="TestA"/>
<include name="tearDown"/>
</run>
</groups>
<classes>
<class name="seleniumtest.test.technical.Support"/>
<class name="seleniumtest.test.business.TestClassA"/>
</classes>
</test>
<test name="TestB">
<groups>
<run>
<include name="setUp" />
<include name="TestB"/>
<include name="tearDown"/>
</run>
</groups>
<classes>
<class name="seleniumtest.test.technical.Support"/>
<class name="seleniumtest.test.business.TestClassB"/>
</classes>
</test>
I got the glitch!!
The problem is with the annotation
#Test(groups = { "TestA" }, dependsOnGroups = { "setUp" })
Basically your error message is trying to say that there is no #Test method with groupname as setUp!! Coming to your question, the solution is to modify annotation for the test method as below
#Test(groups = { "TestA" })
And in the support method modify the annotation
#BeforeGroups (groups = {"TestA"})
I ran a sample example with this set up
public class TestSupport {
#BeforeGroups(groups = { "sample","sample1" })
public void beforeTest() {
System.out.println("Before test");
}
#AfterGroups(groups = { "sample","sample1" })
public void afterTest() {
System.out.println("after test");
}
}
and with my Test class as
public class TestClassA {
#Test(groups = { "sample" })
public void superTestA() {
System.out.println("This is the actual test");
}
#Test(groups = { "sample" })
public void superTestB() {
System.out.println("This is the another test under sample group");
}
#Test(groups = { "sample1" })
public void superTest() {
System.out.println("This is another test");
}
}
and my testng.xml as shown below
<test name="sampletest" >
<groups>
<run>
<include name="sample" />
<include name="sample1" />
</run>
</groups>
<classes>
<class name="test.global.testng.TestClassA"/>
<class name="test.global.testng.TestSupport"/>
</classes>
</test>
Now this is how the test runs: beforeGroups-> superTestA/superTestB ->afterGroups and beforeGroups-> superTest -> afterGroups and closes off
I think I have come up with the solution I wanted.
What I need to use is #BeforeTest and #AfterTest instead of #BeforeGroups and #AfterGroups, respectively, in the support class:
#BeforeTest(groups = {"setUp"})
public void beforeTest() {[...]}
#AfterTest( groups = {"tearDown"})
public void afterTest() {[...]}
In the test class:
#Test(groups = { "TestA" })
public void testSomething() {[...]}
The dependsOnGroups is gone, as in Patton's approach.
The testng.xml is unchanged compared to my question. I.e. the tests can be configured in the testng.xml file, without having to change java code.
Moreover, this solution also gets rid of another problem of the BeforeGroups approach, at least as supposed by Patton (#Patton I do not mean to offend you).
With the latter a test using several test groups does not run as intended, because the beforeTest() method would be run before any of the groups. E.g. if you have the following test (extract of testng.xml):
<groups>
<run>
<include name="TestA"/>
<include name="TestB"/>
</run>
</groups>
... the resulting steps of execution are:
beforeTest(), TestA, beforeTest(), TestB, afterTest().
Using the solution with BeforeTest, you would have the following test:
<groups>
<run>
<include name="setUp" />
<include name="TestA"/>
<include name="TestB"/>
<include name="tearDown"/>
</run>
</groups>
... the resulting steps of execution are:
setUp = beforeTest(), TestA, TestB, tearDown = afterTest().
package com.test.MySample;
import org.testng.annotations.*;
public class TestNGTest1 {
#BeforeTest
public void BeforeTest() {
System.out.println("#BeforeTest");
}
#BeforeClass
public void BeforeClass() {
System.out.println("#BeforeClass");
}
#BeforeGroups (groups = {"My group"})
public void BeforeGroups() {
System.out.println("#BeforeGroups");
}
#BeforeGroups (groups = {"My group1"})
public void BeforeGroups1() {
System.out.println("#BeforeGroups1");
}
#AfterGroups (groups = {"My group1"})
public void AfterGroups1() {
System.out.println("#AfterGroups1");
}
#BeforeMethod
public void BeforeMethod() {
System.out.println("#BeforeMethod");
}
#Test(groups = {"My group"})
public void test1() {
System.out.println("test1");
}
#Test (groups = {"My group", "My group1"})
public void test2() {
System.out.println("test2");
}
#AfterMethod
public void AfterMethod() {
System.out.println("#AfterMethod");
}
#AfterGroups (groups = {"My group"})
public void AfterGroups() {
System.out.println("#AfterGroups");
}
#AfterClass
public void AfterClass() {
System.out.println("#AfterClass");
}
#AfterTest
public void AfterTest() {
System.out.println("#AfterTest");
}
}