Running a single JUnit test in Eclipse - eclipse

If I have a test suite with multiple tests, when I try to run a single unit test, either from the context menu of the code editor, or from the JUnit view, it seems to insist on always running the entire suite, rather than the single test. Is there a way to disable to change this behavior so that I can ask to to run that, and only that, test.

In the package explorer unfold the class. It should show you all methods. Right click on the one method you want to run, then select Run As -> JUnit from the context menu (just tested with Eclipse 3.4.1). Also selecting "Run" on a single entry in the JUnit-results view to re-run a test works in the same way.

Fastest way I know of:
Press Ctrl+Shift+↑ (moves cursor to current method declaration),
press Alt+Shift+x (or d for debug) then press t (hotkey for "Run JUnit Test"),
check test result,
press Alt+← to get back to the line of code you were before.
If you want to run all tests, you can skip steps 1 & 4.

In Eclipse 3.5, you can get around this by changing the test runner to JUnit 3. This will only work for JUnit 3 tests, not JUnit 4 tests. You can change the test runner by doing the following:
Right click on one of the test methods in the Outline explorer
Select Run As -> Run Configurations
On the 'Test' tab, select 'Run a single test'
In the Test Runner dropdown, select 'JUnit 3'
It may work in other versions as well.

This is possible in Eclipse Indigo with JUnit 4.8.2. You right click the method you want to unit test individually in the Outline pane, and select Run As -> JUnit Test.
You can even selectively right click a Test method name in the source editor and select Run As -> Junit Test.

Don't use Strg+F11 but the hotkey Alt+Shift+X -> T.
Then Eclipse will execute the current open file as a JUnit test. The green play button will only run the last chosen Run Configuration.
For me, it works well with Alt+Shift+X -> T.

I'll add to the others by including a highly productive keyboard only way that allows you to debug a single unit test (method).
Move your cursor to the method name by using either
Ctrl+Shift+Up or
Ctrl+Shift+Down or
Ctrl+o then type the name of the method
Once your cursor is on the method name (or right before it):
Alt+Shift+D -> T (Debug)
Alt+Shift+X -> T (Run)
After you run the test you can go back to where your cursor was by doing:
Alt+Back
You almost get REPL like behavior by:
Ctrl+Shift+Up and Alt+Shift+X -> T and Alt+Back
You can also quickly set a breakpoint:
Ctrl+Shift+B

Right click somewhere on the test method name in the file and select "Run" -> "Junit Test". I do it all the time with Kepler and JUnit 4.

To run only one test method.
In the (package explorer or project explorer) unfold the class. It should show you all methods. Right click on the one method you want to run, then select Run As -> JUnit from the context menu.

I had the same problem others have had using Eclipse 3.4.1 and JUnit 4 test runner -- couldn't run single test. But I found a suggestion somewhere else that resolved the problem. My test class was still extending junit.framework.TestCase. When I stopped extending TestCase, everything worked fine and I was able to run single JUnit tests with the JUnit 4 test runner. Of course, I needed to JUnit 4 annotations use static imports for the assert methods, but I had already done that -- I had just not removed the TestCase inheritance.

If you have many tests in more than one file, and you want to run only the tests in a particular file, you could right click that file, and select run as -> junit test.
Now, if you want to run only one test, contained in a file with many tests, my guess is (I dont have eclipse installed here) that the Outline view will list all test methods for a file, and you will probably be able to right click a single test method and execute it as a JUNit test.
Hope it helps.

For me, it also does not work in Eclipse 3.4.2 , although it worked in the previous releases (I have tried running single method from package explorer and single junit test from junit view)

I agree with the point about 3.4.2 no longer working the way it did in previous versions. Before you could right-click on a single test in a group of tests from the JUnit view and execute just that test. In the latest version it insists on always running all of them. Granted, the view does run the test you clicked on first and dumps the rest in a rollup called "Unrooted Tests". If anyone figures this out I'd like to know how to get around it.

Reading some of the comments here, it seems you might be interested in running the tests for the code you change as you change it, without losing focus on the code you are working on. There's an eclipse plugin for doing just that. See infinitest.

If by test you might even mean single test method I like to use "Run Tests of Selected Member" using a key binding.
When I am inside of a test method only that test method will be run – if I have the cursor in the class scope it will run the whole class. I changed the default binding to something easy like Ctrl+r since I use it a lot.
Please see:
(If you don't see that keys entry it might come from the MoreUnit plugin – which is great anyway and you might want to have it)

Related

Eclipse run test one by one

I'm looking for a way to run a only a one test case in my test class from the some kind of test methods list.
For example, when I run a whole batch of test methods, the failure list appears like that:
And I can right-click on the appropriate test method and re-run it without a running a whole batch. But in the next i'm getting the error list, that consists of only that method:
At that point it would be nice to run the another test method as only one, without running a whole batch. Is there a way in Eclipse to get the list of my test methods where it's would be possible to choose the method to run?
In the JUnit view toolbar there is a Test Run History... button/drop-down where you can go back to the test result of a previous run without rerunning it.
In addition, you can also right-click on a test method to run this method only: for example in the Java editor, in Outline view of the Java editor, in the Package Explorer or in the Project Explorer.

How to run specific tests from one class xcode uitesting

I'm creating ui tests in xcode using XCTest(Swift)
How can I run, for example, 2-3 tests from one class without launching the rest of the test from this class?
Is it possible to add some tests to suite or run and run from command line just necessary suite/run?
According to the XCTestSuite documentation there is an option to create a custom test suite, but I haven't found any examples of it.
You can adjust which tests you want to run in the scheme, under the Test menu. There will be a list of your test classes and tests in a disclosure triangle hierarchy in the Edit Scheme > Test window, where you can select or deselect tests. When you run the tests with that scheme selected, only the tests you selected will run.
You can use multiple -test-only params in your run
xcodebuild test [...] -only-testing:"superApp/SuperTestClass/test2" -only-testing:"superApp/SuperTestClass/test5"

Eclipse Run As Groovy Script by Default

I'd like to run my Groovy project in Eclipse using a shortcut (CTRL+F11). The trouble is, every time I do so I get a "Select a way to run " dialog, which gives me the choice between Groovy Console, Groovy Script, Java Application.
I've already set a Run Configuration to use Groovy Script, and if I click that Run Configuration under the Run As menu, it just runs.
How can I get my project to execute from a keyboard shortcut without that dialog popping up? I can't imagine ever selecting one of the other options.
I've never been able to get a satisfactory keyboard shortcut for Groovy script runs out of ctrl-F11. Instead I use alt-shift-X and then press G when the menu appears.
For debugging I use alt-shift-D, then G.
Both are two-stroke, rather than one-stroke, solutions, but neither one forces me to use the mouse or the arrow keys.
Under Project>Properties>Java Build Path and then click on Java SE, what ever it may be, click finish.
On the off chance that you deleted your java system library you can o pick up a new one... http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

How can I step into a core java class method in Eclipse?

Hi I am going to dig a little more in Java so I would like to see how the program executed in the core class. For example, I would like to know how String.chatAt() is implemented, so I set breakpoint and tried to step into with debug mode. But I failed, I set bp at the second line when the program hit it, I used step into it still continue to the third line.
String a = "1231231241241";
char b = a.charAt(0);
System.out.println(b);
I think it should go into the source and show "no source found" and then give me a chance to attach the source file, right? But why it cannot get in? I can only use ctrl+right click on a method to get into source and attach.
It might be that your Eclipse is not setup to run with JDK, it might run with JRE instead.
Check this link.
Also, I might suggest you to try and use Maven for your Java projects management, it makes the life so much easier, when you get a grasp of it.
Most likely, you have the "Step filtering" functionality activated.
When debugging, it can sometimes be a bit annoying when the debugger steps into trivial classes whose internals you were not really interested in. Because of this, you can configure "Step filters". Classes or packages that are added as step filters will be ignored by the "Step into" operation.
If you want to configure which classes/packages should be included in the step filter, you can go to: Window --> Preferences --> Java --> Debug --> Step Filtering
My guess is that you have a java.* package filter defined there, meaning that the debugger will never step into any class within a package which starts with "java". Uncheck this package filter if you want to be able to step into java.lang.String
Alternatively, if you want to activate/deactivate step filtering completely, you can do this by clicking the Use step filtering button in the debug view, as shown below:
To be able to look at the code of libriaries you are using in your project you should configure your build path by attaching the soruces and JavaDoc.
Source Lookup
Seems like you dont have the "Source Attachment" set. Go to Preferences -> Java -> Installed JREs. Select the JRE you use and click "Edit", then set the Source Attachment to the src.zip that comes with the jdk by selecting the jars and clicking "Source Attachment" :).
You can do this with all of the jars at the same time by selecting them via shift-click.
for example in this System.out.println("test") piece of code if you want to step into System class just click on System and click F3 you will be redirected to attach the source code if you haven't already attached.Just attached the src folder of the jdk

PerlUnit in Eclipse

If we click on Java perspective in Eclipse and right click on a Java project, we see “new” a JUnit file.On executing the test cases in JUnit we see red/green bar displaying failure/success of test cases.
I am trying to find a plugin that would add these two features :
In the perl perspective (provided by EPIC) , right-click on a Perl
project should list a new “t” file.
On executing perl unit the status of test cases should be displayed
using Reed/Green bar.
If there is no plugin is there a way we can do the same.
As far as I know, there is no EPIC plugin for this or even a more generic TAP-based test runner for Eclipse. Such a thing would be nice to have.
Rigging some sort of report on test success from a web page is pretty common. This can be done using a tool like Test::Harness which can be used to run your test or tests and return or output a report of how well your test run went.
From there, it's a matter of turning that into a red/green progress bar.
If you want to see the progress updated continuously during the test run, you'll have to run and parse the test files more directly as they run. All Perl tests output TAP format, which is a standard format with specs available here:
http://testanything.org