简体   繁体   English

如何从NetBeans运行JUnit?

[英]How do I run JUnit from NetBeans?

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. 它似乎没问题,但后来我注意到本教程中用于测试Java类库的菜单选项与常规Java应用程序或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. 我正在使用NetBeans 6.7,并且已将JUnit 4.5导入库文件夹。 What would be the normal way to run JUnit, after having written the tests? 在编写测试后,运行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描述了来自Console的过程,如果这是典型的,我愿意这样做,但考虑到我在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? 有任何想法吗?

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). 我有同样的问题(导入的项目,右键单击没有提出任何JUnit测试选项)。 I took these steps, which resolved it, using NetBeans 6.8: 我使用NetBeans 6.8采取了这些解决它的步骤:

  1. Add a folder called "tests" to your project. 将名为“tests”的文件夹添加到项目中。
  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. 在“测试包文件夹”下,单击“添加文件夹”按钮,然后选择“tests”文件夹。
  5. Right clicking a file + Tools > Create JUnit Tests. 右键单击文件+工具>创建JUnit测试。
  6. Once a test is created, right-clicking a file + Test File runs the test. 创建测试后,右键单击文件+测试文件将运行测试。

One way is to right click on your project in the Projects pane and select "Tests". 一种方法是在“项目”窗格中右键单击项目,然后选择“测试”。 That will run the JUnit tests. 那将运行JUnit测试。 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". 您还可以通过右键单击源文件然后“工具>创建JUnit测试”为您自己生成NetBeans自动生成测试。

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. 步骤3再次右键单击该文件 - >测试文件。

Now it will run as junit, will show the Test Result. 现在它将以junit运行,将显示测试结果。

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. 谢谢。

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. 原始的,它的工作原理。

Had a similar issue. 有一个类似的问题。 In Netbeans 7.0.1, what worked for me was to locate the project.xml file (ie {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) (在我的例子中,测试文件与源目录在同一个目录中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从NetBeans运行所有JUnit测试用例? - How to run all JUnit test cases from NetBeans? 如何在Gradle中运行JUnit测试 - How do I run JUnit tests in Gradle 如何在 Sublime Text 3 中运行 junit? - How do I run junit in Sublime Text 3? 如何从NetBeans从命令行创建的ant项目中运行单个junit测试? - How to run a single junit test from an ant project created by Netbeans from the command line? 当JUnit类有多个导入时,如何从Windows CMD窗口运行JUnit测试? - How do I run JUnit tests from a Windows CMD window when the JUnit class(es) have multiple imports? JUnit参数化测试:如何仅从IntelliJ / Eclipse运行1个特定测试? - JUnit parameterized tests: how do I run only 1 specific test from IntelliJ/Eclipse? 如何从我的java应用程序中运行JUnit测试? - How do I run JUnit tests from inside my java application? 如何从命令行运行增量编译的NetBeans应用程序? - How do I run an incrementally-compiled NetBeans application from the command line? 如何从Netbeans Web项目在Java ME SDK(Mac)模拟器中运行JAD / JAR - How do I run a JAD/JAR in the Java ME SDK (Mac) Emulator from a Netbeans Web Project 如何从NetBeans IDE外部运行.jar可执行Java文件? - How do I run a .jar executable java file from outside NetBeans IDE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM