寻找有关将 JUnit 与 Intellij IDEA 9.x 结合使用的教程

发布于 2024-10-02 01:54:09 字数 766 浏览 4 评论 0原文

我需要一份关于一起使用 JUnit 和 Intellij IDEA 9.x 的绝对初学者指南。我在 WinXP 上运行 JDK 1.6.0_22。我正在寻找以下问题的答案:

  1. 我需要安装 JUnit 还是它已经集成到 Intellij 中?如果需要怎么设置?
  2. 假设我有要测试的类的接口和实现,如何设置 JUnit 项目?
  3. 有没有办法根据类接口自动生成测试框架?

我拥有使用 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:

  1. Do I need to install JUnit or is it already integrated into Intellij? If I need to set it up how?
  2. Assuming I have the interface and impl for a class I want to test, how do I set up a JUnit project?
  3. 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 技术交流群。

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

发布评论

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

评论(3

十级心震 2024-10-09 01:54:09
  1. IDEA 附带 JUnit 支持,但请记住,您需要在项目上调用测试 - 因此您的项目需要在其类路径上包含 JUnit。因此,您不需要“安装”JUnit,但您需要使其 JAR 可用于您的项目,就像使用任何其他第三方库一样(例如将其作为 Maven 依赖项引用或将 JAR 放入 lib/)。
  2. 这就是 IDEA 支持发挥作用的地方 - 我 99% 确定您不需要做任何特殊的事情。由于使用 JUnit 通常只涉及使用 @org.junit.Test 注释您的测试方法,按照 这个快速教程,IDEA 不需要任何进一步的内容。对于至少在其一个方法上具有此注释的任何类,IDEA 将为您提供将该类作为 JUnit 测试用例执行的选项。 (注意:这可能仅适用于项目结构中标记为“测试源”的目录中的文件,但我尚未对此进行测试。最好设置您的项目无论如何,就像这样。)
  3. 默认情况下,不作为 JUnit 支持的一部分。还有其他插件可以做到这一点,但在我个人看来,这并不像听起来那么有用。 IDEA确实具有集成的代码覆盖率(完整版),这(再次恕我直言)是确保您的测试用例覆盖类/接口的全部功能的更好方法。
  1. IDEA comes with JUnit support, but bear in mind that you need to invoke tests on your project - and hence your project will need to have JUnit on its classpath. Hence you don't need to "install" JUnit, but you'll need to make its JAR available to your project as you would with any other third party library (e.g. reference it as a Maven dependency or drop the JAR in lib/).
  2. This is where the IDEA support kicks in - I'm 99% sure you don't need to do anything special. Since using JUnit in general simply involves annotating your test methods with @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.)
  3. Not by default as part of the JUnit support. There exist other plugins to do this, but in my personal opinion this is not as useful as it might sound. IDEA does have integrated code coverage (with the full edition), which (again IMHO) is a better way to ensure that your tests cases cover the full functionality of your class/interface.
何时共饮酒 2024-10-09 01:54:09

对于名为 Foo 的类:

public class Foo
{
   public static int add(int x, int y) { return x+y; }
}

编写如下 JUnit 测试:

public class FooTest:
{
    @Test
    public void testAdd()
    {
        int expected = 5;
        int actual = Foo.add(2, 3);
        Assert.assertEquals(expected, actual);
    }
}

我在 IntelliJ 项目中创建 /src 和 /test 文件夹,并镜像每个文件夹下的包结构。

您可以通过右键单击 /test 文件夹并选择“运行所有测试”来让 IntelliJ 运行项目中的所有测试。

如果您编辑“所有测试”任务的配置,您将看到一个复选框,要求 IntelliJ 为您检查代码覆盖率。每次测试运行后它都会刷新这些值。

For a class named Foo:

public class Foo
{
   public static int add(int x, int y) { return x+y; }
}

write a JUnit test like this:

public class FooTest:
{
    @Test
    public void testAdd()
    {
        int expected = 5;
        int actual = Foo.add(2, 3);
        Assert.assertEquals(expected, actual);
    }
}

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.

梦冥 2024-10-09 01:54:09
  1. 我想你需要下载 junit 库 jar 并将其添加到项目的模块设置中,你可以按 Shift+Ctrl+Alt+S

  2. 在模块设置本身中,您需要选择测试文件夹。选择测试文件夹并单击顶部的测试源按钮
    要为相应的类创建测试,请按 Shift+Ctrl+T。

  3. 我认为您可能不需要任何实时模板来创建它。按 Ctrl+Alt+S 并转到实时模板并创建一个查看其他模板的示例。

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

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

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

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