【问题标题】:How to customize the Junit's testcase calling functionality如何自定义 Junit 测试用例调用功能
【发布时间】:2012-02-08 10:22:54
【问题描述】:

我不希望 Junit 按顺序调用我的测试方法/硒测试用例。但我希望执行特定的测试用例,否则应该根据我的需要调用它。

示例代码:

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

注意

我希望根据条件调用 test_3,test_4.... 方法。

【问题讨论】:

    标签: java selenium junit


    【解决方案1】:

    您可以使用 JUnit 中的 Assume 类。您可以在http://junit.org/apidocs/org/junit/Assume.html阅读更多使用方法。

    【讨论】:

      【解决方案2】:

      您可以使用Assume

      assumeTrue(conditionIsFulfilled)
      

      来自文档:

      假设失败并不意味着代码被破坏,而是意味着 测试没有提供有用的信息。默认的 JUnit runner 处理 忽略失败假设的测试。自定义跑步者可能会表现 不同。

      【讨论】:

        【解决方案3】:

        TestNG 通过 @dependsOnMethods 注释允许这样做。

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

        如果您被 JUnit 卡住了,那么请编写您自己的 RunsWith,您可以使用它来根据您的需要添加相同或相似的功能。

        这里有一个很好的例子:Specifying an order to junit 4 tests at the Method level (not class level)

        【讨论】:

          【解决方案4】:

          不用写不同的@test,你可以把这些测试写成普通的java方法,只写一个测试。

          在该测试中,您可以根据某些条件调用任何方法。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-11-09
            • 1970-01-01
            • 1970-01-01
            • 2021-11-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多