【问题标题】:Doing multi threaded unit test with testng使用 testng 进行多线程单元测试
【发布时间】:2016-12-05 11:44:18
【问题描述】:

我正在学习使用 TestNg 进行单元测试。我想将变量“val”的唯一值传递给线程池中的每个线程,但它没有拾取它。

这里是 testng 类:

public class NewTest {

    int val = 0;

  /*@Test(dataProvider = "dp")
  public void f(Integer n, String s) {
  }*/
  @BeforeMethod
  public void beforeMethod() {
      long id = Thread.currentThread().getId();
      System.out.println("beforeMethod. Thread id is: " + id);



  }

  @AfterMethod
  public void afterMethod() {/*
      long id = Thread.currentThread().getId();
      System.out.println("After test-method. Thread id is: " + id);*/
  }


  @DataProvider
  public Object[][] dp() {
    return new Object[][] {
      new Object[] { 1, "a" },
      new Object[] { 2, "b" },
    };
  }
  @BeforeClass
  public void beforeClass() {

  }

  @AfterClass
  public void afterClass() {
  }

  @BeforeTest
  public void beforeTest() {
      val++;
  }

  @AfterTest
  public void afterTest() {
  }

  @BeforeSuite
  public void beforeSuite() {
  }

  @AfterSuite
  public void afterSuite() {
  }

  @Test(threadPoolSize = 5, invocationCount = 5, timeOut = 1000)
  public void methodOne(){
      System.out.println("Value of val from MethodOne::"+val);
  }
}

和输出:

[ThreadUtil] 启动执行器timeOut:1000ms workers:5 threadPoolSize:5 beforeMethod。线程 id 为:15 beforeMethod。线 id 是:12 beforeMethod。线程 id 为:14 beforeMethod。线程 ID 为: 13 之前的方法。线程 id 是:16 来自 MethodOne::1 值的 val 值 MethodOne::1 中的 val 值 MethodOne::1 中的 val 值 来自 MethodOne::1 val 的值来自 MethodOne::1 PASSED: methodOne 通过:methodOne 通过:methodOne 通过:methodOne 通过: 方法一

================================================ 默认测试

测试运行:5,失败:0,跳过:0

================================================默认套件总测试运行:5,失败:0,跳过:0

[TestNG] [FailedReporter pass=0 failed=0 skipped=0] 花费的时间:1 ms [TestNG] 所用时间 org.testng.reporters.SuiteHTMLReporter@3159c4b8:50 毫秒 [TestNG] 时间 由 org.testng.reporters.JUnitReportReporter@6adede5 拍摄:7 毫秒 [TestNG] org.testng.reporters.XMLReporter@64bf3bbf 花费的时间:9 毫秒 [TestNG] org.testng.reporters.jq.Main@1d16f93d 花费的时间:40 毫秒 [TestNG] 所用时间 org.testng.reporters.EmailableReporter2@5bc79255:4 毫秒

【问题讨论】:

    标签: java junit testng


    【解决方案1】:

    注意:@BeforeTest 不是 @BeforeMethod@BeforeSuite -> @BeforeTest -> @BeforeClass -> @BeforeMethod@BeforeGroup 是一个特定的)。

    那么,@BeforeTest 仅被 <test> 调用一次:在您的示例中,仅调用一次,val 始终为 1。

    【讨论】:

    • 我更改了 @BeforeMethod 中的增量步骤,但仍然得到相同的输出。
    • 您能用更新后的示例更新您的问题吗?
    【解决方案2】:

    您可以利用ThreadLocal,如下所示:(但请记住,TestNG 保证只有@BeforeMethod > @Test > @AfterMethod 带注释的方法会在同一线程上运行。

    public class NewTest {
        private static ThreadLocal<Long> val = new ThreadLocal<>();
    
        @BeforeMethod
        public void beforeMethod() {
            long id = Thread.currentThread().getId();
            val.set(id);
            printer("beforeMethod");
        }
    
        @AfterMethod
        public void afterMethod() {
            printer("afterMethod");
            //usage of thread locale done. Lets reset it.
            val.set(null);
        }
    
        @Test (threadPoolSize = 5, invocationCount = 5, timeOut = 1000)
        public void methodOne() {
            printer("methodOne");
        }
    
        private static void printer(String prefix) {
            System.err.println("Thread ID in the method " + prefix + "() is " + val.get());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-30
      • 2019-01-01
      相关资源
      最近更新 更多