---
title: "TestNG官方文档中文版(8)-类级别注解和并发"
author: "skydream"
date: "2008-09-05T08:09:00Z"
tags: []
slug: "testng-8"
draft: false
lastmod: "2009-08-02T11:16:06Z"
id: 437944
url: "https://skydream.iteye.com/blog/437944"
---

5.8 - Class level annotations

@Test注解可以放置在类上：

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;@Test  
public   class  Test1 {  
   public   void  test1() {  
  }

   public   void  test2() {  
  }  
}

类级别注解的效果是将这个类的所有的public方法都变成测试方法，即使他们没有被注解。还可以在需要增加属性的方法上重复@Test注解。

例如:

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;@Test  
public   class  Test1 {  
   public   void  test1() {  
  }

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

将方法test1()和test2()都变成测试方法，但是在此之上，test2()现在属于组"g1".

5.9 - Parallel running and time-outs

可以通过使用parallel属性要求TestNG在单独的线程中运行测试。这个属性可以在两个值中取其一：

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;&lt; suite  name ="My suite"  parallel ="methods"  thread-count ="5" &gt;

&lt; suite  name ="My suite"  parallel ="tests"  thread-count ="5" &gt;

    * parallel="methods": TestNG将在单独的线程中运行测试方法，除了那些依赖其他测试方法的，这些将在同一个线程中运行，以保证他们的执行顺序。

    * parallel="tests": TestNG将在一个线程中运行所有在同一个&lt;test&gt;标签中的测试方法，但是每个&lt;test&gt;标签将在单独的线程中运行。这种方式容许把所有不是线程安全的类分组到相同的&lt;test&gt;标签中，保证他们将在相同的线程中运行，有利于TestNG使用尽可能多的线程来运行测试。

    此外，thread-count属性容许指定运行时将分配多少线程。  
     
    注意：@Test的属性timeOut在并发和非并发模型下都可以工作。

    也可以指定@Test方法在不同的线程中被调用。可以使用threadPoolSize属性来实现这样的结果：

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;@Test(threadPoolSize  =   3 , invocationCount  =   10 ,  timeOut  =   10000 )  
public   void  testServer() {  
}

在这个例子中，方法testServer将被3个不同线程调用10次。此外，10秒种的time-out属性保证任何线程都不会长时间阻塞。