如何从 NetBeans 运行 JUnit?

发布于 2024-08-03 20:21:02 字数 1032 浏览 6 评论 0原文

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

当我阅读这篇文章时:

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

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

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

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

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

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

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

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

非常感谢。

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

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

这并不让我觉得这是测试所需的输出,特别是因为这不会改变测试是否成功条件真或假。

有什么想法吗?

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

When I'm reading this article:

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."

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.

Being stuck, I tried this NetBeans tutorial:

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.

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?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

和影子一齐双人舞 2024-08-10 20:21:03

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

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

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.

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

傲娇萝莉攻 2024-08-10 20:21:03

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

  1. 将一个名为“tests”的文件夹添加到您的项目中。
  2. 右键单击您的项目并选择属性。
  3. 选择来源。
  4. 在“测试包文件夹”下,单击“添加文件夹”按钮,然后选择“测试”文件夹。
  5. 右键单击文件+工具>创建 JUnit 测试。
  6. 创建测试后,右键单击文件 + 测试文件即可运行测试。

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:

  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.
夏了南城 2024-08-10 20:21:03

以上所有答案都是正确的,但如果您在 mac 中使用,则需要进行一些更改。

第 1 步 编写您的 junit 类。

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

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

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

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

Step 1 Write your junit class.

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

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

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

紧拥背影 2024-08-10 20:21:03

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

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

我找到的唯一解决方案是重新导入项目。虽然很原始,但它确实有效。

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.

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

追风人 2024-08-10 20:21:03

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

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

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

谢谢。

I had the same issue after imported a eclipse project into 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.

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

Thanks.

氛圍 2024-08-10 20:21:03

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

        <test-roots/>

为:(

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

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

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:

        <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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文