2017-04-12 111 views
1

我有很多类从具有多个@Test情况的抽象JUnit类扩展而来。问题是,在这些扩展类中,我只需要在一个环境变量具有特定值时跳过/忽略所有测试用例。基于环境变量跳过/忽略基于环境变量的Before/Setup方法中的JUnit测试用例

例如,在红宝石测试套件,我可以做下面的方法之前:

require 'rubygems' 
require 'minitest/spec' 
require 'minitest/autorun' 
require 'selenium-webdriver' 

class WebPcAr < MiniTest::Test 

    def setup 

     ... 

     ### SET URLS 
     case ENV['ENVIRONMENT_NAME'] 
     when "test" 
      skip 
     when "preprod" 
      skip 
     when "prod" 
      @base_url = 'http://www.google.com.ar/' 
     else 
      skip 
     end 
    end 

有没有办法做同样在Java中?所有我能找到的是@Ignore的方式来做到这一点,但作为类运行的@Tests不在同一个类中,而是在抽象类中,并且这些@Tests在几个其他类中共享,我不能将@忽略这些@Test情况的标签。也许只有在那些需要它的特定扩展类中才存在@Rule?

请记住,@Tests不在我的课堂上,而是一个与其他类共享的抽象类,不需要跳过/忽略它们。而且我需要在同一个运行中运行所有的类,我不能将类特定的参数传递给maven命令,因为它需要是类特定的。

我在这里找到的其他方式Conditionally ignoring tests in JUnit 4是使用“org.junit.Assume.assumeTrue(someCondition())”这一行;“但我不明白如何与我的环境变量,使用它:

@Before 
public void beforeMethod() { 
    org.junit.Assume.assumeTrue(someCondition()); 
    // rest of setup. 
} 

可能像Ignore Test Cases Based on Environment?难道不是跳过/忽略测试,但给了我一个“org.junit.AssumptionViolatedException:测试”错误

@Override 
public void setUp() throws Exception { 
    assumeTrue(System.getenv("AMBIENTE"), equals("prod")); 
    super.setUp(); 
    ... 

编辑: 这是我的测试类:

package My.Site.suitesWeb; 

import static org.junit.Assert.assertTrue; 
//import static org.junit.Assume.assumeTrue; 

import org.junit.Ignore; 
import org.junit.Test; 

import My.Site.classesWeb.*; 

public class TestWebPcMySite extends WebPcBase { 

    @Override 
    public void setUp() throws Exception { 
     // Runs only in prod ENVIRONMENT 
     String currentEnv = System.getenv("AMBIENTE"); 
     String expectedEnv = "prod"; 
     assumeTrue(currentEnv == expectedEnv); 
     // Run parent class setup 
     super.setUp(); 
     // Override with specific classes 
     goTo = new ObjGoToPageWebPcMySite(driver); 
     homePage = new ObjHomePageWebPcMySite(driver); 
     loginPage = new ObjLoginPageWebPcMySite(driver); 
    } 

    // I IGNORE SOME METHODS FROM EXTENDED CLASS 
    @Ignore 
    @Test 
    public void testHigh_LandingPage() throws Throwable { 
    } 

    @Ignore 
    @Test 
    public void testLow_LandingPage_LinkLogin() throws Throwable { 
    } 

    @Ignore 
    @Test 
    public void testLow_LandingPage_LinkRegister() throws Throwable { 
    } 

    // I OVERRIDE OTHER METHODS WITH OTHER CODE 
    @Test 
    public void testHigh_UserLogin_OkUserWithSuscriptionActive() throws Throwable { 
     try { 
      // Test data 

      // Test case 
      goTo.homePage(baseUrl); 
      homePage.loginButton().click(); 

      loginPage.userLogin(usernameLogin, passwordLogin); 
      assertTrue(homePage.profileNameButton().isDisplayed()); 
     } catch (Throwable e) { 
      utilsCommon.errorHandle(e, suiteName, testName); 
      throw e; 
     } 
    } 

} 

在此先感谢。

回答

2

在你的第一个例子中,你只需要更换“someCondition()”与您的环境检查例如为:

@Before 
public void beforeMethod() { 
    String currentEnv = someMethodToGetYourEnvironment(); 
    String expectedEnv = "DEV"; 
    org.junit.Assume.assumeTrue(currentEnv == expectedEnv); 
    // rest of setup. 
} 

你可能要注意的是@Before在每个方法之前跑了。由于这是一个环境检查,因此在课堂上检查它可能更有意义。相同的概念,但使用@BeforeClass来代替。

+0

谢谢!但是对于这段代码,执行失败的原因是“org.junit.AssumptionViolatedException:got:,expected:是”错误,而不是跳过执行 –

+0

正确,所以这个方法仍然会执行,它必须测试这个假设,但测试应显示为被忽略而不是失败。如果你发布你的测试班,我可能会帮助你更多。 – SomeoneLost

+0

谢谢!我编辑了问题并添加了代码。 –