testng笔记5

testng笔记5


1 testng的报表可以自定义,有两种方法实现:
      1) listners:实现org.
testng.ITestListener接口,当测试开始,结束,失败跳过,暂停的时候,这些类都会监听到;

2)实现org.
testng.IReporter接口,当整个suite测试结束后,可以获得全部的测试情况

2 先看实现testng.ITestListener接口的例子
    首先给出要测试的类如下:

      public class SampleTest {

@Test
public void testMethodOne(){
Assert.assertTrue(true);
}

@Test
public void testMethodTwo(){
Assert.assertTrue(false);
}

@Test(dependsOnMethods={"testMethodTwo"})
public void testMethodThree(){
Assert.assertTrue(true);
}

    然后实现testng.ITestListener接
      public class CustomLogging implements ITestListener{

@Override
public void onTestStart(ITestResult result) {
System.out.println("Test method started: "+result.getName()+ " and time is: "+getCurrentTime());
}

@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Test method success: "+result.getName()+ " and time is: "+getCurrentTime());

}

@Override
public void onTestFailure(ITestResult result) {
System.out.println("Test method failed: "+result.getName()+ " and time is: "+getCurrentTime());

}

@Override
public void onTestSkipped(ITestResult result) {
System.out.println("Test method skipped: "+result.getName()+ " and time is: "+getCurrentTime());
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// Leving blank

}


      
public void onStart(ITestContext context) {
System.out.println("Test in a suite started: "+context.getName()+ " and time is: "+getCurrentTime());

}

@Override
public void onFinish(ITestContext context) {
System.out.println("Test in a suite finished: "+ context.getName()+ " and time is: "+getCurrentTime());

}

public String getCurrentTime(){
DateFormat dateFormat =
new SimpleDateFormat("HH:mm:ss:SSS");
Date dt = new Date();
return dateFormat.format(dt);
}
 
   
3  编写自定义reporter
     public class CustomReporter implements IReporter {

@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
String outputDirectory) {
//Iterating over each suite included in the test
for (ISuite suite : suites) {
//Following code gets the suite name
String suiteName = suite.getName();
//Getting the results for the said suite
Map<String, ISuiteResult> suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
ITestContext tc = sr.getTestContext();
System.out.println("Passed tests for suite '" + suiteName + "' is:" + tc.getPassedTests().getAllResults().size());
System.out.println("Failed tests for suite '" + suiteName + "' is:" + tc.getFailedTests().getAllResults().size());
System.out.println("Skipped tests for suite '" + suiteName + "' is:" + tc.getSkippedTests().getAllResults().size());
}
}
}


<suite name="Simple Reporter Suite">
<listeners>
<listener class-name="test.reporter.CustomReporter" />
</listeners>

<test name="Simple Reporter test">
<classes>
<class name="test.sample.SampleTest" />
</classes>
</test>
</suite>

 
  输出:Passed tests for suite 'Simple Reporter Suite' is:1
Failed tests for suite 'Simple Reporter Suite' is:1
Skipped tests for suite 'Simple Reporter Suite' is:1

     
  4  程序中动态编程使用testng
      public class SimpleTestngCode {

public void simpleTestNgTest(){
//List of xml suites to be considered for test execution
List<XmlSuite> suites = new ArrayList<XmlSuite>();
//List of classes to be considered for test execution
List<XmlClass> classes = new ArrayList<XmlClass>();

//Defines a simple xml suite with a name
XmlSuite suite = new XmlSuite();
suite.setName("Simple Config suite");

//Defines a xml test for a suite and with a said name
XmlTest test = new XmlTest(suite);
test.setName("Simple config test");

//A single xml class to be considered for execution
XmlClass clz = new XmlClass("test.sample.SampleTest");
classes.add(clz);
//Sets the list of classes to be considered for execution for a test
test.setXmlClasses(classes);

//Adds a single suite to the list suites
suites.add(suite);

//Defining a testng instance
TestNG tng = new TestNG();
//Sets the List of xml suites to be considered for execution
tng.setXmlSuites(suites);
//Runs the configured testng tests.
tng.run();
}

      对应的API
          XmlSuite Defines a simple XML suite tag of the testng XML
XmlTest Describes a test tag of the testng XML
XmlPackage Describes a package tag in the testng XML
XmlClass Describes a class tag in the testng XML
  
   6 看下如何动态代码中,增加参数
      
      public class ParametrizedTest {
@Parameters({"suite-param-one","test-param-one"})
@Test
public void paramTestOne(String suiteParam,String testParam){
System.out.println("Test One.");
System.out.println("Suite param is: "+suiteParam);
System.out.println("Test param is: "+testParam);
}

@Parameters({"suite-param-two","test-param-two"})
@Test
public void paramTestTwo(String suiteParam,String testParam){
System.out.println("Test Two.");
System.out.println("Suite param is: "+suiteParam);
System.out.println("Test param is: "+testParam);
}

    测试类:
        public class ParameterizedCode {

public void parameterizedTest(){
List<XmlSuite> suites = new ArrayList<XmlSuite>();
List<XmlClass> classes = new ArrayList<XmlClass>();
Map<String,String> suiteParams = new HashMap<String,String>();
Map<String,String> testParams = new HashMap<String,String>();

XmlSuite suite = new XmlSuite();
suite.setName("Parameterized suite");

//Defining suite level params and their values
suiteParams.put("suite-param-one", "Suite Param One");
suiteParams.put("suite-param-two", "Suite Param Two");
//Setting the params to the suite
suite.setParameters(suiteParams);

XmlTest test = new XmlTest(suite);
test.setName("Parameterized test");

//Defining test level params and their values
testParams.put("test-param-one", "Test Param One");
testParams.put("test-param-two", "Test Param Two");
//Setting the test level params
test.setParameters(testParams);

XmlClass clz = new XmlClass("test.sample.ParametrizedTest");
classes.add(clz);
test.setXmlClasses(classes);


suites.add(suite);

TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
}

  7  include /exclude的API
      public void includeExcludeTest(){
List<XmlSuite> suites = new ArrayList<XmlSuite>();
List<XmlClass> classes = new ArrayList<XmlClass>();

XmlSuite suite = new XmlSuite();
suite.setName("Include Exclude Method suite");

XmlTest test = new XmlTest(suite);
test.setName("Include Exclude Method test");
//Test class to be included for test execution
XmlClass clz = new XmlClass("test.sample.IncludeExcludeMethodTest");

//Test methods to be included
XmlInclude methodOne= new XmlInclude("testMethodOne");
XmlInclude methodTwo= new XmlInclude("testMethodTwo");

//Creating a list included methods and adding the methods instances to it
List<XmlInclude> includes = new ArrayList<XmlInclude>();
includes.add(methodOne);
includes.add(methodTwo);

//Setting the included methods for the class
clz.setIncludedMethods(includes);

classes.add(clz);
test.setXmlClasses(classes);

suites.add(suite);

TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
}
8  也可以包含或者排除分组
      public void includeExcludeTest(){
List<XmlSuite> suites = new ArrayList<XmlSuite>();
List<XmlClass> classes = new ArrayList<XmlClass>();

XmlSuite suite = new XmlSuite();
suite.setName("Include Exclude Group suite");

XmlTest test = new XmlTest(suite);
test.setName("Include Exclude Group test");
XmlClass clz = new XmlClass("test.sample.IncludeExcludeGroupTest");
classes.add(clz);
test.setXmlClasses(classes);

//Including and excluding groups
test.addIncludedGroup("group-one");
test.addExcludedGroup("group-two");

suites.add(suite);

TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
}

9 依赖测试API
       public void dependencyTest(){
List<XmlSuite> suites = new ArrayList<XmlSuite>();
List<XmlClass> classes = new ArrayList<XmlClass>();

XmlSuite suite = new XmlSuite();
suite.setName("Dependency suite");

XmlTest test = new XmlTest(suite);
test.setName("Dependency test");
XmlClass clz = new XmlClass("test.sample.DependencyTest");
classes.add(clz);
test.setXmlClasses(classes);

//Defining an xml dependency where "group-one" depends on "group-two"
test.addXmlDependencyGroup("group-one", "group-two");

suites.add(suite);

TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
}
   10 在testng下运行junit
       <test junit="true" name="Junit test">
<classes>
<class name="test.sample.junit.JunitSampleTest" />
</classes>
</test>
  设置junit="true"就可以了,可以在同一个testsuite中运行junit和testng
    
<suite name="Junit Testng Suite">
<test junit="true" name="Junit test">
<classes>
<class name="test.sample.junit.JunitSampleTest" />
</classes>
</test>
<test name="Testng test">
<classes>
<class name="test.sample.testng.TestngSampleTest" />
</classes>
</test>
</suite>
 
  11 testng maven的配置
    <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>

其他设置项:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<parallel>tests</parallel>
<threadCount>5</threadCount>
</configuration>
</plugin>
Have a

12 testng结合selenium
  
    public class SampleSeleniumTest {
WebDriver driver;
@BeforeMethod
public void beforeMethod(){
//Initializing the selenium webdriver object
driver = new FirefoxDriver();
}

@Test
public void googleTest(){
//Opening the google page
driver.navigate().to("http://www.google.com");
//Finding the search field and entering text to it.
driver.findElement(By.cssSelector("input[name='q']")).sendKeys("TestNg");
WebDriverWait wait = new WebDriverWait(driver, 30);
//Waiting for the serach list to be populated.
List<WebElement> results=wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("h3.r")));
//Getting the text of the first search result.
String searchResult=results.get(0).getText();
//Verifying the text of first search test result with the expected text
Assert.assertEquals(searchResult,"TestNG - Welcome");
}

@AfterMethod
public void afterMethod(){
//Quitting the browser.
driver.quit();
}


      testng.xml:
         <suite name="Selenium Suite" parallel="tests" thread-count="2">
<test name="Selenium test - 1">
<classes>
<class name="test.java.org.sample.selenium.SampleSeleniumTest" />
</classes>
</test>
<test name="Selenium test - 2">
<classes>
<class name="test.java.org.sample.selenium.SampleSeleniumTest" />
</classes>
</test>
</suite>
   这样的话,同时打开两个firefox去进行测试搜索结果。

    

你可能感兴趣的:(TestNG)