如何在testng类中调用java方法

时间:2013-10-16 10:18:00

标签: java report testng

我在testng报告中收到错误:

我按照以下步骤

  1. 我用java方法编写了所有测试用例,并使用了java验证,如果没有传递我的测试用例
  2. 2.我创建了一个testng类,在testng类中我调用了我的所有java方法

    1. 我执行了testng类,这个类包含大约30个java方法,每个方法都是一个测试用例。

    2. 如果我执行为基于testng的注释生成的类报告,它不会将java方法考虑到测试用例中,如何调用我的所有java方法?我需要为我的java方法生成报告

    3. 这是我的代码:

      import java.io.FileNotFoundException;
      import java.io.IOException;
      
      import jxl.read.biff.BiffException;
      import jxl.write.WriteException;
      import jxl.write.biff.RowsExceededException;
      
      import org.testng.annotations.BeforeClass;
      import org.testng.annotations.Test;
      
      public class DocumentSearchTest {
          WebElements webEleObj;
          /*
           * AllLetter Lettobj ; AllSearch allseObj; AllTranscript TraObj;
           */
          FrameSearchExported fseObj;
          TextBoxSearch textObj;
          DateSinceSearch dateSinceObj;
      
          /*
           * public void loginTest() throws FileNotFoundException, BiffException,
           * IOException, InterruptedException {
           * webEleObj.textbox(webEleObj.properties
           * ("Username"),webEleObj.excelRead(1,2,1));
           * webEleObj.textbox(webEleObj.properties
           * ("Password"),webEleObj.excelRead(2,2,1)); webEleObj.sleep(5000);
           * webEleObj.button(webEleObj.properties("Login")); webEleObj.sleep(20000);
           * 
           * }
           */
      
          @BeforeClass
          public void start() throws FileNotFoundException, BiffException,
          IOException, InterruptedException, RowsExceededException,
          WriteException {
              // Assert.assertEquals(true, true, "Loggend into application");
              webEleObj = new WebElements();
              /*
               * allseObj = new AllSearch(webEleObj); Lettobj = new
               * AllLetter(webEleObj); TraObj =new AllTranscript(webEleObj);
               */
              fseObj = new FrameSearchExported(webEleObj);
              textObj = new TextBoxSearch(webEleObj);
              dateSinceObj = new DateSinceSearch(webEleObj);
              webEleObj.browserLaunch();
              webEleObj.loginTest();
              webEleObj.sleep(20000);
              webEleObj.setUpApp();
              // webEleObj.excelwrite(4);
              System.out.println("hi logged in");
          }
      
          @Test
          public void ts_1() throws FileNotFoundException, IOException,
          InterruptedException, RowsExceededException, BiffException,
          WriteException {
              webEleObj.sleep(10000);
              System.out.println("First TestCase---->");
              fseObj.allexportedSearch();
              fseObj.letterexportedSearch();
              fseObj.transcriptexportedSearch();
              fseObj.allnotexpSearch();
              fseObj.letternotexpSearch();
              fseObj.transcriptnotexpSearch();
              fseObj.allsignsearch();
              fseObj.lettersignSearch();
              fseObj.transcriptsignSearch();
              fseObj.allnotsignSearch();
              fseObj.letternotsignSearch();
              fseObj.transcriptnotsignSearch();
              System.out.println("Document Search Test Case Completed");
      
          }
      
          /*
           * @Test(enabled=false) public void ts_2() throws FileNotFoundException,
           * BiffException, IOException, InterruptedException, RowsExceededException,
           * WriteException { System.out.println("Second TestCase---->");
           * textObj.accountNo_All(); textObj.accountNo_Letter();
           * textObj.accountNo_Transcript(); textObj.firstName_All();
           * textObj.firstName_Letter(); textObj.firstName_Transcript();
           * textObj.lastName_All(); textObj.lastName_Letter();
           * textObj.lastName_Transcript();
           * 
           * }
           */
          @Test
          public void ts_3() throws FileNotFoundException, IOException,
          InterruptedException, RowsExceededException, BiffException,
          WriteException {
              // webEleObj.sleep(10000);
              System.out.println("Third TestCase---->");
              dateSinceObj.datesinceAll_Today();
              dateSinceObj.datesinceAll_Yesterday();
              dateSinceObj.datesinceAll_ThisMonth();
              dateSinceObj.datesinceAll_LastMonth();
              dateSinceObj.datesinceAll_ThisYear();
              dateSinceObj.datesinceAll_LastYear();
              dateSinceObj.datesinceLetter_Today();
              dateSinceObj.datesinceLetter_Yesterday();
              dateSinceObj.datesinceLetter_ThisMonth();
              dateSinceObj.datesinceLetter_LastMonth();
              dateSinceObj.datesinceLetter_ThisYear();
              dateSinceObj.datesinceLetter_LastYear();
              dateSinceObj.datesinceTranscript_Today();
              dateSinceObj.datesinceTranscript_Yesterday();
              dateSinceObj.datesinceTranscript_ThisMonth();
              dateSinceObj.datesinceTranscript_LastMonth();
              dateSinceObj.datesinceTranscript_ThisYear();
              dateSinceObj.datesinceTranscript_LastYear();
      
              // logOut();
      
          }
      }
      

2 个答案:

答案 0 :(得分:0)

您可以使用jUnit testSuite,您可以在其中定义要同时运行的所有测试类:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ MyClassTest.class, MySecondClassTest.class })
public class AllTests {

} 

您可以在@ Vogella http://www.vogella.com/articles/JUnit/article.html#juniteclipse_testsuite

找到更多信息

答案 1 :(得分:0)

以下是调用测试类中任何java方法的代码: -

A类

公共类A {

static void method1()
{
    System.out.println("Selenium_1");
}
static void method2()
{
    System.out.println("Selenium_1");
}

}

B类

公共类B扩展A {

public static void main(String ar[])
{
    method1();
    method2();
}

}