Peter Penzov
Peter Penzov

Reputation: 1626

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?

Upvotes: 7

Views: 4887

Answers (2)

Mureinik
Mureinik

Reputation: 311498

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
}

Upvotes: 16

ToYonos
ToYonos

Reputation: 16833

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
}

Upvotes: 3

Related Questions