TestNG运行时参数说明

TestNG运行的各种参数

本文基于testNG官网文档解析+自身实践说明。testNG运行时的常用参数有如下几个:

  • thread-count
  • parallel
  • verbose
  • time-out

从字面理解也能大概看出是什么意思。为了方便理解,我们先来看一个完整的xml
下面这个是我自己平时项目用到的xml

<suite  thread-count="10" parallel="" verbose="1"
    name="simpleTest"   annotations="JDK" time-out="1800000">
    <parameter name="browser" value="chrome" />
    <listeners>
        <listener class-name="com.mytestng.utils.testngListener"></listener>
        <listener class-name="com.mytestng.utils.myReport"></listener>
    </listeners>
    <test name="simpleTest" junit="false" annotations="JDK">

        <classes>
            <class name="com.mytestng.test.runner.cardListTest" />
            <class name="com.mytestng.test.runner.wsConfigTest" />
            <class name="com.mytestng.test.runner.planTest" />              
            <class name="com.mytestng.test.runner.quickCreateTest" />
        </classes>
    </test>
</suite>

thread-count

这个参数是指testNG运行时的线程池的大小。当启用了parallel时生效。具体的大小可以从xml文件中指定。若不指定,默认为10.启用此参数后可以大幅提高测试用例执行的速度

parallel

  1. parallel=”methods” 按照method级别进行并发,即便是配置了依赖跟顺序的也会用不同的线程运行,但是会按照顺序。
  2. parallel=”tests” 按照xml定义的< test > 来运行。
  3. parallel=”classes” 按照类来并发运行
  4. parallel=”instances” 这个是按照实例来并发运行(这个不常用)

verbose

其实就是日志的冗余级别。tesng官方文档 数字越大,表示日志打的越细致。这个如果是要对testng进行重写,或者扩展,做调试的时候还是很有用的。

来点实际的。我们做个示例,看看究竟是不是这样
先来看看verbose

我们先配置成0:

<suite reruntimes="2" thread-count="10" parallel="tests" verbose="0" name="demo" annotations="JDK" time-out="1800000">
@Test()
    public void doTest() {
        System.out.println("this is the verbose parameter test!");
    }

运行,看看结果。
0级别的日志。就是0冗余:
this is the verbose parameter test!
console只是输出了测试用例自己的输出

我们调整一下。调整到10.看一下会有什么样的变化:

...
... TestNG 6.8.6beta_20130517_2142 by Cédric Beust (cedric@beust.com)
...

[TestRunner] Running the tests in 'TestNgClone' with parallel mode:tests
[RunInfo] Adding method selector: org.testng.internal.XmlMethodSelector@d64342 priority: 10
[TestClass] Creating TestClass for [ClassImpl class=com.netease.demo.verbose]
[TestClass] Adding method verbose.doTest()[pri:0, instance:null] on TestClass class com.netease.demo.verbose
[XmlMethodSelector] Including method com.netease.demo.doBeforeClass()
[XmlMethodSelector] Including method com.netease.demo.doTest()

由于日志很多,就没截全。可以看到,几乎每一步的操作都打印出来了。所以通常这个级别,我建议都是调整成5.这样查找问题会比较方便。

来继续看看并发运行的参数,我们继续调整一下。同时,将测试代码更新一下,加到10个测试
为了不受干扰,我们日志级别调整到最低

<suite reruntimes="2" thread-count="10" parallel="methods" verbose="0" name="demo" annotations="JDK" time-out="1800000">

然后测试方法的代码调整下,打印出线程的名称:

    @Test()
    public void doTest7() {
        System.out.println(Thread.currentThread().getName()+":"+System.currentTimeMillis());
    }

结果如下:

TestNGInvoker-doTest2():1495786491980
TestNGInvoker-doTest3():1495786491981
TestNGInvoker-doTest10():1495786491984
TestNGInvoker-doTest1():1495786491984
TestNGInvoker-doTest7():1495786491984
TestNGInvoker-doTest6():1495786491985
TestNGInvoker-doTest9():1495786491986
TestNGInvoker-doTest4():1495786491987
TestNGInvoker-doTest8():1495786491987
TestNGInvoker-doTest5():1495786491988

可以看到,几乎是同时发起的。当然因为我的电脑比较渣,同时的比较少。而且这里也可以看到,如果没有配置test运行的顺序,使用了并发的方式执行,执行的顺序是无法保证的。

下边我们再改一下,看看是否可以控制他的顺序:

@Test(priority=1..10)
TestNGInvoker-doTest8():1495786748387
TestNGInvoker-doTest1():1495786748388
TestNGInvoker-doTest7():1495786748390
TestNGInvoker-doTest5():1495786748391
TestNGInvoker-doTest2():1495786748392
TestNGInvoker-doTest4():1495786748392
TestNGInvoker-doTest10():1495786748393
TestNGInvoker-doTest3():1495786748393
TestNGInvoker-doTest6():1495786748394
TestNGInvoker-doTest9():1495786748394

事实证明,priorty参数,无法控制执行的顺序,那换成dependonmethods呢?

@Test(dependsOnMethods={"doTest1"})

TestNGInvoker-doTest1():1495787138612
TestNGInvoker-doTest2():1495787138620
TestNGInvoker-doTest3():1495787138624
TestNGInvoker-doTest4():1495787138626
TestNGInvoker-doTest5():1495787138629
TestNGInvoker-doTest6():1495787138633
TestNGInvoker-doTest7():1495787138636
TestNGInvoker-doTest8():1495787138640
TestNGInvoker-doTest9():1495787138643
TestNGInvoker-doTest10():1495787138646

成功了。但是如果只是这样。那我的多线程有何用呢?那还不是相当于1个线程在执行?
当然,别忘了,并发是有多种模式的,methods是最小的一个粒度。还可以是tests或者
classes。在其他模式下,还是可以有不小的提升。

更多的用法,可以参考testng的官网文档,然后自己动手实践一下。
[1]testng官网
[2]testngListenerAPIs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值