zoukankan      html  css  js  c++  java
  • testng教程之testng.xml的配置和使用,以及参数传递


    昨天主要学习的是testng 的annotation基本用法和生命周期,今天学习一下如何使用testng.xml和testng.xml的相关配置



    You can invoke TestNG in several different ways: 你可以用以下三种方式执行测试

    • With a testng.xml file           直接run as test suite
    • With ant                            使用ant
    • From the command line      从命令行
    • eclipse                              直接在eclipse执行

    textng.xml 基本格式如下:

    <test name="Regression1">
          <exclude name="brokenTests"  />
          <include name="checkinTests"  />
        <class name="test.IndividualMethodsTest">
            <include name="testMethod" />


    suite 里可以设置是否使用多线程:


    parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.

    parallel="tests": TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.

    parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.

    parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.

    • parallel="classes"  每个测试用例class级别多线程
    • thread-count="3" 线程数为5,可同时执行3个case
    • preserve-order="true"  根据官方解释(If you want the classes and methods listed in this file to be run in an unpredictible order, set the preserve-order attribute to false)设置为false乱序执行,设置为true会按照你配置执行
    • Parameter标签传递参数


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite" parallel="classes" thread-count="3">
        <test verbose="2" preserve-order="true" name="TestDebug">  
            <parameter name="driverName" value="chrome" />
                <class name="com.dbyl.tests.Case1" />
                <class name="com.dbyl.tests.JDaddress" />
                <class name="com.dbyl.tests.UseCookieLogin" />
                <class name="com.dbyl.tests.MapTest" />
                <class name="com.dbyl.tests.loginTest" />
        </test> <!-- Test -->
    </suite> <!-- Suite -->


         这要在case里添加@Parameters 的annotations


    package com.dbyl.tests;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;
     * This Test for verify Parameter annotation
     * @author Young
    public class passParameter {
         * @param parameter1
         * @param parameter2
        public void parameter(String parameter1,int parameter2 )
            System.out.println("parameter1 is "+parameter1 );
            System.out.println("parameter2 is "+parameter2 );



    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite" parallel="classes" thread-count="5">
        <test verbose="2" preserve-order="true" name="TestDebug">  
            <parameter name="parameter1" value="parameter1" />
            <parameter name="parameter2" value="123" />
                <class name="com.dbyl.tests.passParameter" />
                <class name="com.dbyl.tests.TestngExample" />
        </test> <!-- Test -->
    </suite> <!-- Suite -->



    [TestNG] Running:

    [TestRunner] Starting executor for test TestDebug with time out:2147483647 milliseconds.
    parameter1 is parameter1
    parameter2 is 123

    接下来尝试从ant命令行执行test suite

    首先在 build配置文件加入:

    <taskdef resource="testngtasks" classpath="testng.jar"/>


    <xmlfileset dir="${basedir}" includes="Parametertestng.xml"/>


     1 <?xml version="1.0"?>
     2 <project name="Demo" default="run" basedir=".">
     3     <echo  message="Start selenium Grid" />
     4     <echo  message="import libs" />
     5     <path id="run.classpath">
     6         <fileset dir="${basedir}">
     7             <include name="lib/poi/*.jar" />
     8             <include name="lib/poi/lib/*.jar" />
     9             <include name="lib/testng.jar" />
    10             <include name="lib/sikuli-script.jar" />
    11             <include name="lib/*.jar" />
    12         </fileset>
    13         <fileset dir="${basedir}/lib/selenium">
    14             <include name="selenium-java-2.45.0.jar" />
    15             <include name="libs/*.jar" />
    16         </fileset>
    17     </path>
    18     <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="run.classpath" />
    19     <target name="clean">
    20         <delete dir="build"/>
    21     </target>
    22     <target name="compile" depends="clean">
    23         <echo message="mkdir"/>
    24         <mkdir dir="build/classes"/>
    25         <javac srcdir="src" destdir="build/classes" debug="on" encoding="UTF-8">
    26             <classpath refid="run.classpath"/>
    27         </javac>
    28     </target>
    29     <path id="runpath"> 
    30          <path refid="run.classpath"/> 
    31          <pathelement location="build/classes"/> 
    32        </path> 
    33     <target name="run" depends="compile">
    34         <testng  classpathref="runpath"  outputDir="test-output" 
    35             haltonfailure="true"
    36             useDefaultListeners="false"   
    37             listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.FailedReporter" >  
    38             <xmlfileset dir="${basedir}" includes="testng.xml"/>
    39             <jvmarg value="-Dfile.encoding=UTF-8" />
    40             <sysproperty key="org.uncommons.reportng.title" value="AutoMation TestReport" /> 
    41         </testng>
    42         </target>
    43         <target name="runTestng" depends="compile">
    44         <testng  classpathref="runpath"  outputDir="test-output" 
    45             haltonfailure="true"
    46             useDefaultListeners="false"   
    47             listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.FailedReporter" >  
    48             <xmlfileset dir="${basedir}" includes="Parametertestng.xml"/>
    49             <jvmarg value="-Dfile.encoding=UTF-8" />
    50             <sysproperty key="org.uncommons.reportng.title" value="AutoMation TestReport" /> 
    51         </testng>
    52     </target>
    53 </project>
    View Code

    接下在在命令行执行: ant runTestng


    C:UsersworkspaceDemo>ant runTestng
    Buildfile: C:UsersworkspaceDemouild.xml
    [echo] Start selenium Grid
    [echo] import libs

    [delete] Deleting directory C:UsersYoungworkspaceDemouild

    [echo] mkdir
    [mkdir] Created dir: C:UsersYoungworkspaceDemouildclasses
    [javac] C:UsersYoungworkspaceDemouild.xml:25: warning: 'includeantrunt
    ime' was not set, defaulting to build.sysclasspath=last; set to false for repeat
    able builds
    [javac] Compiling 21 source files to C:UsersYoungworkspaceDemouildcla

    [testng] [TestNG] Running:
    [testng] C:UsersworkspaceDemoParametertestng.xml
    [testng] [TestRunner] Starting executor for test TestDebug with time out:2147
    483647 milliseconds.
    [testng] parameter1 is parameter1
    [testng] parameter2 is 123
    [testng] This is beforeClass method .The Value of a is: 1
    [testng] This is beforeMethod method. The Value of a is: 2
    [testng] This is Test method1 .The Value of a is: 3
    [testng] This is AfterMethod Method .The Value of a is: 6
    [testng] This is beforeMethod method. The Value of a is: 2
    [testng] This is Test method2 .The Value of a is: 4
    [testng] This is AfterMethod Method .The Value of a is: 6
    [testng] This is AfterClass Method .The Value of a is: 5
    [testng] ===============================================
    [testng] Suite
    [testng] Total tests run: 3, Failures: 0, Skips: 0
    [testng] ===============================================
    [testng] [TestNG] Time taken by [FailedReporter passed=3 failed=0 skipped=0]:
    0 ms
    [testng] [TestNG] Time taken by org.uncommons.reportng.HTMLReporter@65fe58e0:
    120 ms

    Total time: 5 seconds

  • 相关阅读:
    Python 爬邮箱
    Attempting to fetch value instead of handling error Internal: failed to get device attribute 13 for device 0: CUDA_ERROR_UNKNOWN: unknown error
    cmake 常用命令
    IOS 图片拉伸技巧
    IOS学习-报错误 Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/4582173.html
Copyright © 2011-2022 走看看