仅在Linux上运行JUnit测试

时间:2014-10-17 14:39:37

标签: java junit

我有一个基本的JUnit测试,我只想在Linux上运行。如果我在Windows上构建代码,我怎么能跳过测试?

例如,我可以从Java获得OS平台吗?

2 个答案:

答案 0 :(得分:6)

System.getProperty("os.name")将为您提供操作系统的名称。如果操作系统是Windows,则可以使用Assume类跳过测试:

@Test
public void testSomething() {
    Assume.assumeFalse
        (System.getProperty("os.name").toLowerCase().startsWith("win"));
    // test logic
}

修改:
现代JUnit Jupiter对@EnableOnOs@DisableOnOs注释有一个built-in capability

@Test
@EnabledOnOs(LINUX)
public void testSomething() {
    // test logic
}

答案 1 :(得分:3)

您还可以使用@Before绕过课程中包含的所有测试:

@Before
public void beforeMethod()
{
    Assume.assumeFalse(System.getProperty("os.name").toLowerCase().startsWith("win"));
    // rest of the setup
}