寻找有关将 JUnit 与 Intellij IDEA 9.x 结合使用的教程
我需要一份关于一起使用 JUnit 和 Intellij IDEA 9.x 的绝对初学者指南。我在 WinXP 上运行 JDK 1.6.0_22。我正在寻找以下问题的答案:
- 我需要安装 JUnit 还是它已经集成到 Intellij 中?如果需要怎么设置?
- 假设我有要测试的类的接口和实现,如何设置 JUnit 项目?
- 有没有办法根据类接口自动生成测试框架?
我拥有使用 PHPUnit 和 Boost.Test 等其他单元测试框架的经验,因此我主要关心在 Intellij 中设置和运行所有这些框架的机制。
编辑
我是一个使用 Intellij 的新手。
我有命令行背景,即在 Linux 上使用 vim 和手写 make 文件的 C++ 开发人员。
我已经成功地通过命令行编译并运行了一些 JUnit 测试(下载了 JUnit 4.8.2 并使用了 -cp 开关),但是在 Intellij 下进行任何设置都非常困难。我尝试查看 在线 Intellij 文档,但还没有找到这些非常有用。我查看了 Intellij 的 lib 目录,它包含 Junit-4.7.jar。
我确实需要某种快速入门指南,其中包含从初始项目创建到成功运行第一个单元测试的逐步说明。
I need an absolute beginners guide to using JUnit and Intellij IDEA 9.x together. I'm running JDK 1.6.0_22 on WinXP. I'm looking for answers to the following questions:
- Do I need to install JUnit or is it already integrated into Intellij? If I need to set it up how?
- Assuming I have the interface and impl for a class I want to test, how do I set up a JUnit project?
- Is there a way to autogen a test skeleton based on the class interface?
I've got experience with other Unit testing frameworks like, PHPUnit and Boost.Test, so I'm primarily concerned with the mechanics of getting all this set up and running in Intellij.
Edit
I'm a complete newb using Intellij.
I'm coming from a command-line background, i.e. C++ dev using vim and handwritten make files on Linux.
I've managed to compile and run some JUnit tests via command line ( downloaded JUnit 4.8.2 and used the -cp swith), but I'm having a heck of a time getting anything set up under Intellij. I tried looking at the online Intellij docs, but haven't found those to be very useful. I looked in Intellij's lib directory and it includes Junit-4.7.jar.
I really need some kind of quick start guide with step by step instructions from initial project creation through successfully running the first unit test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
lib/
)。@org.junit.Test
注释您的测试方法,按照 这个快速教程,IDEA 不需要任何进一步的内容。对于至少在其一个方法上具有此注释的任何类,IDEA 将为您提供将该类作为 JUnit 测试用例执行的选项。 (注意:这可能仅适用于项目结构中标记为“测试源”的目录中的文件,但我尚未对此进行测试。最好设置您的项目无论如何,就像这样。)lib/
).@org.junit.Test
, as per this quick tutorial, IDEA does not require anything further. For any class with this annotation on at least one of its methods, IDEA will give you the option to execute that class as a JUnit test case. (Note: this may only be the case for files in a directory that's marked as "Test Sources" in the project structure, but I haven't tested this. It's a good idea to set your project up like this anyway.)对于名为 Foo 的类:
编写如下 JUnit 测试:
我在 IntelliJ 项目中创建 /src 和 /test 文件夹,并镜像每个文件夹下的包结构。
您可以通过右键单击 /test 文件夹并选择“运行所有测试”来让 IntelliJ 运行项目中的所有测试。
如果您编辑“所有测试”任务的配置,您将看到一个复选框,要求 IntelliJ 为您检查代码覆盖率。每次测试运行后它都会刷新这些值。
For a class named Foo:
write a JUnit test like this:
I create /src and /test folders in my IntelliJ project and mirror the package structure under each one.
You can have IntelliJ run all the tests in your project by right clicking on the /test folder and selecting "Run all tests".
If you edit configurations on your "all tests" task, you'll see a checkbox to ask IntelliJ to check code coverage for you. It'll refresh the values after every test run.
我想你需要下载 junit 库 jar 并将其添加到项目的模块设置中,你可以按 Shift+Ctrl+Alt+S
在模块设置本身中,您需要选择测试文件夹。选择测试文件夹并单击顶部的测试源按钮
要为相应的类创建测试,请按 Shift+Ctrl+T。
我认为您可能不需要任何实时模板来创建它。按 Ctrl+Alt+S 并转到实时模板并创建一个查看其他模板的示例。
I guess you need to download junit library jar and add it to module settings of your project which you can get pressing Shift+Ctrl+Alt+S
In the module settings itself you need to select your test folders. Select the test folders and click on the test sources button on the top
To create a test for a corresponding class press Shift+Ctrl+T.
I dont think there are any live templates you may need to create it. Press Ctrl+Alt+S and go to live templates and create one looking into other templates for examples.