从代码运行测试时在 Eclipse 中显示 JUnit 视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16230876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing JUnit view in Eclipse when running tests from Code
提问by Hans En
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:
当我在 Eclipse 中只运行我的 Testclass 时,我会得到 JUnit 视图,显示树结构以及测试是否成功。如果我从代码开始我的测试:
JUnitCore core = new JUnitCore();
core.run(SimpleTests.class);
the view does not show. Can I change this?
视图没有显示。我可以改变这个吗?
回答by Anurag Tripathi
On your toolbar click Windows-->Show View-->Others. Type "Junit" without quotes. Select from list and click OK.
在您的工具栏上单击 Windows--> 显示视图--> 其他。键入不带引号的“Junit”。从列表中选择并单击确定。
回答by mki
I suppose you run your code in the main method like this example :
我想你像这个例子一样在 main 方法中运行你的代码:
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 viewbecause 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.
在这种情况下,它不会显示您的视图,因为您将程序作为 Java 应用程序 (alt-shift-XJ) 启动。您将在控制台中看到“运行测试”,但就是这样。
如果您想查看 Junit 视图,您必须将您的程序作为 Junit 测试(alt-shit-XT) 启动。
请注意,通过这种方式 main() 不是必需的,Eclipse 将直接启动带有 @Test 标记的方法。您还可以使用@Suite 对测试进行分组。
回答by Matthew Farwell
You can't interact with the JUnit view from your code using JUnitCore.
您无法使用 JUnitCore 从您的代码中与 JUnit 视图进行交互。
You can however, add your tests to the JUnitView if you use a @Parameterizedtest, or if you implement your own test runner. Try extending the Suiteclass and reimplement one of the constructors with the tests that you want to execute.
但是,如果您使用@Parameterized测试或实现自己的测试运行程序,则可以将测试添加到 JUnitView 。尝试扩展Suite类并使用您要执行的测试重新实现其中一个构造函数。
回答by user939857
I get this problem from time to time.
我不时遇到这个问题。
There is something corrupted in the eclipse workspace.
eclipse 工作区中存在损坏的内容。
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.
当工作区中有五十个项目时,这是一种严重的痛苦。
回答by bigcitylife
If the view doesn't show, go to Window -> Perspective -> Reset Perspective..
如果视图未显示,请转到窗口 -> 透视图 -> 重置透视图。