2

I have a lot of classes extended from an abstract JUnit class that has several @Test cases. The thing is that in some of this extended classes i need to skip/ignore all the test cases only if one environment variable has a specific value.

For example, in ruby tests suites i can do the following in the before method:

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

Is there a way to do the same in Java? All i can find is the @Ignore way to do it, but as the @Tests that the class run are not in the same class, but in the abstract class, and those @Tests are shared among several other classes i cannot add the @Ignore tag to those @Test cases. Maybe with a @Rule that exists only in those specific extended classes that need it?

Remember that the @Tests are not in my class but an abstract one that is shared with other classes that don't need to skip/ignore them. And that I need to run all the classes in the same run, i can not pass class specific arguments to the maven command, because it need to be class specific.

Other way i found here Conditionally ignoring tests in JUnit 4 is to use the line "org.junit.Assume.assumeTrue(someCondition());" but i don't understand how to use it with my environment variable:

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

Maybe something like Ignore Test Cases Based on Environment? Did that not skip/ignore the test but gives me an "org.junit.AssumptionViolatedException: test" error

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

EDIT: This is my test class:

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;
        }
    }

}
kaya3
  • 47,440
  • 4
  • 68
  • 97
Lea2501
  • 311
  • 6
  • 22

1 Answers1

3

In your first example you just need to replace "someCondition()" with your environment check e.g.:

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

You might want to note that @Before is ran before each method. Since this is an environment check it may make more sense to check it on the class level. Same concept but use @BeforeClass instead.

SomeoneLost
  • 226
  • 1
  • 7
  • Thanks! but with this code the execution fails with a "org.junit.AssumptionViolatedException: got: , expected: is " error instead of skipping the execution – Lea2501 Apr 17 '17 at 19:23
  • Right, so this method will still execute, it has to to test the assumption, but the test should display as being ignored instead of failed. If you post your test class I may be able to help you more. – SomeoneLost Apr 17 '17 at 19:51
  • Your using the `@Override` annotation on your setup method. I'm not sure that JUNIT will treat failed assumptions the same way as it would if you were using `@Before` or `@BeforeClass`. Try changing `@Override` to `@BeforeClass`. – SomeoneLost Apr 18 '17 at 13:23
  • If I put the code in the @BeforeClass, the tests skips even if the condition applies, `@BeforeClass public static void setUpBeforeClass() throws Exception { // Runs only in prod ENVIRONMENT String currentEnv = System.getenv("AMBIENTE"); String expectedEnv = "prod"; assumeTrue(currentEnv == expectedEnv); }` – Lea2501 Apr 18 '17 at 16:49
  • Have you verified that `System.getenv("AMBIENTE")` is returning the expected result, including casing? Alternatively, you can manually set `currentEnv` and `expectedEnv` to the same value. If the tests run, then you know the functionality is correct and you'll need to take a closer look at the results of getenv. – SomeoneLost Apr 18 '17 at 17:36