testng学习-before&after,parameters,并行,factory,beanshell,监听器,依赖注入

一系列的before after的操作测试

[TestNG] Running:
C:\Users\user\AppData\Local\Temp\testng-eclipse-1538841744\testng-customsuite.xml class1-@BeforeSuite
Class2-@BeforeSuite
NewTest1-@BeforeSuite
pck1_-@BeforeSuite
pck2-@BeforeSuite
class1-@BeforeTest
Class2-@BeforeTest
NewTest1-@BeforeTest
pck1_-@BeforeTest
pck2-@BeforeTest
pck1_-@BeforeClass
pck1_-@BeforeMethod
testpck1_
pck1_-@AfterMethod
pck1_-@BeforeMethod
testpck1_
pck1_-@AfterMethod
pck1_-@AfterClass
pck2-@BeforeClass
pck2-@BeforeMethod
testpck2
pck2-@AfterMethod
pck2-@BeforeMethod
testpck2
pck2-@AfterMethod
pck2-@AfterClass
Class2-@BeforeClass
Class2-@BeforeMethod
testClass2
Class2-@AfterMethod
Class2-@BeforeMethod
testClass2
Class2-@AfterMethod
Class2-@AfterClass
NewTest1-@BeforeClass
NewTest1-@BeforeMethod
test1-----testpck1_
NewTest1-@AfterMethod
NewTest1-@BeforeMethod
test1-----testpck1_
NewTest1-@AfterMethod
NewTest1-@BeforeMethod
test2-----testpck1_
NewTest1-@AfterMethod
NewTest1-@BeforeMethod
test2-----testpck1_
NewTest1-@AfterMethod
NewTest1-@AfterClass
class1-@BeforeClass
class1-@BeforeMethod
testClass1
class1-@AfterMethod
class1-@BeforeMethod
testClass1
class1-@AfterMethod
class1-@AfterClass
class1-@AfterTest
Class2-@AfterTest
NewTest1-@AfterTest
pck1_-@AfterTest
pck2-@AfterTest
PASSED: f(1, "a")
PASSED: f(2, "b")
PASSED: f(1, "a")
PASSED: f(2, "b")
PASSED: f(1, "a")
PASSED: f(2, "b")
PASSED: f1(1, "a")
PASSED: f1(2, "b")
PASSED: f2(1, "a")
PASSED: f2(2, "b")
PASSED: f(1, "a")
PASSED: f(2, "b") ===============================================
Default test
Tests run: 12, Failures: 0, Skips: 0
=============================================== class1-@AfterSuite
Class2-@AfterSuite
NewTest1-@AfterSuite
pck1_-@AfterSuite
pck2-@AfterSuite ===============================================
Default suite
Total tests run: 12, Failures: 0, Skips: 0
=============================================== [TestNG] Time taken by org.testng.reporters.EmailableReporter2@694e1548: 16 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@7f13d6e: 219 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@4e7dc304: 92 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@184f6be2: 47 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2a742aa2: 297 ms

由操作日志可以得出:

  按照时间线排列

1.before suite ,在多个class中写了,在执行的时候,会把每个class的beforeSuite先执行

2.beforeTest ,在多个class中写了,在执行的时候,会把每个class的beforeTest执行

3.beforeClass,在不同的class中写了,在执行的时候,会在对应class的beforeClass执行

4.beforeMethod,一个class中,写了好几个@test,在执行的时候,会在每个@test用例执行beforeMethod

5.AfterMethod,该class中,每个@test执行完后,都执行AfterMethod

6.AfterClass,在每个class执行完后,执行对应class的AfterClass

7.AfterTest,在所有class执行完后,执行所有的AfterTest

8.AfterSuite在测试结果已经得出,测试已经完成了,执行所有的AfterSuite

疑问:AfterTest和AfterSuite到底有啥差别?

parameters

parameters 在使用两个参数时,一直报错:

参考下 https://www.cnblogs.com/meitian/p/5199079.html

结论:

1.parameter写了之后,在xml里面添加值,执行也必须使用xml执行;

2.@parameter和@Test 谁前谁后并不重要;

3.调用的方法如果涉及传参,似乎要很注意,我这里卡住了。。。

写了个测试demo

public class test {
@Test
@Parameters({"username","password"})
public void test (String username ,String password){
System.out.println("username is :"+username+",password is "+password);
}
}

  直接执行这个class,会出现报错

Parameter 'username' is required by @Test on method test but has not been marked @Optional or defined
in C:\Users\user\AppData\Local\Temp\testng-eclipse-63400206\testng-customsuite.xml

执行testng.xml

执行顺畅:

username is :test1994,password is qwer1234

testng并行:

  当前暂时用不上,以后可用于参考:

    https://blog.csdn.net/wanghantong/article/details/40394625

testng的@Factory注解

  如果用例使用的数据有一定的规律,可以用程序实现,就可以用factory对数据进行初始化赋值,交给test执行。  

  factory注解相关   https://www.cnblogs.com/silence-hust/p/4539363.html

  dataprovider和factory的区别:

    -factory用程序实现给test赋值

    -dataprovider是存储的数据,可以给factory,也可以给test

  dataprovider和factory的混用  https://blog.csdn.net/wanghantong/article/details/39271015

  示例:

    测试数据提供的测试账号,是test5~test10,test1994~test1998.

    如果只用dataprovider来提供数据,就需要写一大串的测试账号,如果使用dataprovider和factory,只需要用以下的代码解决

 

public class TestngFactory {
@Factory (dataProvider="account_Name")
//这个方法是factory创建测试实例数组的方法,并调用dataprovider的数据
public Object[] createInstances(int a,int b) {
System.out.println("a:"+a+",b:"+b);
int sub=Math.abs(a-b)+1;
Object[]result = new Object[sub]; for(int i = 0; i <sub; i++) {
String iString =String.valueOf(a+i);
          //这个方法返回测试用例,并给测试用例传参
result[i] = new FactoryTest("test"+iString);
}
return result;
}
@DataProvider(name = "account_Name")
public Object[][] accountName(){
      //设置数据来源,提供给factory使用
return new Object[][]{{new Integer(1994),new Integer(1998)},{new Integer(5),new Integer(10)}};
}
}

  

public class FactoryTest {
private String accountName;
        //测试用例类的构造方法,用于初始化测试类,并提供数据
public FactoryTest(String accountName) {
this.accountName = accountName;
}
@Test
        //测试用例,使用数据
public void testServer() {
System.out.println("accountName :"
+ accountName);
}
}

 

testng+beanshell

  不懂是什么,我记得jmeter里面beanshell是用来断言的,但是testng的,我看很多博客都是说是用来选择执行的用例的。

  jmeter相关https://blog.csdn.net/cyjs1988/article/details/79715592

  有待理解。 

testng监听器

  https://www.ibm.com/developerworks/cn/opensource/os-cn-testinglistener/index.html

  在学习extentsreports美化测试报告,里面很多用到了两个监听器:IReport ,TestListenerAdapter

  两者的用途:前者是用来出报告,后者用来监听测试的状态(start,success,failure,skip)

  就研究了一下testng自带的监听器。

参考:https://www.cnblogs.com/superbaby11/p/6400686.html

http://blog.csdn.net/huazhongkejidaxuezpp/article/details/48662833

作用:

用来监听testng的执行状态,来做一些对应的操作。

比如开始测试前记录一下时间,测试完成后发个结果的邮件;用例成功了就标记一下,用例失败了重新跑。

步骤:

1.初始化监听器,做一些基本的监听操作设置:

package testNGReport;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter; public class UserDefinedReporter extends TestListenerAdapter{
private int m_count = 0;
@Override
public void onTestStart(ITestResult result){
System.out.println("Test started running "+result.getMethod().getMethodName()+
"at "+result.getStartMillis());
}
@Override
public void onTestFailure(ITestResult tr) {
log(tr.getName()+ "--Test method failed\n");
}
@Override
public void onTestSkipped(ITestResult tr) {
log(tr.getName()+ "--Test method skipped\n");
}
@Override
public void onTestSuccess(ITestResult tr) {
log(tr.getName()+ "--Test method success\n");
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
System.out.println("Result fail but with success percentage");
}
public void onStart(ITestContext context) {
System.out.println("now start test");
}
public void onFinish(ITestContext context) {
System.out.println("now finish test");
}
private void log(String string) {
System.out.print(string);
if (++m_count % 40 == 0) {
System.out.println("");
}
} }

  2.在test.xml中添加监听器

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="suite1下有多test" verbose="1" preserve-order="true" parallel="false">
<!--
<listeners>
<listener class-name="extentReport.ExtentTestNGIReporterListener"></listener>
</listeners>
-->
<listeners>
<listener class-name="testNGReport.UserDefinedReporter"></listener>
</listeners>
<test name="有参数的用例的test">
<classes>
<class name="com.test.test">
<methods>
<include name="dataProviderTest"></include>
</methods>
</class>
</classes>
</test>
<test name="成功的test">
<classes>
<class name="com.test.test">
<methods>
<include name="testTrue"></include>
</methods>
</class>
</classes>
</test>
</suite>

  除了步骤2 ,还可以使用其他的方法调用监听器:

    在执行的用例中,使用监听器

package com.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test; import testNGReport.UserDefinedReporter;
@Listeners(UserDefinedReporter.class)//这个语句指明了使用自定义的listener。
public class test {
@DataProvider(name = "createData")
public Iterator<Object[]> createData(){
List<Object[]> dataProvider = new ArrayList<Object[]>();
for (int i=0;i<2;i++){
String[] s = {String.format("我是第(%s)个参数",i)};
dataProvider.add(s);
}
return dataProvider.iterator();
} @Test(dataProvider = "createData")
public void dataProviderTest(String s){
//输出log会在报告中提现
Reporter.log("获取到参数:"+s,true);
Assert.assertTrue(s.length()>2," 成功?失败?");
} @Test
public void testTrue() {
Assert.assertTrue(true,"成功咯!");
} @Test
public void testFail() {
Assert.fail("失败咯!");
}
@Test
@Parameters({ "username", "password" })
public void test(String username,String password){
System.out.println("the username is :"+username+",the password is :"+password);
Assert.assertTrue(1==1, "1!=1");
} }

 

执行之后查看结果:

[RemoteTestNG] detected TestNG version 6.13.1
now start test
now start test
Test started running dataProviderTestat 1516171050894
Test started running dataProviderTestat 1516171050894
获取到参数:我是第(0)个参数
dataProviderTest--Test method success
dataProviderTest--Test method success
Test started running dataProviderTestat 1516171050909
Test started running dataProviderTestat 1516171050909
获取到参数:我是第(1)个参数
dataProviderTest--Test method success
dataProviderTest--Test method success
now finish test
now finish test
now start test
now start test
Test started running testTrueat 1516171050925
Test started running testTrueat 1516171050925
testTrue--Test method success
testTrue--Test method success
now finish test
now finish test ===============================================
suite1下有多test
Total tests run: 3, Failures: 0, Skips: 0
===============================================

  

 

依赖注入

Dependency injection

  

上一篇:dotween tips


下一篇:ZooKeeper对比Eureka