基于文件分隔符禁用 Junit5 测试

Disable Junit5 tests based on file separator

我希望能够根据 OS 使用的文件分隔符禁用 JUnit5 测试。

例如 windows 文件分隔符是 \,示例路径是 src\resources\fou.

在另一个 OS 上,文件分隔符可以是 /(也许 MAC?)。 当文件分隔符为 \ 时,我希望我的测试仅 运行

我发现了这个:@EnabledIfSystemProperty(named = "file.separator", matches = "[/]") 所以我将 / 更改为 \ 以匹配 windows 文件分隔符,但它没有工作给我这个错误: failed to evaluate condition.

只有 Windows 使用 \ 作为文件分隔符。

因此,以下应该会导致所需的行为。

@Test
@EnabledOnOs(OS.WINDOWS)
void onlyOnWindows() {
    // ...
}

复制自:https://junit.org/junit5/docs/current/user-guide/#writing-tests-conditional-execution-os

就是说,您通过 4(四)个反斜杠匹配 Java 中的单个反斜杠。这也应该有效:

@EnabledIfSystemProperty(named = "file.separator", matches = "\\")