Showing JUnit view in Eclipse when running tests f

2019-05-11 20:17发布

问题:

When I run just my Testclass in Eclipse I get the JUnit view showing the tree structure and if the test was successful. If I start my Test from code:

JUnitCore core = new JUnitCore();
core.run(SimpleTests.class);

the view does not show. Can I change this?

回答1:

On your toolbar click Windows-->Show View-->Others. Type "Junit" without quotes. Select from list and click OK.



回答2:

I suppose you run your code in the main method like this example :

public class MiniTest extends TestCase
{
@Test
public void test()
{
    System.out.println("Running test");
}

public static void main(String[] args)
{
    JUnitCore core = new JUnitCore();
    core.run(MiniTest.class);
}

In this case, it will not show your view because you launch your program as a java application (alt-shift-X-J). You will see "Running test" in the console but that is.
If you want to see the Junit View you must launch your program as a Junit test (alt-shit-X-T).
Note that with this manner the main() is not necessary, Eclipse will directly launch the method tagged with @Test. You can also group tests with @Suite.



回答3:

You can't interact with the JUnit view from your code using JUnitCore.

You can however, add your tests to the JUnitView if you use a @Parameterized test, or if you implement your own test runner. Try extending the Suite class and reimplement one of the constructors with the tests that you want to execute.



回答4:

I get this problem from time to time.

There is something corrupted in the eclipse workspace.

The only solution for me is to create a new workspace and bring in the projects.

It's a serious pain when there are fifty projects in the workspace.