在TestNG中处理断言

在TestNG中处理断言,testng,assertion,Testng,Assertion,如果断言失败,如何在TestNG中继续执行测试?如何在TestNG的HTML报告中报告失败 运行以下代码时,将执行断言后的行,但在报告中未列出断言失败: @Test public void googleSearch(){ driver.get("http://www.google.co.in/"); System.out.println(" ---------- Start -------------"); try { Assert.assertTrue

如果断言失败,如何在TestNG中继续执行测试?如何在TestNG的HTML报告中报告失败

运行以下代码时,将执行断言后的行,但在报告中未列出断言失败:

@Test
public void googleSearch(){
    driver.get("http://www.google.co.in/");
    System.out.println(" ---------- Start -------------");

    try {
        Assert.assertTrue(driver.findElement(By.xpath("xyz")).isDisplayed(), "unable to find the link");
    } catch (Throwable t) {
        System.out.println("Error");
    }
    System.out.println(" ---------- End -------------");
}

如果捕捉到断言错误,TestNG看不到它。

如果捕捉到断言错误,TestNG看不到它。

在测试设计中,最好避免在测试失败后继续测试,而不是捕捉断言错误

  • 如果您需要关闭、断开连接或以其他方式释放测试使用的资源,请在@AfterMethod中执行此操作
  • 如果您想测试多个独立的东西,并验证其中哪些通过,哪些失败,请将它们放入单独的测试中。如果有多个测试共享类似的设置(例如,导航到应用程序中的某个点),请将设置移动到@BeforeMethod
  • 如果要测试相互依赖的多个条件,则应在遇到第一个故障时立即退出测试。由于依赖性,您可能会在以后的断言中看到错误否定
在您的示例中:

@BeforeMethod
public void openPage() {
    driver.get("http://www.google.co.in/");
    System.out.println(" ---------- Start -------------");
}

@Test
public void googleSearchThis(){
    Assert.assertTrue(driver.findElement(By.xpath("xyz")).isDisplayed(), "unable to find the link");
}

@Test
public void googleSearchThat(){
    // assert for other things you want to test on the same page
}

@AfterMethod
public void closePage() {
    System.out.println(" ---------- End -------------");
}

在测试设计中,最好避免在测试失败后继续测试,而不是捕获断言错误

  • 如果您需要关闭、断开连接或以其他方式释放测试使用的资源,请在@AfterMethod中执行此操作
  • 如果您想测试多个独立的东西,并验证其中哪些通过,哪些失败,请将它们放入单独的测试中。如果有多个测试共享类似的设置(例如,导航到应用程序中的某个点),请将设置移动到@BeforeMethod
  • 如果要测试相互依赖的多个条件,则应在遇到第一个故障时立即退出测试。由于依赖性,您可能会在以后的断言中看到错误否定
在您的示例中:

@BeforeMethod
public void openPage() {
    driver.get("http://www.google.co.in/");
    System.out.println(" ---------- Start -------------");
}

@Test
public void googleSearchThis(){
    Assert.assertTrue(driver.findElement(By.xpath("xyz")).isDisplayed(), "unable to find the link");
}

@Test
public void googleSearchThat(){
    // assert for other things you want to test on the same page
}

@AfterMethod
public void closePage() {
    System.out.println(" ---------- End -------------");
}

您可以在catch块中使用此代码:-

org.testng.Assert.fail("expected and actual result do not match");
让我们以以下代码为例:-

String expectedtitle="xyz";
String actualtitle="xywz";
try {
    Assert.assertEquals(expectedtitle, actualtitle);
} catch(Throwable t) {              
    org.testng.Assert.fail("expected and actual result do not match");      
}

您可以在catch块中使用此代码:-

org.testng.Assert.fail("expected and actual result do not match");
让我们以以下代码为例:-

String expectedtitle="xyz";
String actualtitle="xywz";
try {
    Assert.assertEquals(expectedtitle, actualtitle);
} catch(Throwable t) {              
    org.testng.Assert.fail("expected and actual result do not match");      
}

同意,失败后继续测试不是最佳做法。对stuff使用after*Methods()

但是,特别是对于测试(配置)开始和结束的日志记录,根本不要在测试类中执行此操作-创建一个侦听器,并从侦听器中执行日志记录

您可以扩展TestListenerAdapter并实现它指定的所有方法:


同意,失败后继续测试不是最佳做法。对stuff使用after*Methods()

但是,特别是对于测试(配置)开始和结束的日志记录,根本不要在测试类中执行此操作-创建一个侦听器,并从侦听器中执行日志记录

您可以扩展TestListenerAdapter并实现它指定的所有方法:


我建议使用软断言,它是在TestNG本地提供的

package automation.tests;

import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;

public class MyTest {
  private Assertion hardAssert = new Assertion();
  private SoftAssert softAssert = new SoftAssert();
}

@Test
public void testForSoftAssertionFailure() {
  softAssert.assertTrue(false);
  softAssert.assertEquals(1, 2);
  softAssert.assertAll();
}

我建议使用软断言,它是在TestNG本地提供的

package automation.tests;

import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;

public class MyTest {
  private Assertion hardAssert = new Assertion();
  private SoftAssert softAssert = new SoftAssert();
}

@Test
public void testForSoftAssertionFailure() {
  softAssert.assertTrue(false);
  softAssert.assertEquals(1, 2);
  softAssert.assertAll();
}

由于您已捕获错误,因此不会失败。我重新创造了如下情况。这并没有失败。但是扔了一个一次性的<已打印代码>错误

public class TNG {
    WebDriver driver;

    @Test
    public void googleSearch(){
        System.setProperty("webdriver.chrome.driver", "path to web driver");
        driver = new ChromeDriver();
        driver.get("http://www.google.co.in/");
        System.out.println(" ---------- Start -------------");      
    try {
         Assert.assertTrue(driver.findElement(By.xpath("xyz")).isDisplayed(), "unable to find the link");

    } catch (Throwable t) {
        System.out.println("Error");
    }
    System.out.println(" ---------- End -------------");
  }

   @Test
   public void anotherTest(){
      System.out.println("another test");
   }
}
以下是执行测试的结果

another test
Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 43423
Only local connections are allowed.
Jul 20, 2017 5:55:33 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
---------- Start -------------
Error
---------- End -------------
PASSED: anotherTest
PASSED: googleSearch

===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
如果您想在某些测试失败的情况下继续执行测试,只需编写带有
@test
注释的方法,就像我创建的另一个test()方法一样。但是您不能保证测试执行的顺序。
在上面的示例中,首先执行了另一个test()。

因为您捕获了错误,所以不会失败。我重新创造了如下情况。这并没有失败。但是扔了一个一次性的<已打印代码>错误

public class TNG {
    WebDriver driver;

    @Test
    public void googleSearch(){
        System.setProperty("webdriver.chrome.driver", "path to web driver");
        driver = new ChromeDriver();
        driver.get("http://www.google.co.in/");
        System.out.println(" ---------- Start -------------");      
    try {
         Assert.assertTrue(driver.findElement(By.xpath("xyz")).isDisplayed(), "unable to find the link");

    } catch (Throwable t) {
        System.out.println("Error");
    }
    System.out.println(" ---------- End -------------");
  }

   @Test
   public void anotherTest(){
      System.out.println("another test");
   }
}
以下是执行测试的结果

another test
Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 43423
Only local connections are allowed.
Jul 20, 2017 5:55:33 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
---------- Start -------------
Error
---------- End -------------
PASSED: anotherTest
PASSED: googleSearch

===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
如果您想在某些测试失败的情况下继续执行测试,只需编写带有
@test
注释的方法,就像我创建的另一个test()方法一样。但是您不能保证测试执行的顺序。
在上面的示例中,首先执行了另一个test()。

如果我删除try-catch块,则不会执行断言失败后的下一步如果我删除try-catch块,则不会执行断言失败后的下一步当测试失败时,此afterMethod()不会运行此afterMethod()当测试失败时,此afterMethod()不会运行