TestNG简单的学习(七)TestNG编程方式运行

TestNG官方网站:

http://testng.org/doc/documentation-main.html

官方文档:

5.12 - JUnit tests

TestNG can run JUnit 3 and JUnit 4 tests. All you need to do is put the JUnit jar file on the classpath, specify your JUnit test classes in the testng.classNames property and set the testng.junit property to true:

 

 

testng.xml

<test name="Test1" junit="true">
  <classes>
    <!-- ...
-->

 

The behavior of TestNG in this case is similar to JUnit depending on the JUnit version found on the class path:

 

  • JUnit 3:
    • All methods starting with test* in your classes will be run
    • If there is a method setUp() on your test class, it will be invoked before every test method
    • If there is a method tearDown() on your test class, it will be invoked before after every test method
    • If your test class contains a method suite(), all the tests returned by this method will be invoked
  • JUnit 4:
    • TestNG will use the org.junit.runner.JUnitCore runner to run your tests

<!------------------------------------- JUNIT ------------------------------------>

<!------------------------------------- RUNNING TESTNG ------------------------------------>

5.13 - Running TestNG programmatically

You can invoke TestNG from your own programs very easily:

Java

TestListenerAdapter tla =
new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] {
Run2.
class });
testng.addListener(tla);
testng.run();

This example creates a TestNG object and runs the test class Run2. It also adds a TestListener. You can either use the adapter class org.testng.TestListenerAdapter or implement org.testng.ITestListener yourself. This interface contains various callback methods that let you keep track of when a test starts, succeeds, fails, etc...

Similary, you can invoke TestNG on a testng.xml file or you can create a virtual testng.xml file yourself. In order to do this, you can use the classes found the package org.testng.xml: XmlClass, XmlTest, etc... Each of these classes correspond to their XML tag counterpart.

For example, suppose you want to create the following virtual file:

 

<suite name="TmpSuite" >
  <test
name=
"TmpTest" >
    <classes>
      <class name="test.failures.Child" />
    <classes>
    </test>
</suite>

You would use the following code:

 
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
 
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes =
new ArrayList<XmlClass>();
classes.add(new XmlClass("test.failures.Child"));
test.setXmlClasses(classes)
;

And then you can pass this XmlSuite to TestNG:

 
List<XmlSuite> suites =
new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();

 

Please see the JavaDocs for the entire API.

 

<!------------------------------------- BEANSHELL ------------------------------------>

5.14 - BeanShell and advanced group selection

 

If the <include> and <exclude> tags in testng.xml are not enough for your needs, you can use a BeanShell expression to decide whether a certain test method should be included in a test run or not. You specify this expression just under the <test> tag:

 

testng.xml

<test name="BeanShell test">
   <method-selectors>
     <method-selector>
       <script language="beanshell"><![CDATA[
         groups.containsKey("test1")
       ]]></script>
     </method-selector>
   </method-selectors>
  <!--
... -->

When a <script> tag is found in testng.xml, TestNG will ignore subsequent <include> and <exclude> of groups and methods in the current <test> tag: your BeanShell expression will be the only way to decide whether a test method is included or not.

 

 

Here are additional information on the BeanShell script:

 

  • It must return a boolean value. Except for this constraint, any valid BeanShell code is allowed (for example, you might want to return true during week days and false during weekends, which would allow you to run tests differently depending on the date).
  • TestNG defines the following variables for your convenience:
    java.lang.reflect.Method method: the current test method.
    org.testng.ITestNGMethod testngMethod: the description of the current test method.
    java.util.Map<String, String> groups: a map of the groups the current test method belongs to.
  • You might want to surround your expression with a CDATA declaration (as shown above) to avoid tedious quoting of reserved XML characters).

你可能感兴趣的:(JUnit4,TestNG)