I was trying to automate an Eclipse-based standalone application. I recorded a script using Silk4J and I am also able to run the script separately.
Under the same package I have created one more .java file which invokes the script. But I am not able to run the .java file as "Silk4j Test". The option is not available.
What changes should I make in order to run the .java file which will eventually run the script?
A Silk4J test is actually a JUnit test. Until today I could not find a real difference running a test as Silk4J test or as JUnit test.
If the option "Run as Silk4J test" and "Run as JUnit test" are not available, the most likely reason is that your method is not marked as a JUnit test.
You need the #Test annotation:
import org.junit.Test;
[...]
#Test
public void test()
{
protected Desktop desktop = new Desktop();
BaseState baseState = new BaseState();
baseState.execute(desktop);
[...]
}
Related
Is there anyway to run cucumber scenarios using the TestNG runner by right clicking individual scenarios in the feature files rather than using the command file or running the TestNG runner file directly?
I'm using Intellij to run cucumber scenarios in a maven testing framework. In the POM.xml file I have the Surefire plugin referencing the testNG.xml file which points to the TestNG runner class.
When I run "mvn test" from terminal it calls the TestNG testrunner,but when I right click on the scenario in the feature file to select run, it runs the io.cucumber.core.cli.main class that calls the io.cucumber.core.runner.Runner class testrunner.
I can't edit Run/Debug configuration to use the TestNG runner because the TestNG runner doesn't have a Main method.
you can create a runner class and add tags you need into CucumberOptions.
#CucumberOptions(plugin = {"pretty"}, strict = true, tags = {"#yourTag"})
public class RunTestNGTest extends AbstractTestNGCucumberTests {
}
If anybone runs into the same issue, I managed to get around the issue by creating another class with a main method and calling the mvn command to run the test from there. something like this:
String mvnCommand = "mvn test ";
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", mvnCommand + args);
processBuilder.start();
Not sure if there's a better solution, but I can't seem to find any other option with Cucumber for Java plugin.
I finished my two projects, one is a real one, and the other simulates a server that just generates some data, and now i made a junit test suite to test it both and all works.
Now is the time to send the real project together with the tests to a friend that made the real server project, that generates real data.
So I need now to prepare the tests somehow so he can just run them on Command Line, but I have no idea how to do that.
I searched a little on the net, and there was no way to run a test suite from the command line...and I hope you can help me.
Here is what I have in the test project:
The class AllTests is a test suite, from witch I can run all the tests listed in it:
package test.dss;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
#RunWith(Suite.class)
#SuiteClasses({
TestAddAndDeleteScript.class,
TestEnumerateScripts.class,
TestEnumerateTemplates.class,
TestExecute.class,
TestGetAllImportsList.class,
TestGetBindings.class,
TestGetSource.class,
TestGetTemplate.class,
TestLogin.class,
TestLogout.class,
TestOpenAndCloseFile.class,
TestReloadLastLog.class,
TestSetDates.class,
TestSetPatientList.class,
TestStartStopTimer.class,
TestUpdateBindingParameters.class,
TestUpdateScript.class,
TestUpdateSharedUserList.class,
TestUpdateTextArea.class
})
public class AllTests {
}
Then there are the tests that are for the other project:
With a similar code in the AllTests class:
package test.mw;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
#RunWith(Suite.class)
#SuiteClasses({
TestEnumerateParams.class,
TestEnumeratePatients.class, // za properties, pogledati red 55
TestEnumerateScripts.class,
TestGetAll.class,
TestGetResults.class,
TestLogin.class,
TestLogout.class,
TestRaiseAlarm.class,
TestRegisterUnregisterScript.class,
TestSaveResult.class
})
public class AllTests {
}
Now, I removed the tests from the workspace, and placed it on the desktop and have to figure out how to test it in command line, while my two projects run in elipse on localhost.
On top, I wanna create a BAT file witch my friend can just run (from the command line) and test his part (the mw part).
Yeah, I forgot to mention, all the jars I need to run the tests are in the JRE System Library, JUnit 4 and referenced Libraries that are from the lib folder.
So, I created a CMD Tests folder on the desktop, and copied all the files from the tests, and it looks like this:
So here are the two parts of my question:
a) How to test them all at once with the two test suites?
a.b) If there is no way, how to test each class individually?
b) How to make a bat file to run it all at once?
Please help.
There is no need to create two test suites if you want to test them together. Just place all of the test classes into single Suite.
But running tests from two suites will look somehow like this:
java -cp "lib/*;bin" org.junit.runner.JUnitCore test.dss.AllTests test.mv.AllTests
Just place a .bat file in the root of your project with this content and you're ready to go.
You can also use the same command for running a particular test, means something like:
java -cp "lib/*;bin" org.junit.runner.JUnitCore test.dss.TestExecute
Note: I don't know how you compile your classes. I guess all of them are in bin folder. If they are not just add the appropriate folder to the -cp section.
Can some one tell me how can i run the java file which was exported from selenium IDE through command prompt.
I have used the following command: "java -jar selenium-server.jar -htmlSuite "*firefox" "http://www.google.com" "C:\mytestsuite\mytestsuite.html" "C:\mytestsuite\results.html"
Able to launch selenium functional test runner but nothing is executed there.
The converted tests are JUnit tests. So you should have two processes:
Your selenium server process:
java -jar lib/selenium-server-standalone-2.28.0.jar
Your JUnit test runner
"java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name] "
If you have several test classes, it might be better to create a TestSuite with the Suite annotation:
#RunWith(Suite.class)
#SuiteClasses({
MyTestClass1.class,
MyTestClass2.class})
public class TestSuite {
...
If you are using Spring, you can setup config containing selenium server address, browser, ...
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:my/package/seleniumConfig.xml"})
public abstract class SeleniumTestSuite {
The java test cases exported don't compile because they require at least the Selenium library to compile and may need junit or TestNG as well to actually run. I really suggest that you do this from within eclipse.
You can get eclipse 32 or 64 bit here. Then create a new Java project by going File-> New -> Java Project. You can get the Selenium client zip here. You need to get the client jar (called selenium-java-2.31.0.jar) out of this zip and put it in the lib directory of your new Java project in eclipse. You may have to create the lib directory and then right-click the jar file in the lib directory and "add to build path".
Put the java code that was generated by the Selenium IDE into the src directory of the new Java project in eclipse. (you may need to create the appropriate packages under src etc). Then right-click the test case java file that you want and select "Run As ... Junit". This should run it for you. If you get compile or run errors you can update your question above.
This is the solution:
Test is your Class mande by selenium
import org.junit.runner.JUnitCore;
import com.example.tests;
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Test.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
I'm having problems running methods as a jUnit in Eclipse. I know how to do so in Eclipse on my Mac and now I'm trying to get it to work on Ubuntu 12.04.
Normally, if you have a method like...
#Test
public void foo(){}
...you can right-click the foo method and one of the options is "Run as jUnit Test". Since I started attempting to use jUnit test in Eclipse on my Ubuntu machine, I see this option when I define a method in a brand new class. For example, I created a brand new class with the method test1:
package all;
import org.junit.Test;
public class BabyClass {
#Test
public void test1(){}
}
Right-clicking on test1 gives me the "Run as jUnit Test" option and the jUnit test works as expected.
However, if I go into one of my older classes (from before I ever tried using jUnit) and try to run a method as a jUnit test by right-clicking on its name, then the run as jUnit test option isn't there, which is puzzling. The "old" and "new" classes are all in the same package, in case that is relevant to know.
Does anyone have any ideas why the option to run as a jUnit test is not appearing for methods in my older classes? Is there something I'm supposed to do to make the option appear? This seems kind of like a bug in Eclipse to me.
Thanks.
You can't do jUnit tests with classes that involve generics.
package all;
import org.junit.Test;
public class BabyClass<T> {
#Test
public void test1(){
}
}
The baby class is now parameterized, but you can't do a jUnit test with it any more.
I want to be able to run all tests in a project programmatically. I know Eclipse has a "Run as JUnit test" configuration which somehow grabs all the tests in a project and run them. Is there any way for me to also grab the list of tests programmatically and run them? Or is there some good way to construct a test suite containing all the test cases without manually listing out every one (all 700+) of them?
I've tried the "New... -> Test Suite" option in Eclipse, but that seems to work only for JUnit 3, identifying tests by their extending from TestCase
The test classes are JUnit 4, so their only distinguishing characteristic is the annotation, no naming convention, no subclassing from TestCase.
Thanks in advance!
Though it does not really solve your immediate problem, I find it a very useful general practice to create suites and suites of suites, e.g. for a package something like PackageFooSuite etc. and assemble these suites in one or more suites again, like ModuleFooSuite and have one top-level suite, like AllTestsSuite. That way it's easy to run both all tests in one step as well as submodule tests for the package I'm currently working on (and have the tests run quicker than if I would always run all of them):
#RunWith(Suite.class)
#Suite.SuiteClasses({ PackageFooSuite.class, PackageBarSuite.class} )
public final class AllTestsSuite {} // or ModuleFooSuite, and that in AllTests
None of the other answers did it for me. I had 40k tests I needed to run, so manually listing every class was not an option.
I did it with ClasspathSuite. A test suite that runs all Junit4 and Junit3 test cases in the class path is as follows:
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.*;
import org.junit.runner.RunWith;
import org.junit.runner.JUnitCore;
import static org.junit.extensions.cpsuite.SuiteType.*;
#RunWith(ClasspathSuite.class)
#SuiteTypes({ JUNIT38_TEST_CLASSES, TEST_CLASSES })
public class RunAllSuite {
/* main method not needed, but I use it to run the tests */
public static void main(String args[]) {
JUnitCore.runClasses(RunAllSuite.class);
}
}
I needed to run it from command line, so this is what I did:
Downloaded cp-1.2.6.jar
Create the previously mentioned RunAllSuite
Compile the class, javac RunAllSuite.java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar
run it with target tests in the class path, java -cp cpsuite-1.2.6.jar;junit-4.8.1.jar;path/to/runallsuite/folder;target/classes;target/test-classes RunAllSuite
And that's it. With the RunAllSuite above, anywhere in your code you can just do JUnitCore.runClasses(RunAllSuite.class), which runs all tests in class path. There are other config options as well which are explained in the ClasspathSuite home page.
Note also that the class given above does not print anything. If that is needed, you can do
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.*;
import org.junit.runner.RunWith;
import org.junit.runner.JUnitCore;
import org.junit.internal.TextListener;
import static org.junit.extensions.cpsuite.SuiteType.*;
#RunWith(ClasspathSuite.class)
#SuiteTypes({ JUNIT38_TEST_CLASSES, TEST_CLASSES })
public class RunAllSuite {
public static void main(String args[]) {
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(RunAllSuite.class);
}
}
You can do this fairly easily from within maven using the surefire plugin: I usually clean/compile/install my projects from the command line before comparing them for eclipse usage (mvn eclipse:clean eclipse:eclipse) and you can define a test suite in your pom which lists all the tests you want to run en masse every time you run mvn install. You're not calling them programatically, exactly, but you can certainly call them en masse.
In Eclipse (I'm using 4.6.1) - Right click the project folder, select "Run As", choose "JUnit Test"
It will run all tests in that project. Same for a package.
Of the top of my head using Spring:
Implement a TypeFilter that matches classes with methods annotated with #Test (don't forget to consider the superclasses)
Invoke classpath scanning on your top-most test package
Invoke the JUnitRunner with the scan results
More info on classpath scanning and custom type filters here
With Eclipse Indigo (possibly Helios as well) in the Run Configurations dialog box, you now have the ability to Run all tests in a selected project, package or source folder.
Also a good reference from Eclipse is the article Java Unit testing with JUnit 4.x in Eclipse.
I also recommend using the JUnit Suite annotations. Follow the link for more detail.