Is it possible to run a testNG XML from java code, in a separate java project with the given path to the XML?
-
You would likely need to load the test classes as well as any other required classes unique to that other project into your runtime classpath. But, yes, you should be capable of it. I would ask yourself though, is this the best way to do whatever you're trying to do. You could be looking at few or many classes+jars that you would need to manually include in your classpath.– JulianCommented Jul 6, 2016 at 16:15
-
Hi @JulianCleary. I'm trying to create a GUI that can be used to load up a test suite and when its selected it is executed. I have it all set up and I have a trigger that when an XML file is selected , it will be executed. I'm just returned with an error saying it can't find the first test class that is specified in the XML. I always taught it would just be the exact same as if you where to run if from Eclipse, that your just telling something else to run it?– colinCommented Jul 7, 2016 at 7:40
2 Answers
The simplest way to start test automation project from another one would be to sent command line command:
Process process = Runtime.getRuntime().exec("java org.testng.TestNG C:\path-to\testng.xml");
or call some parametrized .bat file so not to write lots of dependencies in java code. E.g.:
Process process = Runtime.getRuntime().exec("run-suite.bat C:\path-to\testng.xml");
And run-suite.bat:
java -cp [lots of *.jar files] org.testng.TestNG %1
May be you also need to deal with results and sure you'll have to handle possible exceptions.
-
Hi Ivan, I looked into this way of doing stuff, however the path to my XML file is stored in a variable. Can you use variables in the command line code you have shown above ? and what is " Java org.testng.TestNG " doing in the command? Thanks Ivan !– colinCommented Jul 8, 2016 at 7:39
-
@colin, you can check this line explanation in testng docs and in this stack question Commented Jul 8, 2016 at 8:11
-
Thanks Ivan, my project has to be dynamic but it should work ! :) Thank you.– colinCommented Jul 8, 2016 at 8:42
-
I have tried the command, but I keep getting "Cannot load main class org.testng.TestNG " . Here is the path I am using " java -cp C:\users\reganc3\desktop\work\aaaaa\bbbb\ccccc\libs;C:\Users\reganc3\git\aaaa\bbbb\cccc\ddd\eeee\ffff\gggg\hhhh\iiii\testclasses\ C:\users\reganc3\desktop\TestXML.xml– colinCommented Jul 8, 2016 at 10:55
-
@colin, there is no 'org.testng.TestNG' in your command. Try to fix this command in command prompt. Commented Jul 8, 2016 at 11:01
Eclipse does a lot of voodoo behind the scenes to make sure everything is plugged together in a really big an robust way. Good for your development environment, but way overkill for production.
Java only knows to look in very specific places for bytecode to load into it's runtime.
If you want runtime flexibility to load classes within your gui from anywhere, it sounds like you will want to look into either extending the classloader so you can add to your classpath at runtime. Or do a more hacky solution and use reflection to bypass the scope of the standard classloader.
Here is an example of a custom classloader that can load jar files and search for class bytecode inside: http://kalanir.blogspot.com/2010/01/how-to-write-custom-class-loader-to.html
This question might be useful to go through as well as a couple of the answers are informative: https://stackoverflow.com/questions/7884393/can-a-directory-be-added-to-the-class-path-at-runtime
Another totally different option, if you don't want or need the ability to dynamically alter your classpath at runtime, would be to preemptively either add the folder your testng tests are in to your CLASSPATH environment variable, or move them to a folder that is already defined in your CLASSPATH.
Remember though, if those tests have any 3rd party dependencies, no matter what solution you choose. Make sure those get added too otherwise you'll just run into the same problem the moment your test tries to invoke it.
-
Thank you Julian, this has been a brilliant answer and help explain a good deal to me. What I couldn't get my head around was the difference of running from eclipse and running from the GUI. I can now see how Eclipse helps tie loose ends together , where code can't. I will have a look into your recommendations ! Thanks again, much appreciated.– colinCommented Jul 8, 2016 at 7:38