Maven TestNG surefire plugin run groups via terminal - maven

Im currently working on trying to get the maven command line to run a certain group. I have annotated my tests as follows:
#Test(priority = 2, groups = "MyGroup")
public void myFirstTest(){};
then I I kick off the test run via command line like this:
mvn clean install test -Dgroups=MyGroup
Aaaaannnndddd nothing runs. Maven starts up, then quits saying there are no tests. Like so:
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 38.071 sec - in TestSuite
Now I tried a few things. 1st. I tried putting a testng.xml file and adding the group name "MyGroup" to the file. I then reference this file inside my pom.xml as such:
<suiteXmlFiles>
<!-- command to run "mvn clean test -DsuiteFile=src/test/resources/testng.xml" -->
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
When i the re-run using the command line it works. Now i know we dont need to add an xml file specifying the group names. As this is made dynamically by maven, so then what am I missing.
Also, i put a breakpoint in the method overridden transformer
#Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
And annotation.getGroups() is always an empty array. This means that maven is not passing in the test groups. Why!!!. I really dont see it and been hacking away at this for hours, Any help would be greatly appreciated.

Related

Maven Tests not Running in sequential Order

I'm using a TestNG framework for my automation project.
While running from command line i'm giving the following command.
mvn clean test -Dtest=Login,OpenImage,Logout
By running the above command it the order of execution was Login->Logout->OpenImage (may be in alphabetical order).
Can anyone help me how to run tests in the given order.
Note: As per my requirement I need to run my tests in the above way it self.
If it was through testNG.xml file then i guess preserve-order will work.
can anyone help me on this.....!!!!!
Thank you in advance..
First why do you need to run tests in a particula order because units should never rely on a particular order. But your question looks like more an integration tests.
If you need to run in order defined dependencies between the tests
#Test
public void serverStartedOk() {}
#Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
The above defines the order that serverStartedOk will run before method1..based on the dependsOnMethods...

User defined system variable throws null pointer exception when executed as testng suite

There has been a situation when I need to run with the user defined <systemPropertyVariables> in one of my utility class.
<systemPropertyVariables>
<environment>uat</environment>
</systemPropertyVariables>
I have defined this in my pom.xml file.
inside my utility class I am extracting this value using below
System.getProperty("environment");
When I run the test case through pom.xml it runs fine. But when I try to run it through testing.xml file or as a testng test, it gives me nullpointerException.
I have defined vmarguments in run configuration as a temp solution but when this code gets distributed again this won't work as run configuration is limited to the local machines.
Can anyone please help me on how to resolve this error. Where should I define the SystemPropertyVariable so that it also works when I run the testng suite individually.
One thing you can do is pass this values from testNG using parameter tag and you can retrieve these values in Test class. By this you can run this both from maven and testng
Hi i have found a solution for this if any one else is stuck with this type of problem while designing framework.
Write a simple java code
try{
your code
}catch(NullPointerException ee){
environment="";
e.printStackTrace();
}catch(Exception e){
Assert.assertTrue(fail);
}
it only handles your code when you run via testng.xml (which is not part of your major test runs) and fails when we get any other exceptions.

Dependency Injection failure during mvn install loading standalone run

I have a RESTful service created using Spring MVC. I created an integration test to test my Rest controller, using spring-test mvcmock.
When I am running this test using run as Junit. It is running fine.
However, when the same test is getting executed during mvn install, using surefire plugin. This test is complaining throwing fatal error:
No qualifying bean of type ...
When I checked previous logs it says that it loaded 0 dependecy.
This was the log :
DEBUG XmlBeanDefinitionReader - Loaded 0 bean definitions from location pattern [classpath*:service-test-context.xml]
However, I again run this test class as Junit and I could see "loaded 23 beans at the same log line."
Question: Could you please suggest, what can be the issue?
Note:
This is multi maven module application and dependency of another module is not getting injected.
I was getting same issue, when I was running this test as Junit. Then I added dependent modules in eclipse > build > project and issue got resolved in run as JUnit. Now getting this issue while doing mvn install.
Does your surefire configuration contain an includes parameter?
"A list of elements specifying the tests (by pattern) that should be included in testing. When not specified and when the test parameter is not specified, the default includes will be
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
"
Easy fix is to rename your test runner so that it ends in Test.java. Then
mvn test
will pick it up.
Updated
An alternative is this:
"test:
Specify this parameter to run individual tests by file name, overriding the includes/excludes parameters. Each pattern you specify here will be used to create an include pattern formatted like **/${test}.java, so you can just type "-Dtest=MyTest" to run a single test called "foo/MyTest.java". The test patterns prefixed with a ! will be excluded.
This parameter overrides the includes/excludes parameters, and the TestNG suiteXmlFiles parameter. Since 2.7.3, you can execute a limited number of methods in the test by adding #myMethod or #my*ethod. For example, "-Dtest=MyTest#myMethod". This is supported for junit 4.x and testNg.
Since 2.19 a complex syntax is supported in one parameter (JUnit 4, JUnit 4.7+, TestNG):
"-Dtest=???Test, !Unstable*, pkg/**/Ci*leTest.java, Test#testOne+testTwo?????, #fast*+slowTest"
"-Dtest=Basic*, !%regex[..Unstable.], !%regex[..MyTest.class#one.|two.], %regex[#fast.|slow.*]"
The Parameterized JUnit runner describes test methods using an index in brackets, so the non-regex method pattern would become: #testMethod[]. If using #Parameters(name="{index}: fib({0})={1}") and selecting the index e.g. 5 in pattern, the non-regex method pattern would become #testMethod[5:].
Type: java.lang.String
Required: No
User Property: test
"
So if your test runner was named foo/MyVeryOwnRunner.java you could do the following
mvn clean test -Dtest=MyVeryOwnRunner

I want to run many SOAPUI project xmls using Gradle script, in Linux

I want to run the SOAPUI project xmls using Gradle script. The GRADLE script should read the project xmls from soapuiInputs.properties file and run automatically all. Please guide me step by step how to create Gradle script to run the SOAPUI projects in Linux server.
Note: We use SOAPUI version 5.1.2.
Probably the simple way is to call the SOAPUI testrunner directly from gradle as Exec task, like you can do from cli.
In gradle you can define the follow tasks (Note that I try it on windows but to do the same on linux as you ask simply you've to change the paths):
// define exec path
class SoapUITask extends Exec {
String soapUIExecutable = 'C:/some_path/SoapUI-5.2.1/bin/testrunner.bat'
String soapUIArgs = ''
public SoapUITask(){
super()
this.setExecutable(soapUIExecutable)
}
public void setSoapUIArgs(String soapUIArgs) {
this.args = "$soapUIArgs".trim().split(" ") as List
}
}
// execute SOAPUI
task executeSOAPUI(type: SoapUITask){
// simply pass the project path as argument,
// note that the extra " are needed
soapUIArgs = '"C:/location/of/project.xml"'
}
To run this task use gradle executeSOAPUI.
This task simply runs a SOAPUI project, however testrunner supports more parameters which you can pass to soapUIArgs string in executeSOAPUI task, take a look here.
Instead of this if you want to deal with more complex testing there is a gradle plugin to launch SOAPUI project, take a look on it here
Hope this helps,

gradle ask recognize the TestIING suite.xml

I'm using Gradle 1.7 and Test-ING in my project along with selenium, the problem which I'm facing is that I have defined a xml file for the sequence of tests that I want to take place. The line for the test defined in build.gradle is as follows-
test {
useTestNG(){
options.suites("src/test/resources/sample.xml")
}
}
Gradle is unable to recognize the sample.xml file and is not executing any test, its just skipping.
Can anyone please help me in this. I want the tests to be executed in the same order as defined in sample.xml as this already the current implementation of the project which can't be altered.

Resources