TestNG - 软断言

The*_*tor 5 java testng

我有一个@Test脚本设置,它运行一些soft asserts.

但是,我遇到了assertAll. 我希望URLsassertAll. 这是可能的还是另一种推荐的方法?

@Test
public static void checkUrl(String requestUrl, String expectedUrl){

    SoftAssert softAssert = new SoftAssert ();

    try {

        URL obj = new URL(requestUrl);
        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        conn.setReadTimeout(5000);
        conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
        conn.addRequestProperty("User-Agent", "Mozilla");
        conn.addRequestProperty("Referer", "google.com");
        System.out.println();
        System.out.println("Request URL ... " + requestUrl);


        boolean redirect = false;

        // normally, 3xx is redirect
        int status = conn.getResponseCode();
        if (status != HttpURLConnection.HTTP_OK) {
            if (status == HttpURLConnection.HTTP_MOVED_TEMP
                    || status == HttpURLConnection.HTTP_MOVED_PERM
                    || status == HttpURLConnection.HTTP_SEE_OTHER) redirect = true;
        }

        System.out.println("Response Code ... " + status);
        if (redirect) {

            // get redirect url from "location" header field
            String redirectUrl = conn.getHeaderField("Location");

            // get the cookie if need, for login
            String cookies = conn.getHeaderField("Set-Cookie");

            // open the new connnection again
            conn = (HttpURLConnection) new URL(redirectUrl).openConnection();
            conn.setRequestProperty("Cookie", cookies);
            conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
            conn.addRequestProperty("User-Agent", "Mozilla");
            conn.addRequestProperty("Referer", "google.com");

            System.out.println("Redirect to URL : " + redirectUrl);
            //Assert.assertEquals (redirectUrl, expectedUrl);
            softAssert.assertEquals (redirectUrl, expectedUrl, "Expected URL does not match"
                    + requestUrl);
        } else {
            //org.testng.Assert.assertTrue (redirect);
            softAssert.assertTrue (redirect, "Please check the status for " + requestUrl);
             System.out.println("** Please check status for " + requestUrl);
             System.out.println("************************************************");
             System.out.println("************************************************");
        }


    }
    catch (Exception e) {
        e.printStackTrace();
    }

}
Run Code Online (Sandbox Code Playgroud)

Kri*_*van 5

您正在寻找的用例有点违背SoftAssert. SoftAssert基本上是在 TestNG 中引入的,因此您可以在一个@Test方法中收集所有断言,但仅在最后(当您调用assertAll())时使测试方法失败。

数据驱动@Test方法基本上是@Test一种运行“n”次的方法(每次迭代都使用不同的数据集运行)。因此,在最后一次迭代中尝试利用SoftAssert并调用它是没有意义的assertAll()。因为如果你这样做,它基本上会归结为只有最后一次迭代失败。

因此,如果您正在查看使用重新运行的测试 testng-failed.xml那么它将只包含最后一次迭代的索引(这有点荒谬,因为实际上失败的并不是最后一次迭代)。

所以理想情况下,你应该SoftAssert只在单次迭代的范围内使用。这意味着你SoftAssert在一个@Test方法中实例化一个对象,调用一堆assertXXX()调用,并在方法的末尾调用assertAll().

总而言之,如果您仍在寻找可以向您展示如何执行此操作的示例,这里有一个示例。

首先,我们定义一个接口,让我将数据提供者提供的数据集的大小设置为测试类的属性。

public interface IDataSet {
    void setSize(int size);
}
Run Code Online (Sandbox Code Playgroud)

测试类如下所示

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.TestInstance;
import org.testng.asserts.SoftAssert;

import java.util.concurrent.atomic.AtomicInteger;

public class SoftAssertDemo implements IDataSet {
  private int size;
  private SoftAssert assertion = new SoftAssert();
  private AtomicInteger counter = new AtomicInteger(1);

  @Override
  public void setSize(int size) {
    this.size = size;
  }

  @Test(dataProvider = "dp")
  public void testMethod(int number) {
    if ((number % 2) == 0) {
      assertion.fail("Simulating a failure for " + number);
    }
    if (counter.getAndIncrement() == size) {
      assertion.assertAll();
    }
  }

  @DataProvider(name = "dp")
  public Object[][] getData(@TestInstance Object object) {
    Object[][] data = new Object[][] {{1}, {2}, {3}, {4}, {5}};
    if (object instanceof IDataSet) {
      ((IDataSet) object).setSize(data.length);
    }
    return data;
  }
}
Run Code Online (Sandbox Code Playgroud)

这种方法的注意事项:

  1. 一个测试类应该只有一个 @DataProvider方法,因为数据提供者将数据集的大小传递回测试类实例。因此,如果您有 2 个或更多数据提供者,则可能会发生数据竞争,其中一个数据提供者会覆盖另一个数据提供者。
  2. 如果您想要容纳 2 个或更多数据提供者,那么您需要确保@Test由数据提供者提供支持的那些方法不会并行运行。