【问题标题】:Automatically creating tests in intellij idea with multiple test directories使用多个测试目录在 intellij idea 中自动创建测试
【发布时间】:2020-05-18 12:52:13
【问题描述】:

在我的项目中,我决定将单元测试和集成测试拆分为单独的包,我的项目结构基本上如下所示:

├── main
│   ├── java
│   │   └── ...
│   └── resources
│       └── ...
├── test
│   └── java
│       └── ...
└── test-integration
    ├── java
    │   └── ...
    └── resources
        └── ...

轻松分离两种测试是一个不错的选择,intellij 重构与此解决方案配合得很好。一切都设置正确,两个test 目录都使用gradle 的idea 插件标记为test sources roots,gradle 识别两个源集,我创建了两个单独的任务来运行每个测试套件等。我唯一的问题是intellij 自动 create test 向导不区分这两个目录,我唯一可以修改的是 Destination package 类路径,这对于两个测试目录显然是相同的,并且没有选项选择我想使用的。

有什么方法可以配置 IDEA 让我可以在生成测试的目录之间进行选择?或者可能是某种 gradle 插件,它会根据后缀移动测试文件(例如,*Test 类转到 test 目录,*IT 类转到 test-integration 目录)?或者也许根本没有这样的选择:D

【问题讨论】:

    标签: java testing gradle intellij-idea


    【解决方案1】:

    没有办法这样做。这是该对话框的the source。最终决定目录的是this method

    protected static List<VirtualFile> computeTestRoots(@NotNull Module mainModule) {
        if (!computeSuitableTestRootUrls(mainModule).isEmpty()) {
          //create test in the same module, if the test source folder doesn't exist yet it will be created
          return suitableTestSourceFolders(mainModule)
              .map(SourceFolder::getFile)
              .filter(Objects::nonNull)
              .collect(Collectors.toList());
    }
    

    IntelliJ 具有很强的可扩展性。您可以相对容易地编写自己的插件来更改此行为,方法是用您自己的对话框替换与操作关联的对话框。您自己的对话框可能主要是对已有内容的复制粘贴,但使用 selectTargetDirectory 的不同实现。

    【讨论】:

      【解决方案2】:

      “创建测试”对话框之后的下一步应该是“选择目标目录”

      v2020.1.1 (https://www.jetbrains.com/idea/download) 中的行为。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 2012-05-19
        • 1970-01-01
        • 2021-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多