package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test
public void testB(){
System.out.println("B");
}
@Test
public void testA(){
System.out.println("A");
}
}
结果
A B调整后
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(enabled = false)
public void testB(){
System.out.println("B");
}
@Test
public void testA(){
System.out.println("A");
}
}
结果
A超时测试 timeOut
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(timeOut = 5000)
public void testA() throws InterruptedException {
Thread.sleep(4000);
System.out.println("A");
}
@Test(timeOut = 5000)
public void testB() throws InterruptedException {
Thread.sleep(6000);
System.out.println("B");
}
}
结果,第一条执行通过,第二条执行失败
A org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testB() didn't finish within the time-out 5000异常测试 exceptedException
我们先来写一个自己的Exception
package com.newcrud.learn;
public class MyException extends RuntimeException{
public MyException(String s){
super(s);
}
}
测试类
package com.newcrud.testngTest;
import com.newcrud.learn.MyException;
import org.testng.annotations.Test;
import java.io.IOException;
public class TestFour {
@Test(expectedExceptions = MyException.class)
public void testA() {
System.out.println("A");
throw new MyException("我的异常A");
}
@Test(expectedExceptions = RuntimeException.class)
public void testB() {
System.out.println("B");
throw new MyException("我的异常B,MyException继承了RuntimeException,属于RuntimeException");
}
@Test(expectedExceptions = IOException.class)
public void testC() {
System.out.println("C");
throw new MyException("我的异常C,RuntimeException和IOException都继承了Exception,所以MyException和IOException无从属关系");
}
@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class)
public void testD() {
System.out.println("D");
throw new MyException("我的异常D,ArrayIndexOutOfBoundsException继承了IndexOutOfBoundsException,IndexOutOfBoundsException继承了RuntimeException,所以和MyException无从属关系");
}
}
结果
A B C com.newcrud.learn.MyException: 我的异常C,RuntimeException和IOException都继承了Exception,所以MyException和IOException无从属关系 at com.newcrud.testngTest.TestFour.testC(TestFour.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) at org.testng.internal.Invoker.invokeMethod(Invoker.java:645) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:756) at org.testng.TestRunner.run(TestRunner.java:610) at org.testng.SuiteRunner.runTest(SuiteRunner.java:387) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) at org.testng.TestNG.runSuitesLocally(TestNG.java:1218) at org.testng.TestNG.runSuites(TestNG.java:1133) at org.testng.TestNG.run(TestNG.java:1104) at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66) at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109) D com.newcrud.learn.MyException: 我的异常D,ArrayIndexOutOfBoundsException继承了IndexOutOfBoundsException,IndexOutOfBoundsException继承了RuntimeException,所以和MyException无从属关系 at com.newcrud.testngTest.TestFour.testD(TestFour.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) at org.testng.internal.Invoker.invokeMethod(Invoker.java:645) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:756) at org.testng.TestRunner.run(TestRunner.java:610) at org.testng.SuiteRunner.runTest(SuiteRunner.java:387) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) at org.testng.TestNG.runSuitesLocally(TestNG.java:1218) at org.testng.TestNG.runSuites(TestNG.java:1133) at org.testng.TestNG.run(TestNG.java:1104) at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66) at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)分组测试 groups
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(groups = "ten")
public void testF() {
System.out.println("F");
}
@Test(groups = "ten")
public void testG() {
System.out.println("G");
}
@Test(groups = "eleven")
public void testI() {
System.out.println("I");
}
@Test(groups = {"eleven","ten"})
public void testK() {
System.out.println("K");
}
}
groups真正的用途不在 定义,而在于依赖使用,下面的dependsOnGroups
依赖分组测试 dependsonGroupspackage com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(dependsonGroups = {"ten"})
public void testA(){
System.out.println("A");
}
@Test(dependsonGroups = {"eleven"})
public void testB(){
System.out.println("B");
}
@Test(dependsonGroups = {"eleven","ten"})
public void testC(){
System.out.println("C");
}
@Test(groups = "ten")
public void testF() {
System.out.println("F");
}
@Test(groups = "ten")
public void testG() {
System.out.println("G");
}
@Test(groups = "eleven")
public void testI() {
System.out.println("I");
}
@Test(groups = {"eleven","ten"})
public void testK() {
System.out.println("K");
}
}
结果,可以看到并没有按照ABCD这样的顺序,而是因为在ABC由于有了dependsOnGroups的存在,导致了执行顺序反而在依赖的groups执行完之后才开始执行的
F G I K A B C
如果被依赖的groups未执行后者未全部执行或者执行失败或者全部失败会怎么样呢
被依赖的groups部分未执行testC依赖了groups的ten,但是ten有两个方法,其中一个被enable=false不执行了
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(dependsonGroups = {"ten","eleven"})
public void testC(){
System.out.println("C");
}
@Test(groups = "ten",enabled = false)
public void testF() {
System.out.println("F");
}
@Test(groups = "ten")
public void testG() {
System.out.println("G");
}
@Test(groups = "eleven")
public void testI() {
System.out.println("I");
}
}
结果,正常执行,只有被enable=false的testF未执行,并不会影响testC的执行
G I C被依赖的groups全部未执行
这次我们把groups为ten的全部忽略执行
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(dependsonGroups = {"ten","eleven"})
public void testC(){
System.out.println("C");
}
@Test(groups = "ten",enabled = false)
public void testF() {
System.out.println("F");
}
@Test(groups = "ten",enabled = false)
public void testG() {
System.out.println("G");
}
@Test(groups = "eleven")
public void testI() {
System.out.println("I");
}
}
结果,发现报错了,testI也没执行
org.testng.TestNGException: DependencyMap::Method "TestFour.testC()[pri:0, instance:com.newcrud.testngTest.TestFour@7c75222b]" depends on nonexistent group "ten" at org.testng.DependencyMap.getMethodsThatBelongTo(DependencyMap.java:43) at org.testng.TestRunner.createDynamicGraph(TestRunner.java:1054) at org.testng.TestRunner.privateRun(TestRunner.java:723) at org.testng.TestRunner.run(TestRunner.java:610) at org.testng.SuiteRunner.runTest(SuiteRunner.java:387) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) at org.testng.TestNG.runSuitesLocally(TestNG.java:1218) at org.testng.TestNG.runSuites(TestNG.java:1133) at org.testng.TestNG.run(TestNG.java:1104) at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66) at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109) Test ignored.被依赖的groups部分执行失败
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(dependsonGroups = {"ten","eleven"})
public void testC(){
System.out.println("C");
}
@Test(groups = "ten",timeOut = 5000)
public void testF() throws InterruptedException {
Thread.sleep(4000);
System.out.println("F");
}
@Test(groups = "ten",timeOut = 5000)
public void testG() throws InterruptedException {
Thread.sleep(6000);
System.out.println("G");
}
@Test(groups = "eleven")
public void testI() {
System.out.println("I");
}
}
结果,发现testC被忽略执行了。
F org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testG() didn't finish within the time-out 5000 at java.lang.Throwable.toString(Throwable.java:481) at java.lang.Throwable.被依赖的groups全部执行失败(Throwable.java:312) at java.lang.Exception. (Exception.java:102) at java.lang.RuntimeException. (RuntimeException.java:96) at org.testng.internal.InvokeMethodRunnable$TestNGRuntimeException. (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) I Test ignored.
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(dependsonGroups = {"ten","eleven"})
public void testC(){
System.out.println("C");
}
@Test(groups = "ten",timeOut = 5000)
public void testF() throws InterruptedException {
Thread.sleep(6000);
System.out.println("F");
}
@Test(groups = "ten",timeOut = 5000)
public void testG() throws InterruptedException {
Thread.sleep(6000);
System.out.println("G");
}
@Test(groups = "eleven")
public void testI() {
System.out.println("I");
}
}
结果,和部分执行失败一样,也是对应的testC未执行
org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testF() didn't finish within the time-out 5000 org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testG() didn't finish within the time-out 5000 at java.lang.Throwable.综上所述(Throwable.java:265) at java.lang.Exception. (Exception.java:66) at java.lang.InterruptedException. (InterruptedException.java:67) at java.lang.Thread.sleep(Native Method) at com.newcrud.testngTest.TestFour.testG(TestFour.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:54) 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) I Test ignored.
被依赖的groups里的方法,只要不是全部忽略执行,并不影响依赖了改groups方法的执行。
但是
只要被忽略的groups里的方法,有一个执行失败了,依赖了该groups的方法就会被忽略执行
依赖方法测试 dependsonMethods和dependsOnGroups类似,只不过并不是依赖groups,而是依赖某一个具体的方法。
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(dependsonMethods = {"testC"})
public void testA(){
System.out.println("A");
}
@Test(dependsonMethods = {"testC","testD"})
public void testB(){
System.out.println("B");
}
@Test
public void testC(){
System.out.println("C");
}
@Test
public void testD(){
System.out.println("D");
}
}
执行结果,(优先级是高于dependsOnGroups的),这段代码就不演示了
C D A B
那么,如果被依赖的方法失败了或者部分失败了或者未执行或者部分未执行怎么办
被依赖的方法部分未执行package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(dependsonMethods = {"testC"})
public void testA(){
System.out.println("A");
}
@Test(dependsonMethods = {"testC","testD"})
public void testB(){
System.out.println("B");
}
@Test
public void testC(){
System.out.println("C");
}
@Test(enabled = false)
public void testD(){
System.out.println("D");
}
}
结果:报错
org.testng.TestNGException: com.newcrud.testngTest.TestFour.testB() is depending on method public void com.newcrud.testngTest.TestFour.testD(), which is not annotated with @Test or not included. at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:109) at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:208) at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:284) at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:57) at org.testng.TestRunner.initMethods(TestRunner.java:474) at org.testng.TestRunner.init(TestRunner.java:252) at org.testng.TestRunner.init(TestRunner.java:222) at org.testng.TestRunner.被依赖的方法全部未执行(TestRunner.java:163) at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:585) at org.testng.SuiteRunner.init(SuiteRunner.java:189) at org.testng.SuiteRunner. (SuiteRunner.java:136) at org.testng.TestNG.createSuiteRunner(TestNG.java:1375) at org.testng.TestNG.createSuiteRunners(TestNG.java:1355) at org.testng.TestNG.runSuitesLocally(TestNG.java:1209) at org.testng.TestNG.runSuites(TestNG.java:1133) at org.testng.TestNG.run(TestNG.java:1104) at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66) at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(dependsonMethods = {"testC"})
public void testA(){
System.out.println("A");
}
@Test
public void testB(){
System.out.println("B");
}
@Test(enabled = false)
public void testC(){
System.out.println("C");
}
@Test(enabled = false)
public void testD(){
System.out.println("D");
}
}
结果
org.testng.TestNGException: com.newcrud.testngTest.TestFour.testA() is depending on method public void com.newcrud.testngTest.TestFour.testC(), which is not annotated with @Test or not included. at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:109) at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:208) at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:284) at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:57) at org.testng.TestRunner.initMethods(TestRunner.java:474) at org.testng.TestRunner.init(TestRunner.java:252) at org.testng.TestRunner.init(TestRunner.java:222) at org.testng.TestRunner.被依赖的方法部分执行失败(TestRunner.java:163) at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:585) at org.testng.SuiteRunner.init(SuiteRunner.java:189) at org.testng.SuiteRunner. (SuiteRunner.java:136) at org.testng.TestNG.createSuiteRunner(TestNG.java:1375) at org.testng.TestNG.createSuiteRunners(TestNG.java:1355) at org.testng.TestNG.runSuitesLocally(TestNG.java:1209) at org.testng.TestNG.runSuites(TestNG.java:1133) at org.testng.TestNG.run(TestNG.java:1104) at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66) at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(dependsonMethods = {"testC"})
public void testA(){
System.out.println("A");
}
@Test(dependsonMethods = {"testC","testD"})
public void testB(){
System.out.println("B");
}
@Test(timeOut = 5000)
public void testC() throws InterruptedException {
Thread.sleep(4000);
System.out.println("C");
}
@Test(timeOut = 5000)
public void testD() throws InterruptedException {
Thread.sleep(6000);
System.out.println("D");
}
}
结果,testB被忽略执行了,原因是它依赖的D报错了
C org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testD() didn't finish within the time-out 5000 at java.lang.Thread.interrupt(Thread.java:919) at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:64) 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) A Test ignored.被依赖的方法全部执行失败
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test
public void testE(){
System.out.println("E");
}
@Test(dependsonMethods = {"testC","testD"})
public void testB(){
System.out.println("B");
}
@Test(timeOut = 5000)
public void testC() throws InterruptedException {
Thread.sleep(6000);
System.out.println("C");
}
@Test(timeOut = 5000)
public void testD() throws InterruptedException {
Thread.sleep(6000);
System.out.println("D");
}
}
结果,testB被忽略执行了。
org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testC() didn't finish within the time-out 5000 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1157) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) org.testng.internal.thread.ThreadTimeoutException: Method com.newcrud.testngTest.TestFour.testD() didn't finish within the time-out 5000 at java.lang.Throwable.fillInStackTrace(Native Method) at java.lang.Throwable.fillInStackTrace(Throwable.java:784) at java.lang.Throwable.综上所述(Throwable.java:266) at java.lang.Exception. (Exception.java:66) at java.lang.InterruptedException. (InterruptedException.java:67) at java.lang.Thread.sleep(Native Method) at com.newcrud.testngTest.TestFour.testD(TestFour.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:54) 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) E Test ignored.
被依赖的方法未执行,是会直接报错的,程序中断
但是
被依赖的方法报错,并不会中断程序,而是忽略执行了。
优先级 priority 调整前执行顺序不等于书写的顺序,比如我先写的testB后写的testA,并不会先执行testB,而是按照字母的顺序来执行的,比如testB和testA,前面有相同的test,那就对比B和A的顺序,A在前,先执行testA
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test
public void testB(){
System.out.println("B");
}
@Test
public void testA(){
System.out.println("A");
}
}
结果
A B调整后
数字越小,优先级越高
package com.newcrud.testngTest;
import org.testng.annotations.Test;
public class TestFour {
@Test(priority = 1)
public void testB(){
System.out.println("B");
}
@Test(priority = 2)
public void testA(){
System.out.println("A");
}
}
结果
B A参数化之 @Paramters
emmmm,其实个人感觉@Paramters并不如DataProvider好用,不过既然学习嘛,那我们就来看看@Paramters的使用方法吧
只有一个参数 第一步,在测试类上添加@Paramters@Parameters注解里的内容不必和接口的参数名保持一致,它是按照顺序一一对应的
package com.newcrud.testngTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestFour {
@Parameters({"name-A"})
@Test
public void testE(String name){
System.out.println(name);
}
}
第二步,在xml文件上为参数赋值
结果
执行xml文件
已经提取到了我们在xml配置的文件
zhangsan有两个参数 第一步,在测试类上添加@Paramters
package com.newcrud.testngTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestFour {
@Parameters({"name-A","id-A"})
@Test
public void testE(String name,Integer id){
System.out.println(name);
System.out.println(id);
}
}
第二步,在xml文件上为参数赋值
结果
执行xml文件
zhangsan 99使用默认值
那如果说我担心忘记在xml上面配置parameter了,想要在测试类上加一个默认值怎么办
package com.newcrud.testngTest;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestFour {
@Parameters({"name-A","id-A"})
@Test
public void testE(@Optional("wing") String name, @Optional("77") Integer id){
System.out.println(name);
System.out.println(id);
}
}
xml文件
结果
wing 77
发现,emmm,@Optional里必须得是String,所以int类型的参数,你在里面传成了"77",也是可以正常识别的。



