Testng 介绍 (四)

本贴最后更新于 1357 天前,其中的信息可能已经时移世改

11、factory

之前写的测试类并不能自己创建,使用工厂注解之后允许我们动态创建测试类。

public class WebTestFactory {
  @Factory
  public Object[] createInstances() {
   Object[] result = new Object[10]; 
   for (int i = 0; i < 10; i++) {
      result[i] = new WebTest(i * 10);
    }
    return result;
  }
}
public class WebTest {
  private int m_numberOfTimes;
  public WebTest(int numberOfTimes) {
    m_numberOfTimes = numberOfTimes;
  }
 
  @Test
  public void testServer() {
   for (int i = 0; i < m_numberOfTimes; i++) {
     System.out.println(i);
    }
  }
}

我们只需要在testng.xml中配置工厂类,就能完成测试。

<class name="WebTestFactory" />

factory注解可以使用dataProvider属性,以构造方法的传递给测试类

public class WebTestChild extends WebTest {
	@Factory(dataProvider = "dp")
	public WebTestChild(int n) {
		super(n);
	}

	@DataProvider
	static public Object[][] dp() {
		return new Object[][] { new Object[] { 41 }, new Object[] { 42 }, };
	}
}

12、类级别注解

12.1、Test

可以将@Test批注放在类而不是测试方法上:

@Test
public class Test1 {
  public void test1() {
  }
 
  public void test2() {
  }
}

类级别的@Test注释的作用是使该类的所有公共方法都成为测试方法,即使它们没有被注释也是如此。 如果要添加某些属性,仍可以在方法上重复@Test批注。
例如:

@Test
public class Test1 {
  public void test1() {
  }
 
  @Test(groups = "g1")
  public void test2() {
  }
}

test1()test2()都是测试方法,但是test2属于g1

12.2、Ignore

@Ignore注释在功能上等效于@Test(enabled = false)@Ignore比各个@Test方法批注具有更高的优先级。 将@Ignore放在类上时,该类中的所有测试都将被禁用。

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;
 
@Ignore
public class TestcaseSample {
 
    @Test
    public void testMethod1() {
    }
 
    @Test
    public void testMethod2() {
    }
}

要忽略特定程序包中的所有测试,只需创建package-info.java并向其添加@Ignore批注。例如:

@Ignore
package com.testng.master;
 
import org.testng.annotations.Ignore;

这将导致在包com.testng.master及其所有子包中忽略所有@Test方法。

13、并发测试

13.1、parallel属性

标签上的parallel属性可以采用以下值之一:

<suite name="My suite" parallel="methods" thread-count="5">
<suite name="My suite" parallel="tests" thread-count="5">
<suite name="My suite" parallel="classes" thread-count="5">
<suite name="My suite" parallel="instances" thread-count="5">

parallel =“methods”TestNG将在单独的线程中运行所有测试方法。 依赖方法也将在单独的线程中运行,但是它们将遵循您指定的顺序。

parallel =“tests”TestNG将在同一线程中的同一标记中运行所有方法,但是每个标记将位于单独的线程中。 这样,您就可以将所有不是线程安全的类归入同一个中,并确保它们都将在同一线程中运行,同时利用TestNG使用尽可能多的线程来运行测试。

parallel =“classes”TestNG将在同一线程中运行同一类中的所有方法,但是每个类将在单独的线程中运行。

parallel =“instances”TestNG将在同一线程中的同一实例中运行所有方法,但是在两个不同实例中的两个方法将在不同线程中运行。

此外,属性thread-count允许您指定应为此执行分配多少个线程。

13.2、threadPoolSize

您还可以指定从不同的线程调用@Test方法。 您可以使用属性threadPoolSize来实现以下结果:

@Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
public void testServer() {

在此示例中,将从三个不同的线程调用函数testServer十次。 此外,十秒的超时保证没有任何线程将永远在该线程上阻塞。

14、重试机制

有时,我们可能希望TestNG在测试失败时自动重试。 在这种情况下,您可以使用重试分析器。 当您将重试分析器绑定到测试时,TestNG会自动调用重试分析器,以确定TestNG是否可以再次重试测试用例,以查看是否刚刚通过的测试现在通过。 这是使用重试分析器的方法:
构建接口org.testng.IRetryAnalyzer的实现
将此实现绑定到@Test注释,例如@Test(retryAnalyzer = LocalRetry.class)
以下是重试分析器的示例实现,该示例最多重试三次,加上第一次测试一共执行四次测试方法。

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
 
public class MyRetry implements IRetryAnalyzer {
 
  private int retryCount = 0;
  private static final int maxRetryCount = 3;
 
  @Override
  public boolean retry(ITestResult result) {
    if (retryCount < maxRetryCount) {
      retryCount++;
      return true;
    }
    return false;
  }
}
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class TestclassSample {
 
  @Test(retryAnalyzer = MyRetry.class)
  public void test2() {
    Assert.fail();
  }
}
1 操作
luojie 在 2020-08-06 17:37:48 更新了该帖
回帖
请输入回帖内容 ...