`

testng控制测试用例执行顺序及并发测试

阅读更多
1、class的执行顺序
       通过preserve-order属性,默认情况下preserve-order为true,即按照classes下class的顺序执行。
<code="java">
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="framework_testng"> 
    <test name="test" verbose="2" preserve-order="true">   
    <!--verbose="2" 标识的就是记录的日志级别,共有0-10的级别,其中0表示无,10表示最详细-->
        <classes> 
            <class name="com.demo.One"></class>
            <class name="com.demo.Two"></class>
            <class name="com.demo.Three"></class> 
        </classes> 
    </test> 
</suite>
</code>
       以上将按照类One Two Three的顺序执行,与设置preserve-order属性一样。
       以上面为例,当设置preserve-order属性为false时,则倒叙执行,按照类Three、Two、One的顺序执行
2、test方法的执行顺序
1)使用priority控制
<code="java">
@Test(priority = 0)
public void test001() {
}

@Test(priority = 1)
public void test002() {
}

@Test(priority = 2)
public void test003() {
}
</code>
       这样指定以后,将按照test001、test002、test003顺序执行。

2)使用方法命名控制
      执行顺序默认是按照方法名的字典顺序升序排序执行的,例如方法名命名为:A()  B()  C()
这样就会按照A B C的顺序进行执行。
3)在testng.xml中通过include控制
<code="java">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="framework_testng">
    <test name="test" preserve-order="true"> 
        <classes>
            <class name="com.demo.Yltest">
                <methods >
                   <include name="z" />
                   <include name="c" />
                   <include name="d" />
                   <include name="e" />
                   <include name="f" />
                   <include name="g" />
                   <include name="h" />
               </methods>
            </class>
        </classes>
    </test>
</suite>
</code>
      这样将按照 z c d e f g h的顺序执行。
      classes不设置include的时候,一个类中所有设置了@Test的方法都会执行,如果xml中设置了include,那么不在include中的测试方法即使有@Test也不会执行。

3、parallel参数
       这个参数为什么在这里介绍呢?因为在设置通过priority设置用例执行顺序时我发此参数有关,先来看看该参数介绍。
       paraller是执行的模式,有tests、classes、methods三种取值:
       1)tests:相同<test>标记下的所有类的方法在同一个线程中运行,不同的<test>下的方法会在不同的线程中运行。该值也是默认值。paraller=tests时,只要没有设置preserve-order=False,也没有设置priority,那么同一个<test>下的所有方法会先按照类的排列顺序执行类中的方法,同一个类中的方法会按照方法名字典升序排序后执行;如果有设置priority,那么会按照priority顺序执行。此时priority是对线程中所有方法生效的,即同一个线程中的多个类的所有方法按照priority顺序执行。如下例:
<code="java">
public class WaybillSelect {
@Test(priority = 0)
public void test1() {
System.out.println("this is test1");
}
@Test(priority = 1)
public void tester1() {
System.out.println("this is tester1");
}
        @Test(priority = 2)
public void test2() {
System.out.println("this is test2");
}
}

public class MoneySelectTest {
@Test(priority = 0)
public void test1() {
System.out.println("this is moneyselect.test1 method");
}
@Test(priority = 2)
public void beforesuitetest() {
         System.out.println("this is beforesuite test");
}
@Test(priority = 1)
public void beforetest() {
System.out.println("this is beforetest  test");
}
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!--  <suite name="zhanglutest" verbose="2" thread-count="2" parallel="classes"> -->
<suite name="zhanglutest" verbose="2" >
    <test name="testcase1">
    <classes>
            <class name="com.propertiesfile.WaybillSelect"></class>
            <class name="com.moneyselect.MoneySelectTest"></class>
        </classes>
    </test>
</suite>
</code>
执行结果如下(先执行所有priority为0的,依次为1、2):
this is test1
this is moneyselect.test1 method
this is tester1
this is beforetest  test
this is test2
this is beforesuite test
       在上面的基础上把两个类中的priority参数去掉,执行结果如下,可见是先按照类的先后顺序,在类里面按照方法名字典顺序排序后执行。
this is test1
this is test2
this is tester1
this is beforesuite test
this is beforetest  test
this is moneyselect.test1 method
       2)classes:TestNG 会在相同线程中运行相同类中的所有的方法,但是每个类都会用不同的线程运行。
       在上面代码的xml中设置parallel=classes
<code="java">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="zhanglutest" verbose="2" parallel="classes">
    <test name="testcase1">
    <classes>
            <class name="com.propertiesfile.WaybillSelect"></class>
            <class name="com.moneyselect.MoneySelectTest"></class>
        </classes>
    </test>
</suite>
两个类的代码不变,这里不重复粘贴了
</code>
       运行结果如下,两个类用两个线程执行,priority参数在各自的线程中生效,而两个线程的执行快慢我们无法掌控,所以打印结果如下,可以看出每个类中的方法先后顺序是按照各自的priority执行的,而两个类之间并不能保证哪个先执行哪个后执行。
this is moneyselect.test1 method
this is test1
this is tester1
this is beforetest  test
this is beforesuite test
this is test2
       注意,上面的xml中如果我设置了线程数,例如将线程数设置为1(thread-count="1"),那么所有的方法都在一个线程中执行,相当于parallel参数失效。而当classes下面有三个类,设置了parallel="classes" thread-count="2"时,会有两个类共用一个线程。thread-count默认为5。
       3)methods:TestNG 会在不同的线程中运行测试方法,除非那些互相依赖的方法。那些相互依赖的方法会运行在同一个线程中,并且遵照其执行顺序。

关于并发测试,这里不再贴出代码,附上网上的资料
https://blog.csdn.net/df0128/article/details/83792901

单个用例也可以由多个线程并发执行,只需要在@Test中声明,@Test(invocationCount = x,threadPoolSize = y),invocationCount表示执行次数,threadPoolSize表示线程池大小。
例子:
    /**
     * 多线程测试,没有关联的用例可以使用多线程,减少执行时间
     * 以下演示3个线程同时运行,共执行10次
     */
    @Test(invocationCount = 10,threadPoolSize = 3)
    public void test() {
           long id = Thread.currentThread().getId();
           System.out.printf("Thread Id :");
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics