我有一个基本的JUnit测试,我只想在Linux上运行。如果我在Windows上构建代码,我怎么能跳过测试?
例如,我可以从Java获得OS平台吗?
答案 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
}