如何从NetBeans运行JUnit ?

时间:2023-01-18 20:14:52

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导入到libraries文件夹中。在编写了测试之后,运行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:

编辑:如果我右键单击项目并选择“Test”输出是:

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?

什么好主意吗?

6 个解决方案

#1


12  

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.

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

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

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

#2


14  

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. 在项目中添加一个名为“测试”的文件夹。
  3. Right-click your project and select Properties.
  4. 右键单击项目并选择Properties。
  5. Select Sources.
  6. 选择来源。
  7. Under Test Package Folders, click the Add Folder button, and select the "tests" folder.
  8. 在“测试包”文件夹下,单击“添加文件夹”按钮,并选择“测试”文件夹。
  9. Right clicking a file + Tools > Create JUnit Tests.
  10. 右键单击文件+工具>创建JUnit测试。
  11. Once a test is created, right-clicking a file + Test File runs the test.
  12. 一旦创建了测试,右键单击一个文件+测试文件运行测试。

#3


5  

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.

谢谢。

#4


3  

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

以上所有的答案都是正确的,但是如果你在mac电脑上使用,那么你就需要改变。

Step 1 Write your junit class.

步骤1编写junit类。

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

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

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

第三步右键点击文件->测试文件。

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

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

#5


2  

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将不会在tools菜单上提供JUnit选项。

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

我找到的唯一解决办法是重新导入这个项目。虽然原始,但它工作。

#6


2  

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中,为我工作的是定位项目。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)

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

#1


12  

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.

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

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

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

#2


14  

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. 在项目中添加一个名为“测试”的文件夹。
  3. Right-click your project and select Properties.
  4. 右键单击项目并选择Properties。
  5. Select Sources.
  6. 选择来源。
  7. Under Test Package Folders, click the Add Folder button, and select the "tests" folder.
  8. 在“测试包”文件夹下,单击“添加文件夹”按钮,并选择“测试”文件夹。
  9. Right clicking a file + Tools > Create JUnit Tests.
  10. 右键单击文件+工具>创建JUnit测试。
  11. Once a test is created, right-clicking a file + Test File runs the test.
  12. 一旦创建了测试,右键单击一个文件+测试文件运行测试。

#3


5  

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.

谢谢。

#4


3  

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

以上所有的答案都是正确的,但是如果你在mac电脑上使用,那么你就需要改变。

Step 1 Write your junit class.

步骤1编写junit类。

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

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

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

第三步右键点击文件->测试文件。

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

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

#5


2  

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将不会在tools菜单上提供JUnit选项。

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

我找到的唯一解决办法是重新导入这个项目。虽然原始,但它工作。

#6


2  

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中,为我工作的是定位项目。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)

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