1

I don't want the Junit to call my test methods/selenium testcases sequentially. But i want the particular testcase to execute or it should get called as per my need.

Example code:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;


public class demo2 {
    Selenium selenium;

    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/");
        selenium.start();
        selenium.setTimeout("6000");
    }

    @Test
    public void test_3() throws Exception {
        selenium.open("/");
        selenium.type("q", "3");
    }
    @Test
    public void test_4() throws Exception {
        selenium.open("/");
        selenium.type("q", "4");
    }

    @After
    public void tearDown() throws Exception {
            selenium.stop();
    }
}

Note

I want the the methods test_3,test_4.... should be called depending on the condition.

oers
  • 18,436
  • 13
  • 66
  • 75
webtester
  • 21
  • 6

4 Answers4

3

You can use Assume class from JUnit. You can read more in http://junit.org/apidocs/org/junit/Assume.html for usage.

belgther
  • 2,544
  • 17
  • 15
2

You can use Assume

assumeTrue(conditionIsFulfilled)

From the doc:

A failed assumption does not mean the code is broken, but that the test provides no useful information. The default JUnit runner treats tests with failing assumptions as ignored. Custom runners may behave differently.

oers
  • 18,436
  • 13
  • 66
  • 75
0

TestNG allows this via the @dependsOnMethods annotation.

http://testng.org/doc/documentation-main.html

If you are stuck with JUnit, then write your own RunsWith, that you could use to add the same or similar functionality based on your needs.

A great example is seen here: Specifying an order to junit 4 tests at the Method level (not class level)

Community
  • 1
  • 1
npellow
  • 1,985
  • 1
  • 16
  • 23
0

Instead of writing different @test, you can write those test as normal java methods and write only one test.

in that test you can call any method based on some condition.

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;


public class demo2 {
    Selenium selenium;

    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/");
        selenium.start();
        selenium.setTimeout("6000");
    }

    @Test
    public void test() throws Exception {
        if(<condition1>){
          method1(selenium);
        }  
        if(<condition2>){
          method2(selenium);
        }  
        if(<condition3>){
          method3(selenium);
        }  
        if(<condition4>){
          method4(selenium);
        }  

    }

    public static void method1(Selenium selenium) 
    throws Exception {
        selenium.open("/");
        selenium.type("q", "1");
    }

    public static void method2(Selenium selenium) 
    throws Exception {
        selenium.open("/");
        selenium.type("q", "2");
    }

    public static void method3(Selenium selenium) 
    throws Exception {
        selenium.open("/");
        selenium.type("q", "3");
    }

    public static void method4(Selenium selenium) 
    throws Exception {
        selenium.open("/");
        selenium.type("q", "4");
    }

    @After
    public void tearDown() throws Exception {
            selenium.stop();
    }
}
Santoshsarma
  • 5,627
  • 1
  • 24
  • 39