TestNg的xml文件编写

TestNg的xml文件编写

time-out 在本测试中的所有测试方法上使用的默认超时。

那么这个超时时间和注解上的超时时间哪个优先级更高呢,时间出真知,经过验证,在已经有超时注解的方法上,以方法上的为准,在没有超时注解的方法上,会默认以xml文件上的为准。下面是验证过程。

下面这个java类也是后续的测试类,如果有修改会特殊标注出来

package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour  {
    @Test(timeOut = 4000)
    public void testZ() throws InterruptedException {
        System.out.println("Z");
        Thread.sleep(5000);
    }
    @Test(timeOut = 6000)
    public void testY() throws InterruptedException {
        System.out.println("Y");
        Thread.sleep(5000);
    }
    @Test
    public void testA() throws InterruptedException {
        System.out.println("A");
        Thread.sleep(4000);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="/Users/zc/IdeaProjects/NewCRUD/src/test/java/com/newcrud" time-out="3000">
        <classes>
            <class name="com.newcrud.testngTest.TestFour"></class>
        </classes>
    </test>
</suite>

结果

A

org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testA() didn't finish within the time-out 3000

	at java.util.concurrent.FutureTask.run(FutureTask.java:271)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

Y
Z

org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testZ() didn't finish within the time-out 4000

	at java.lang.Throwable.fillInStackTrace(Native Method)
	at java.lang.Throwable.fillInStackTrace(Throwable.java:784)
	at java.lang.Throwable.<init>(Throwable.java:311)
	at java.lang.Exception.<init>(Exception.java:102)
	at java.lang.RuntimeException.<init>(RuntimeException.java:96)
	at org.testng.internal.InvokeMethodRunnable$TestNGRuntimeException.<init>(InvokeMethodRunnable.java:80)
	at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:61)
	at org.testng.internal.InvokeMethodRunnable.run(InvokeMethodRunnable.java:44)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

PASSED: testY
FAILED: testA
org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testA() didn't finish within the time-out 3000
	at java.util.concurrent.FutureTask.run(FutureTask.java:271)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

FAILED: testZ
org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testZ() didn't finish within the time-out 4000
	at java.lang.Throwable.fillInStackTrace(Native Method)
	at java.lang.Throwable.fillInStackTrace(Throwable.java:784)
	at java.lang.Throwable.<init>(Throwable.java:311)
	at java.lang.Exception.<init>(Exception.java:102)
	at java.lang.RuntimeException.<init>(RuntimeException.java:96)
	at org.testng.internal.InvokeMethodRunnable$TestNGRuntimeException.<init>(InvokeMethodRunnable.java:80)
	at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:61)
	at org.testng.internal.InvokeMethodRunnable.run(InvokeMethodRunnable.java:44)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

运行包

以包为单位来运行用例

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="/Users/zc/IdeaProjects/NewCRUD/src/test/java/com/newcrud" time-out="3000">
        <packages>
            <package name="com.newcrud.testngTest"></package>
            <package name="com.newcrud.controller"></package>
        </packages>
    </test>
</suite>

运行class

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="/Users/zc/IdeaProjects/NewCRUD/src/test/java/com/newcrud" time-out="3000">
        <classes>
            <class name="com.newcrud.testngTest.TestFour"></class>
            <class name="com.newcrud.testngTest.TestThree"></class>
        </classes>
    </test>
</suite>

运行methods+指定排除或者包括某一个方法

include

即为只运行include内的方法

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="/Users/zc/IdeaProjects/NewCRUD/src/test/java/com/newcrud" time-out="3000">
        <classes>
            <class name="com.newcrud.testngTest.TestFour">
                <methods>
                    <include name="testY"></include>
                </methods>
            </class>
            <class name="com.newcrud.testngTest.TestThree"></class>
        </classes>
    </test>
</suite>

exclude

除了exclude之外的,都执行

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="/Users/zc/IdeaProjects/NewCRUD/src/test/java/com/newcrud" time-out="3000">
        <classes>
            <class name="com.newcrud.testngTest.TestFour">
                <methods>
                    <exclude name="testY"></exclude>
                </methods>
            </class>
            <class name="com.newcrud.testngTest.TestThree"></class>
        </classes>
    </test>
</suite>

至于同时存在include和exclude的情况,请自己试一下,我试了的结果是,完全没必要一起存在

指定要包括或排除某个分组

准备两个测试类

package com.newcrud.testngTest;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestThree {
    @Test(groups = "men")
    public void testF(){
        System.out.println("F");
    }
    @Test(groups = "men")
    public void testG(){
        System.out.println("G");
    }
    @Test(groups = "women")
    public void testH(){
        System.out.println("H");
    }
    @Test(groups = "women")
    public void testI(){
        System.out.println("I");
    }
    @Test(groups = {"women","men"})
    public void testJ(){
        System.out.println("J");
    }
}
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour  {
    @Test(groups = "men")
    public void testA(){
        System.out.println("A");
    }
    @Test(groups = "men")
    public void testB(){
        System.out.println("B");
    }
    @Test(groups = "women")
    public void testC(){
        System.out.println("C");
    }
    @Test(groups = "women")
    public void testD(){
        System.out.println("D");
    }
    @Test(groups = {"women","men"})
    public void testE(){
        System.out.println("E");
    }
}

include

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="/Users/zc/IdeaProjects/NewCRUD/src/test/java/com/newcrud" time-out="3000">
        <groups>
            <run>
                <include name="men"></include>
            </run>
        </groups>
        <classes>
            <class name="com.newcrud.testngTest.TestFour"></class>
            <class name="com.newcrud.testngTest.TestThree"></class>
        </classes>
    </test>
</suite>

结果

A
B
E
F
G
J
PASSED: testA
PASSED: testB
PASSED: testE
PASSED: testF
PASSED: testG
PASSED: testJ

exclude

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="/Users/zc/IdeaProjects/NewCRUD/src/test/java/com/newcrud" time-out="3000">
        <groups>
            <run>
                <exclude name="men"></exclude>
            </run>
        </groups>
        <classes>
            <class name="com.newcrud.testngTest.TestFour"></class>
            <class name="com.newcrud.testngTest.TestThree"></class>
        </classes>
    </test>
</suite>

结果

C
D
H
I
PASSED: testC
PASSED: testD
PASSED: testH
PASSED: testI
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值