简体   繁体   中英

Run JUnit test only on Linux

I have a basic JUnit test which I want to run only on Linux. How I can skip the test if I build the code on Windows?

For example can I get the OS platform from Java?

System.getProperty("os.name") will give you the name of the OS. You can then use the Assume class to skip a test if the OS is Windows:

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

Edit:
The modern JUnit Jupiter has a built-in capability for this with the @EnableOnOs and @DisableOnOs annotations:

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

You can also use @Before to bypass all tests contained in the class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM