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?
Yes, you have an option to run your test cases in command line, Example: As you are running the mvn clean install for building your application, In the same way, you can also run your test cases as well by using the command.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With