当前位置: 首页 > 知识库问答 >
问题:

TestNG没有执行套件中的所有测试类

章哲彦
2023-03-14

执行testng xml时,它在套件中只执行1个测试

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" verbose = "2" name="Suite" configfailurepolicy="skip">
  <test name="all-tests">
    <classes>
            <class name="Test.DemoMavenEclipseProject.BasicTest"/>
            <class name="Test.DemoMavenEclipseProject.LoginTest"/>
            <class name="Test.DemoMavenEclipseProject.AllProductsTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

基本测试-

package Test.DemoMavenEclipseProject;

    import org.testng.ITestResult;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.BeforeSuite;
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.io.FileHandler;
    import com.relevantcodes.extentreports.ExtentReports;
    import com.relevantcodes.extentreports.ExtentTest;
    import com.relevantcodes.extentreports.LogStatus;
    import Actions.DriverManager;
    import Actions.DriverManagerFactory;
    import Actions.DriverType;
    import Utility.Constants;

public class BasicTest {
    DriverManager driverManager;
    WebDriver driver;
    ExtentReports report;
    ExtentTest logger;

    @BeforeSuite
    public void setUp() throws Exception {
       report=new ExtentReports("./Reports/TestReport.html");
       driverManager = DriverManagerFactory.getDriverManager(DriverType.CHROME);
       driver = driverManager.getWebDriver();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       driver.get(Constants.URL);
       driver.manage().window().maximize();
    }   

    @AfterSuite
    public void getResult(ITestResult result) throws IOException {
       if (result.getStatus()==ITestResult.FAILURE) { 
       logger.log(LogStatus.FAIL, "TEST CASE FAILED IS " + result.getName()); // to add name in extent report
       logger.log(LogStatus.FAIL, "TEST CASE FAILED IS " + result.getThrowable()); 

       String screenshotPath = BasicTest.getScreenshot(driver, result.getName());
            logger.addScreenCapture(screenshotPath);// adding screen shot
       }
       else if (result.getStatus()==ITestResult.SKIP) {
          logger.log(LogStatus.SKIP, "Test Case SKIPPED IS " + result.getName());
       }
       else if (result.getStatus() == ITestResult.SUCCESS) {
               logger.log(LogStatus.PASS, "Test Case PASSED IS " + result.getName());
      }
    }
    public void tearDown() throws Exception {
            report.endTest(logger);
            //Flush the data to report
            driver.quit();
            report.flush();
    }

    public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException {
          String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
          TakesScreenshot ts = (TakesScreenshot) driver;
          File source = ts.getScreenshotAs(OutputType.FILE);

          // after execution, you could see a folder "FailedTestsScreenshots" under src folder
          String destination = "./Reports/Screenshots/" + screenshotName + dateName + ".png";
          File finalDestination = new File(destination);
          FileHandler.copy(source, finalDestination);
         // FileUtils.copyFile(source, finalDestination);
          return destination;
      }
    }

登录测试-

package Test.DemoMavenEclipseProject;

import org.testng.annotations.Test;

import com.relevantcodes.extentreports.LogStatus;

import Actions.login_action;
import PageObjects.homePage;

public class LoginTest extends BasicTest {
  @Test
    public void Login() throws Exception {
        //Create object for Report with filepath
        //Start the test
        logger=report.startTest("LoginTest");
        //Log the status in report
        logger.log(LogStatus.INFO, "Login link is displayed");
        login_action.Execute(driver);
    if (homePage.link_Logout(driver).isDisplayed())
            logger.log(LogStatus.PASS, "Logged into the site successfully");
        else 
        logger.log(LogStatus.FAIL, "Login is unsuccessful");
        //End the test
        Thread.sleep(2000);
  }

}

共有2个答案

郭阳泽
2023-03-14

在xml文件“configfailurepolicy=“skip”中,根据testng,如果套件中有configfailurepolicy==skip,那么如果@Before*方法失败,testng应该跳过以执行套件中的其余测试。

根据您的控制台,我已经看到Beforetest方法失败,所以请检查并让我知道。

孔阳平
2023-03-14

@Brian,这是控制台输出:

[RemoteTestNG] detected TestNG version 7.0.0
...
... TestNG 7.0.0-beta3 by Cédric Beust (cedric@beust.com)
...

Starting ChromeDriver 78.0.3904.105 (60e2d8774a8151efa6a00b1f358371b1e0e07ee2-refs/branch-heads/3904@{#877}) on port 46601
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1576688370.025][WARNING]: This version of ChromeDriver has not been tested with Chrome version 79.
Dec 18, 2019 11:59:30 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
===== Invoked methods
  BasicTest.startTest(java.lang.reflect.Method)[pri:0, instance:Test.EcommerceProject.LoginTest@5d76b067]public void Test.EcommerceProject.LoginTest.Login() throws java.lang.Exception  1568059495
=====
FAILED CONFIGURATION: @BeforeMethod startTest(public void Test.EcommerceProject.LoginTest.Login() throws java.lang.Exception)
java.lang.NullPointerException
    at Test.EcommerceProject.BasicTest.startTest(BasicTest.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:131)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:61)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:340)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:294)
    at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:669)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:496)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:170)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:790)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:143)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at org.testng.TestRunner.privateRun(TestRunner.java:763)
    at org.testng.TestRunner.run(TestRunner.java:594)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:398)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:392)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1146)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1067)
    at org.testng.TestNG.runSuites(TestNG.java:997)
    at org.testng.TestNG.run(TestNG.java:965)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

SKIPPED CONFIGURATION: @AfterMethod getResult
SKIPPED: Login
java.lang.NullPointerException
    at Test.EcommerceProject.BasicTest.startTest(BasicTest.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:131)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:61)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:340)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:294)
    at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:669)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:496)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:170)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:790)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:143)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at org.testng.TestRunner.privateRun(TestRunner.java:763)
    at org.testng.TestRunner.run(TestRunner.java:594)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:398)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:392)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1146)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1067)
    at org.testng.TestNG.runSuites(TestNG.java:997)
    at org.testng.TestNG.run(TestNG.java:965)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    FirstTest
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 1
===============================================

===== Invoked methods
  BasicTest.setUp()[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f] 391618063
  BasicTest.startTest(java.lang.reflect.Method)[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f]public void Test.EcommerceProject.ProductsTest.All_Products() throws java.lang.Exception  391618063
    ProductsTest.All_Products()[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f] 391618063
  BasicTest.getResult(org.testng.ITestResult)[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f][TestResult name=All_Products status=SUCCESS method=ProductsTest.All_Products()[pri:0, instance:Test.EcommerceProject.ProductsTest@17579e0f] output={null}]  391618063
=====
PASSED: All_Products

===============================================
    SecondTest
    Tests run: 1, Failures: 0, Skips: 0
===============================================

log4j:WARN No appenders could be found for logger (freemarker.cache).
log4j:WARN Please initialize the log4j system properly.

===============================================
Suite
Total tests run: 2, Passes: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================

 类似资料:
  • 试着用TestNG运行我所有的cucumber测试。然而,我的套房没有找到。 当我将TestNG.xml文件作为TestNG运行时,未找到如下所示的测试

  • 我试图用TestNG.xml运行我的TestNG套件。我的套件中总共有四个类,但我只能用所有方法一次运行两个类。假设我在每个类中有3个方法,那么在TestNG套件中,如果我只添加两个类,TestNG.xml文件将用6个方法运行我的两个测试。现在,如果我尝试在testng.xml文件中添加第三个类,它将只运行每个类的第一个方法。这意味着现在它只运行3个方法(每个类一个)。我在我的项目下创建了一个源文

  • 我设置了Selenium grid2,它与以下并行TestNG测试套件配合得很好: 但是当我在TestNG蚂蚁任务中执行测试时,并行不再工作。 在我将测试套件文件中的并行模式更改为“tests”后,它再次工作,如下所示: 那么,这是否意味着TestNG ant任务不支持“类”并行测试套件?

  • 我正在尝试运行配置套件文件的并行测试。测试是针对使用Selenium的web应用程序。套件文件由多个测试组成。每个测试都包含多个测试类。每个测试中的第一个类用于初始化(@beforetest)WebDriver并关闭它(@aftertest)。WebDriver是静态的,以便将其传递给其他类(原因是我们需要从最后一个测试类结束的地方继续测试)。 当套件配置为按顺序运行测试时,测试将成功运行。但是当

  • 我试图使用XML和TestNG运行一个测试套件,但是我总是同时使用Eclipse和命令行得到相同的消息: 该文件已正确读取,但测试似乎没有运行。 以下是我的testng.xml的内容: 这是我的目录结构在Eclipse中的样子: 此外,这也是我试图通过命令行运行测试套件的方式: 我尝试过通过eclipse清理项目,但似乎没有帮助。我也试过跑步: < code>mvn clean,但它也没有完成工作

  • 主要内容:1. 测试类,2. testng.xml,其他例子测试套件是用于测试软件程序的行为或一组行为的测试用例的集合。 在TestNG中,我们无法在测试源代码中定义一个套件,但它可以由一个XML文件表示,因为套件是执行的功能。 它还允许灵活配置要运行的测试。 套件可以包含一个或多个测试,并由标记定义。 是的根标记。 它描述了一个测试套件,它又由几个部分组成。 下表列出了接受的所有定义的合法属性。 属性 描述 name 套件的名称,这是一个强制属性。 ve