Java 如何从 NetBeans 运行 JUnit?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1381512/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:24:39  来源:igfitidea点击:

How do I run JUnit from NetBeans?

javaunit-testingidenetbeansjunit

提问by Eric Wilson

I've been trying to understand how to start writing and running JUnit tests.

我一直在尝试了解如何开始编写和运行 JUnit 测试。

When I'm reading this article:

当我阅读这篇文章时:

http://junit.sourceforge.net/doc/testinfected/testing.htm

http://junit.sourceforge.net/doc/testinfected/testing.htm

I get the the middle of the page and they write, "JUnit comes with a graphical interface to run tests. Type the name of your test class in the field at the top of the window. Press the Run button."

我看到页面的中间,他们写道,“JUnit 带有一个图形界面来运行测试。在窗口顶部的字段中输入测试类的名称。按下运行按钮。”

I don't know how to launch this program. I don't even know which package it is in, or how you run a library class from an IDE.

我不知道如何启动这个程序。我什至不知道它在哪个包中,也不知道如何从 IDE 运行库类。

Being stuck, I tried this NetBeans tutorial:

被卡住了,我尝试了这个 NetBeans 教程:

http://www.netbeans.org/kb/docs/java/junit-intro.html

http://www.netbeans.org/kb/docs/java/junit-intro.html

It seemed to be going OK, but then I noticed that the menu options for this tutorial for testing a Java Class Library are different from those for a regular Java application, or for a Java Web App. So the instructions in this tutorial don't apply generally.

看起来一切顺利,但后来我注意到本教程用于测试 Java 类库的菜单选项与常规 Java 应用程序或 Java Web 应用程序的菜单选项不同。所以本教程中的说明并不普遍适用。

I'm using NetBeans 6.7, and I've imported JUnit 4.5 into the libraries folder. What would be the normal way to run JUnit, after having written the tests?

我使用的是 NetBeans 6.7,并且已将 JUnit 4.5 导入到库文件夹中。编写测试后,运行 JUnit 的正常方法是什么?

The JUnit FAQ describes the process from the Console, and I'm willing to do that if that is what is typical, but given all that I can do inside netbeans, it seems hard to believe that there isn't an easier way.

JUnit FAQ 描述了来自控制台的过程,如果这是典型的,我愿意这样做,但是考虑到我在 netbeans 中可以做的所有事情,似乎很难相信没有更简单的方法。

Thanks much.

非常感谢。

EDIT:If I right-click on the project and select "Test" the output is:

编辑:如果我右键单击该项目并选择“测试”,则输出为:

init:
deps-jar:
compile:
compile-test:
test-report:
test:
BUILD SUCCESSFUL (total time: 0 seconds)

This doesn't strike me as the desired output of a test, especially since this doesn't change whether the test condition is true or not.

这并没有让我觉得测试的期望输出,特别是因为这不会改变测试条件是否为真。

Any ideas?

有任何想法吗?

采纳答案by NA.

One way is to right click on your project in the Projects pane and select "Tests". That will run the JUnit tests. You can also right click on the test file and select "Run Test" and that single file will be ran. The keyboard shortcuts depends on how you have your keymapping set, but you'll see them in the context menus.

一种方法是在“项目”窗格中右键单击您的项目,然后选择“测试”。这将运行 JUnit 测试。您也可以右键单击测试文件并选择“运行测试”,然后将运行该单个文件。键盘快捷键取决于您如何设置键盘映射,但您会在上下文菜单中看到它们。

You can also have NetBeans autogenerate tests for you by right clicking your source file and then "Tools > Create JUnit Tests".

您还可以通过右键单击源文件然后“工具 > 创建 JUnit 测试”让 NetBeans 为您自动生成测试。

回答by Eric Wilson

Even though I've accepted an answer, I thought I should mention my difficulty, as someone else may encounter it.

尽管我已经接受了一个答案,但我认为我应该提到我的困难,因为其他人可能会遇到它。

When importing a project from existing sources into NetBeans, if you do not specify a folder for test packages, then NetBeans will not offer the JUnit options on the tools menu.

将项目从现有源导入 NetBeans 时,如果您没有为测试包指定文件夹,则 NetBeans 将不会在工具菜单上提供 JUnit 选项。

The only solution I found was to re-import the project. While primitive, it worked.

我找到的唯一解决方案是重新导入项目。虽然原始,但它起作用了。

回答by alangalloway

Re-importing does not appear to be necessary. I had the same issue (imported project, right clicking did not bring up any JUnit test options). I took these steps, which resolved it, using NetBeans 6.8:

重新导入似乎没有必要。我有同样的问题(导入的项目,右键单击没有出现任何 JUnit 测试选项)。我采取了以下步骤,使用 NetBeans 6.8 解决了这个问题:

  1. Add a folder called "tests" to your project.
  2. Right-click your project and select Properties.
  3. Select Sources.
  4. Under Test Package Folders, click the Add Folder button, and select the "tests" folder.
  5. Right clicking a file + Tools > Create JUnit Tests.
  6. Once a test is created, right-clicking a file + Test File runs the test.
  1. 将一个名为“tests”的文件夹添加到您的项目中。
  2. 右键单击您的项目并选择属性。
  3. 选择来源。
  4. 在 Test Package Folders 下,单击 Add Folder 按钮,然后选择“tests”文件夹。
  5. 右键单击文件 + 工具 > 创建 JUnit 测试。
  6. 创建测试后,右键单击文件 + 测试文件运行测试。

回答by Wei

I had the same issue after imported a eclipse project into NetBeans.

将 Eclipse 项目导入 NetBeans 后,我遇到了同样的问题。

To resolve it, I followed the above steps outlined by alangalloway, but instead of creating a new folder, I just pointed to the imported test folder.

为了解决这个问题,我按照 alangalloway 概述的上述步骤进行了操作,但我没有创建新文件夹,而是指向导入的测试文件夹。

Maybe in future release, NetBeans can automatically recognize imported test cases.

也许在未来的版本中,NetBeans 可以自动识别导入的测试用例。

Thanks.

谢谢。

回答by j-a

Had a similar issue. In Netbeans 7.0.1, what worked for me was to locate the project.xml file (i.e. {project}/nbproject/project.xml) and change:

有一个类似的问题。在 Netbeans 7.0.1 中,对我有用的是找到 project.xml 文件(即 {project}/nbproject/project.xml)并更改:

        <test-roots/>

to:

到:

        <test-roots>
            <root id="src.dir"/>
        </test-roots>

(in my case the test files are in the same dir as the source dir)

(在我的情况下,测试文件与源目录位于同一目录中)

回答by Nirmal Dhara

All the above answers are correct, but if you are using in mac little change needed.

以上所有答案都是正确的,但如果您在 mac 中使用,则几乎不需要更改。

Step 1Write your junit class.

步骤 1编写您的 junit 类。

Step 2Right click on the class ->Tools-> Create/Updates Tests-> Select framework as Junit-> click ok.

第2步右键单击类->工具->创建/更新测试->选择框架为Junit->单击确定。

Step 3Right click on the file again ->Test File.

步骤 3再次右键单击该文件 -> 测试文件。

Now it will run as junit, will show the Test Result.

现在它将作为 junit 运行,将显示测试结果。