I am using Selenium and JUnit to automate some testing. I want to be able to export this into a runnable jar file. I was not able to and I am assuming it is because there is no main method, JVM doesn't know what to run....
I saw this post how to export (JUnit) test suite as executable jar and it suggested adding a main and running the JUnit from there. (I would have commented on that post, but it didn't let me 😢)
public static void main(String[] args) {
JUnitCore.main("folder.package.testClass");
}
I made a new java class with this main method in it but the test class is never executed (from eclipse). Also if I try to export the project, I get errors and it doesn't export.
For my purposed of exporting all the JUnit classes into 1 Jar file and running from there, what the best way I could do this? I might event build a small menu where the user can pick which test class they want to run (which I'm assuming will require a main method some where....).
You can execute test by doing following(just checked current scenario):
Create your test under test folder:
package com.example.junit5; import static org.junit.Assert.assertTrue; import org.junit.Test; public class FirstTest { @Test public void testTrue() { System.out.println("Executing testTrue()"); assertTrue(true); } }
Create your main(executor) class under src folder:
package com.example.junit5; import org.junit.runner.JUnitCore; public class Executor { public static void main(String[] args) { JUnitCore.main("com.example.junit5.FirstTest"); } }
Execute your test as Java Application . Result should be:
My environment config is:
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
Hope this will help
The following code ended up working for me.
package myPackageName;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class testRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(AllTests.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.