Using Surefire to execute TestNG/Selenium tests in a jar - java

Our project has a suite of Selenium tests that we are currently packaging into a jar with the intent of Jenkins running the tests as a build step. We install the jar into the local repository of a client VM that is configured to have Selenium point back to Jenkins as the host. The trouble we are running into is figuring out a way to get Maven/SureFire to find the Selenium/TestNG tests in the jar we installed. We have a pom with all the dependencies that the tests require on the client, including the jar of tests itself, but when we we run "mvn test" no tests are found. Clearly we are missing something here, any ideas? Thanks.

surefire by default look for file names like Test*.java , *Test.java , *TestCase.java and executes them. if your test doesnt follow any of these pattern then you have to include them explicitly. refer here

Create a main method calling testNG main method and pass your main methods args[] to testNG Main method. Now your test jar will have main method of its to trigger test cases of your desire. You can pass the same paramteres as TestNG to your jar file like testng.xml file or -testClass classapath etc... see example below.
public static void main(String args[])
{
org.testng.TestNG.main(arg);
}
now you just need to create a xml file of test classes and in jenkins use windows batch command to call your jar file on remote machine with required testNG parameters.

Related

How can I run Cucumber Test runner from CLI without using Maven

I have seen the posts -
How to run cucumber file from command line
Cucumber java project without maven - how to run from command prompt if i am having Runner class
But the solutions given there are not very clear.
My CucumberRunner.java looks like -
#RunWith(Cucumber.class)
#CucumberOptions(
features = "src/test/resources/features",
tags="#Regression",
monochrome = false,
plugin = {"pretty",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",
"timeline:test-output-thread/",
"json:target/cucumber-reports/jsonReports/Cucumber.json",
"html:target/cucumber-reports/Cucumber.html"}
)
public class CucumberRunner {
}
Unfortunately because of security reasons I can not install maven on Linux machine. So I want to run this CucumberRunner file from CLI.
As suggested in the previous posts I tried below in my Cucumber class
public class CucumberRunner {
public static void main(String[] args){
Main.main(new String[]{"-g", "src/test/java/stepdefinitions", "src/test/resources/features/Validate.feature"});
}
}
But it gives me exception
Exception in thread "main" java.lang.NoClassDefFoundError: io/cucumber/core/cli/Main
at CucumberRunner.main(CucumberRunner.java:29)
Caused by: java.lang.ClassNotFoundException: io.cucumber.core.cli.Main
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
... 1 more
But This also doesn't give any solution to run on the basis of Cucumber tags.
Any solution or pointer is appreciated.
Kind Regards,
Abhi
In order to run a Java application, you need to be able to resolve ALL dependencies at runtime. This means that, every single library referenced on the POM under the dependencies tag, must be somewhere in the machine where Selenium (Cucumber) is running. Likewise, if those libraries have dependencies of their own, you must also download them somewhere in the machine.
You have a few choices to access the dependent libraries. You need to reference all dependencies as external JARs (reside outside your application JAR) or you need to package them inside your application JAR. You can use a build tool like ANT to build your JAR and map your dependencies similar to how is done in Maven. Obviously the difference is that you need to do what I described before (your build tool will need to know where these JARs are physically located).
To execute, you will obviously need a main method. Because this is using Selenium (Cucumber), your Runner class is ran by JUnit or TestNG (depending on what you are using). After that, the rest is up to you. For example, you could simply create a batch file to launch your application, or you could use a tool like Launch4J to create an executable. Of course, the latter required creating an executable JAR.

"Class not found" when runing feature with karate.jar

I have a feature that execute a function from a Java class. So for this reason I use this command to get the class and create a new instance:
When I run this feature with maven (mvn test -Dtest...) everything is okay. The problem is when I run this feature with karate standalone jar, karate can't find the RCNUtils class.
Error:
org.graalvm.polyglot.PolyglotException: TypeError: Access to host class utilities.RCNUtils is not allowed or does not exist.
GraalJS error: https://github.com/oracle/graaljs/blob/master/docs/user/FAQ.md#typeerror-access-to-host-class-commyexamplemyclass-is-not-allowed-or-does-not-exist
I think it's a classpath problem but I have tried with a lot of differents paths and commands to execute the jar, and nothing works.
I don't know if this is a known issue or if there is a karate example using utilities classes and executed with karate.jar
I tested this problem with differents Karate versions. Actually I'm using Karate 1.1.0
Some of the options I have tried with no results:
Use -w / --workdir param to change working directory with no results
Use java -cp instead of java -jar to set classpath following:
Unable to use read('classpath:') when running tests with standalone karate.jar
Use -Dkarate.config.dir param
Note: I don't think it's a security problem because if I try to get "RCNUtils.java" file with "karate.read()" or "read()" in the same feature, it works. I think because I can put the path to the file. The problem is that I can't put the path to java class in "Java.type()" method
Same error here: Executing Karate jar with mock using external library Spring Framework
Thanks in advance.

TestNg test not running with maven build

I have TestNg unit tests which is supposed to run with my maven clean install.
I don't have any test-suite.xmls in my pom to run testes. Expectation is to run all my test files without any configuration with the maven build.
But this is not happening.
My test class goes like this
public class CreateUtilty{
#Test
public void testScope(){
Creationutiltiy.create("myApp");
// remaing code
}
}
What could have I done wrong ?
Running testNG as you are with no configuration, the surefire plugin expects your test classes to end with Test. Try changing your test class name to CreateUtilityTest and it should be picked up.
The documentation for the maven surefile plugin contains useful information to help you get started.
To find out more about how to include/exlude tests based on naming convention read this.
Try execute your class with following syntax
mvn -Dtest=CreateUtilty test

Maven Surefire: run a single unit test

I know that it's possible to run a specific test class with -Dtest=MyTest. But is it possible to run a specific test within that class?
I.e. if MyTest defines testFoo() and testBar(), is there a way to specify that only testfoo() should be run?
I'm aware that it's trivially easy to do this in an IDE, but I occasionally need to run tests on the command line on another server.
From Running a Single Test Using Maven Surefire Plugin
With version 2.7.3, you can run only n tests in a single Test Class.
NOTE : it's supported for junit 4.x and TestNG.
You must use the following syntax
mvn -Dtest=TestCircle#mytest test
You can use patterns too
mvn -Dtest=TestCircle#test* test
It will be available as of Surefire 2.8, see SUREFIRE-577
Don't think its available. You can work around it by passing some system properties & ignore execution of tests based on the property value. However it does not seem to add a great value add. There is also TestNG which offers additional features.
http://maven.apache.org/plugins/maven-surefire-plugin/examples/testng.html
To execute one Test at a time, run mvn test
mvn -Dtest=MyUnitlTest test
To execute one Test at a time and a specific method from it:
mvn -Dtest=MyUnitTest#method test
where MyUnitTest is the name of your test and #method is the name of your method.
Execute tests with surefire:
mvn surefire:test

How to execute ant using java and captured the output?

I have an ant build file that contains JUnit test suite that I would like to execute.
Currently I just right click and run the build file from Eclipse.
I want to write a java code that can execute the ant build file automatically.
So I just run the code and ant will be executed.
Second is I want to capture the test result. Currently the result is based on JUnit HTML report.
I want to make my own simple test report. I read there is JUnitResultFormatter but I can't
find the instructional step by step how to use it. Can anyone point me the reference?
The easiest way to do that is to use the JunitCore class from java. It is not advised to call the main from ant directly, see the Junit Faq, and http://www.answerspice.com/c119/1497833/how-do-i-run-junit-tests-from-inside-my-java-application.
It is very common to define a main like this for each test case, to be able to run the tests individually from command line. I usually also change the logging settings in those methods, to get more information when I run a single test manually than from within ant.
In order then to create a custom report, you will have to implement a RunListener that creates your report, and register it, as described in the javadoc:
public void main(String... args) {
JUnitCore core= new JUnitCore();
core.addListener(new RingingListener());
core.run(MyTestClass.class);
}
Your listener will then be called before and after each test run, and passed descriptive information about the test that is about to run, and how the test went once it is done.

Categories

Resources