Repeating of tests' subsequence using TestNG - java

I want to repeat performing of some subsequence of the test methods in my TestNG project. Let's say I have the following test class:
#Test
class Abc
{
public void a() {}
public void b() {}
public void c() {}
public void d() {}
public void e() {}
}
And the following testng.xml:
<suite name="My suite" verbose="1">
<test name="simple test">
<classes>
<class name="Abc">
<methods>
<include name="e"/>
<include name="d"/>
<include name="c"/>
<include name="b"/>
<include name="a"/>
</methods>
</class>
</classes>
</test>
</suite>
How I can repeat performing of subset of test that begins from the "c" method, so that after the last method in test sequence is completed the methods "c", "b", "a" will be called ?

You may use:
groups and dependsOnGroups;
dependsOnMethods;
or priority;
use Factory;
Run TestNG programitically.
All of them will help you to order you tests if you really needs it.
See code and xml example.

Related

How to configure TestNG to run just a single test case in IntelliJ?

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.

Configuration of Parallel running at Class level in TestNG

I am trying to find out how to run all my TestNG tests in one class first, then all in the second class second and so forth. I need to use parallel running to speed execution though. These are selenium tests so can be slow running.
Given the following TestNG suite file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" parallel="classes" thread-count="2">
<test name="parallel-running-test">
<classes>
<class name="com.mycompany.myproject.mypackage.MyFirstClassTest"/>
<class name="com.mycompany.myproject.mypackage.MySecondClassTest"/>
</classes>
</test>
..and the following 2 classes:
Class one:
public class MyFirstClassTest {
#Test
public void myFirstClassTestOne(){
System.out.println("myFirstClassTestOne");
}
#Test (dependsOnMethods = {"myFirstClassTestOne"})
public void myFirstClassTestTwo(){
System.out.println("myFirstClassTestTwo");
}
#Test (dependsOnMethods = {"myFirstClassTestTwo"})
public void myFirstClassTestThree(){
System.out.println("myFirstClassTestThree");
}
#Test(dependsOnMethods = {"myFirstClassTestThree"})
public void myFirstClassTestFour(){
System.out.println("myFirstClassTestFour");
}
#Test(dependsOnMethods = {"myFirstClassTestFour"})
public void myFirstClassTestFive(){
System.out.println("myFirstClassTestFive");
}
}
Class two:
public class MySecondClassTest {
#Test
public void mySecondClassTestOne(){
System.out.println("mySecondClassTestOne");
}
#Test(dependsOnMethods = {"mySecondClassTestOne"})
public void mySecondClassTestTwo(){
System.out.println("mySecondClassTestTwo");
}
#Test(dependsOnMethods = {"mySecondClassTestTwo"})
public void mySecondClassTestThree(){
System.out.println("mySecondClassTestThree");
}
#Test(dependsOnMethods = {"mySecondClassTestThree"})
public void mySecondClassTestFour(){
System.out.println("mySecondClassTestFour");
}
#Test(dependsOnMethods = {"mySecondClassTestFour"})
public void mySecondClassTestFive(){
System.out.println("mySecondClassTestFive");
}
}
Then the output is as follows:
...How can I have the report as:
MyFirstClassTest
myfirstClassTestOne
myfirstClassTestTwo
myfirstClassTestThree
myfirstClassTestFour
myfirstClassTestFive
MySecondClassTest
mySecondClassTestOne
mySecondClassTestTwo
mySecondClassTestThree
mySecondClassTestFour
mySecondClassTestFive
NB - I need dependsOnMethods and have obvs removed all the browser stuff and actual selenium stuff
Your issue is just the way how IntelliJ is displaying the result of tests.
By default, it displays tests by the order they finish.
You can change it and sort them by alphabetical order if you prefer:
Sadly, IntelliJ is not grouping tests by class name (or I didn't find the way to do it).
Maybe you should ask for the feature on https://youtrack.jetbrains.com
Steerpike, you can try running your tests in parallel (instead of classes) so slight changes to your XML should give you what you are after:
!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" parallel="tests" thread-count="2">
<test name="parallel-running-test1">
<classes>
<class name="com.mycompany.myproject.mypackage.MyFirstClassTest"/>
</classes>
</test>
<test name="parallel-running-test2">
<classes>
<class name="com.mycompany.myproject.mypackage.MySecondClassTest"/>
</classes>
</test>
</suite>
Best of luck!

How to order test cases using priority and groups annotation?

there are some flows that i want to test through automation. i am using selenium , maven, java and testNG. i have 2 different class. Let say class A and Class B.
public class A (){
#Test(groups="flow1",priority=0)
public void method a1()
{
}
#Test(groups="flow1".priority=2)
public void method a2()
{
}
#Test
public void method a3()
{
}
and 2nd class is class B
public class b (){
#Test(groups="flow1", priority=1)
public void method b1()
{
}
#Test
public void method b2()
{
}
#Test
public void method b3()
{
}
now i want to achieve flow like below
method a1()
method b1()
method a2()
i had try in this way through testng.xml
<test name="test1">
<groups>
<run>
<include name="flow1" />
</run>
</groups>
<classes>
<class name="a" />
<class name="b" />
</classes>
</test>
but i am not getting that output. it will run only one test cases and then it is skipping others.
i had try also some different way but i am not getting my goal.
can anybody help me
thanks
in your testng.xml just add group-by-instances="true"
<suite thread-count="2" verbose="10" name="testSuite" parallel="tests">
<test verbose="2" name="MytestCase" group-by-instances="true">
<classes>
<class name="com.A.classA" />
<class name="com.A.classB" />
</classes>
</test>
</suite>

Same test class multiple <test> not run

I want to run same test method as part of multiple . TestNG runs my test method only once irrespective. Paramters are different in both cases. Any pointers on how I can achieve this is greatly appreciated
public class Test1 extends TestBase{
#Test
public void test1(){
System.out.println("This si test1");
}
}
public class TestBase {
#Parameters({ "param1" })
#BeforeMethod
public void setup(#Optional("VCHS") String param1) {
System.out.println("the parameter is "+param1);
}
#Parameters({ "param1" })
#BeforeTest(groups="VC2-UI",alwaysRun=true)
protected void baseSetUpVC2EndPoint(#Optional("VC2") String param1){
System.out.println("This is base"+param1);
}
#Parameters({ "param1" })
#BeforeTest(groups="VC1-UI")
protected void baseSetUpVC1EndPoint(#Optional("VC1") String param1){
System.out.println("This is base and "+param1);
}
}
<suite name="Testing" verbose="1" configfailurepolicy="continue">
<test name="VC2-UI">
<parameter name="param1" value="VC2"/>
<classes>
<class name="Test1"></class>
</classes>
</test>
<test name="VC1-UI">
<parameter name="param1" value="VC1"/>
<classes>
<class name="Test1"></class>
</classes>
</test>
</suite>
You can use invocationCount annotation.

testng - how to separate tests and supporting methods?

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");
}
}

Categories