如何将JUnit与Sublime Text 3结合使用 [英] How to use JUnit with Sublime Text 3

查看:107
本文介绍了如何将JUnit与Sublime Text 3结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个分为两个部分的问题.我正在尝试将JUnit用于Sublime Text 3中正在处理的项目.我的文件设置看起来像这样

This is a two part question. I'm trying to use JUnit for a project I'm working on in Sublime Text 3. The file setup I have looks somewhat like this

Vulcan/
 Vulcan.sublime-project
 bin/
  ...
 src/
  core/
   org/
    vulcan/
     geom/
      Vector.java
      ... (other similar files in the same package)
  tests/
   org/
    vulcan/
     geom/
      VectorTest.java
      ... (relevant test files to match core/org/vulcan/geom/)

Vector.javaVectorTest.java的顶部,我有行package org.vulcan.geom.现在,在我的sublime-project文件中,我已将源文件夹设置为Vulcan/src/core/,以便在构建它时,Vector.java被编译为bin/org/vulcan/geom/Vector.class,但这意味着在构建整个项目时, tests/文件夹将被忽略,并且不会内置到bin/.

At the top of Vector.java and VectorTest.java, I have the line package org.vulcan.geom. Now, in my sublime-project file, I have set the source folder to Vulcan/src/core/, so that when I build it, Vector.java is compiled to bin/org/vulcan/geom/Vector.class, but what this means is that upon building the whole project, the tests/ folder gets ignored and is not built to bin/.

我试图通过将源文件夹设置为Vulcan/src/来解决此问题,但这在org/vulcan/geom/中引起了很多错误,说它无法在该软件包中找到任何类名(我认为是针对此问题的解决方法将软件包从org.vulcan.geom更改为core.org.vulcan.geom,但这将是一个繁琐的过程).

I attempted to fix this by setting the source folder to Vulcan/src/, but that causes a bunch of errors in org/vulcan/geom/, saying that it cannot find any of the class names in that package (I assume the fix for this would be to change the package from org.vulcan.geom to core.org.vulcan.geom but that would be a tedious process).

问题1:那么在构建整个项目时,如何设置项目以使tests/core/一起编译?

Question 1: So how can I set up my project such that the tests/ get compiled along with the core/ when I build the whole project?

问题2:使用此文件结构,我将如何在tests/中使用JUnit运行测试(tests/中的每个.java文件都是)?我已经阅读了这些教程,发现可以从命令行运行它们(我不太确定该怎么做),或者可以创建一个类,其main方法可以运行所有测试.如果选择后一个选项,我将把跑步者"类放在哪里?我要把它放在tests/org/vulcan/geom/还是tests/org/vulcan/tests/org/中?以及如何从Sublime Text 3中运行它?

Question 2: With this file structure, how would I run the tests in tests/ with JUnit (each .java file in tests/ is a test class runnable by JUnit)? I've read the tutorials and I see that I can either run them from the command line (which I'm not quite sure how to do), or I can create a class whose main method runs all the tests. If I choose the latter option, where would I put the "runner" class? Would I put it in tests/org/vulcan/geom/, or in tests/org/vulcan/, or in tests/org/? And how would I run it from Sublime Text 3?

推荐答案

关于问题1:

  • 安装程序包控制
  • 安装Sublime Linter
  • 安装Sublime Linter Javac

编辑Vulcan.sublime-project并添加以下内容

edit Vulcan.sublime-project and add the following

"SublimeLinter": 
{
    "linters": 
    {
        "javac": 
        {
            "args": 
            [
                "-cp",
                "/usr/share/java/junit4.jar",
                "-sourcepath",
                "${project}/src/core:${project}/src/tests"
            ]
        }
    }
}

在-cp中,您可以添加编译所需的任何.jar库.在-sourcepath中,您可以添加项目的多个源(请注意使用唯一的程序包名称).两者都可以采用以:"分隔的多个路径.

In the -cp you can add whatever .jar libraries are needed for compilation. In the -sourcepath you can add multiple sources of your project (beware to use unique package names). Both can take multiple paths separated by ":".

这两个参数实际上是javac cli参数,因此您可以查找javac手册页以获得更多功能.

Those two parameters are actually javac cli parameters, so you can look up javac man page for more functionality.

如果使用上述设置,则每次将.java文件保存在sublime中时,javac都会对其进行编译并在同一目录中创建.class文件. Sublime linter会在侧边栏装订线中向您显示编译错误和警告.

If you use the setup above, every time you save a .java file in sublime, javac will compile it and create a .class file in the same dir. Sublime linter will show you compilation errors and warnings in the sidebar gutter.

关于问题2:

如果要测试vulcan内部的所有内容,TestRunner.java应该位于tests/org/vulcan中.如果要测试geom TestRunner.java中的所有内容,则应位于tests/org/vulcan/geom中.

If you want to test everything that's inside vulcan, TestRunner.java should be in tests/org/vulcan. If you want to test everything that's inside geom TestRunner.java should be in tests/org/vulcan/geom.

TestRunner.java示例- http://www.vogella.com/tutorials /JUnit/article.html#juniteclipse_code

TestRunner.java example - http://www.vogella.com/tutorials/JUnit/article.html#juniteclipse_code

但是要运行测试,您必须保存所有文件,以便在运行后对其进行编译

But for running the tests, you must save all your files so they get compiled and after run

cd .../Vulcan/src/tests
java -cp .:/usr/share/java/junit4.jar org.vulcan.TestRunner

主题不明确-为什么对Java使用sublime:

如果您使用较少的自动完成,自动导入,复制/粘贴功能-您会更快地学习,对细节的关注会更好,从长远来看,您可以更快地编写正确的代码.

If you use less autocomplete, autoimport, copy/paste - you learn faster, your attention to details become better and in the long run you are writing correct code faster.

这篇关于如何将JUnit与Sublime Text 3结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆