• testng教程之testng.xml的配置和使用,以及参数传递


    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">
      <groups>
        <run>
          <exclude name="brokenTests"  />
          <include name="checkinTests"  />
        </run>
      </groups>
      
      <classes>
        <class name="test.IndividualMethodsTest">
          <methods>
            <include name="testMethod" />
          </methods>
        </class>
      </classes>
    </test>

    suite:定义一个测试套件,可包含多个测试用例或测试group

    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" 线程数为3,可同时执行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" />
            <classes>
                <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" />
            </classes>
        </test> <!-- Test -->
    </suite> <!-- Suite -->
    
    

    参数怎么使用?

         这要在case里添加@Parameters 的annotations

         如果有多个parameter,可以一次传入

    
    
    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
         */
        @Parameters({"parameter1","parameter2"})
        @Test(groups="parameter")
        public void parameter(String parameter1,int parameter2 )
        {
            System.out.println("parameter1 is "+parameter1 );
            System.out.println("parameter2 is "+parameter2 );
        }
    }


    其中testng的配置文件为:
    <?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" />
            <classes>
                <class name="com.dbyl.tests.passParameter" />
                <class name="com.dbyl.tests.TestngExample" />
            </classes>
        </test> <!-- Test -->
    </suite> <!-- Suite -->
    
    

    这里使用两个parameter标签,传递两个参数

    执行结果如下:     

    [TestNG] Running:
    C:UsersworkspaceDemoParametertestng.xml

    [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"/>

    在target执行文件设置testng.xml路径

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


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

    运行结果:

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

    clean:
    [delete] Deleting directory C:UsersYoungworkspaceDemouild

    compile:
    [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
    sses

    runTestng:
    [testng] [TestNG] Running:
    [testng] C:UsersworkspaceDemoParametertestng.xml
    [testng]
    [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] ===============================================
    [testng] Suite
    [testng] Total tests run: 3, Failures: 0, Skips: 0
    [testng] ===============================================
    [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

    BUILD SUCCESSFUL
    Total time: 5 seconds




  • 相关阅读:
    是否该让开发人员跟客户直接交流 狼人:
    2010年浏览器随HTML5而动 五大产品年终盘点 狼人:
    微软推出HTML5实验室站点及两项原型技术 狼人:
    传IE9 RC版将于1月28日公开发布 狼人:
    Python——基础篇 狼人:
    百万级访问量网站的技术准备工作 狼人:
    容器对象spring(4)_ bean属性 scope:作用域和lazyinit
    组件注册关于VC++6.0中,MSDev89\Gallery 文件夹为空的问题
    注入参数spring入门(7)装配Bean中构造参数的注入
    优惠播客成都传智播客java基础班大优惠
  • 原文地址:https://www.cnblogs.com/jack1989/p/7767942.html
Copyright © 2020-2023  润新知