• testng 目录结构


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="All Test Suite">
        <test verbose="1" preserve-order="true" name="C:\Users\wumin\Downloads\XunitDemo-master\XunitDemo-master" >
            <groups>
                <dependencies>
                    <group name="1" depends-on="">
                       <classes>
                           <class name="TestXunit.LoginTest">
                               <methods>
                                   <include name="testUserLogin0"></include>
                                   <exclude name="testUserLogin1"></exclude>
                                   <include name="testUserLogin2"></include>
                                   <include name="testUserLogin3"></include>
                               </methods>
                           </class>
                       </classes>
                    </group>
                </dependencies>
            </groups>
    </test>
    </suite
    
    
    <!--<classes>-->
    <!--<class name="DemoXunit.LoginTest">-->
        <!--<methods>-->
            <!--<include name="testLogin"/>-->
        <!--</methods>-->
    <!--</class>-->
    <!--<class name="DemoXunit.ShoppingTest">-->
        <!--<methods>-->
            <!--<include name="testLogin"/>-->
            <!--<include name="testLoginAndShopping"/>-->
            <!--<include name="testDatas"/>-->
        <!--</methods>-->
    <!--</class>-->
    <!--<class name="DemoXunit.ParamDataTest">-->
        <!--<methods>-->
            <!--<parameter name="aa" value="AA"/>-->
            <!--<parameter name="bb" value="BB"/>-->
            <!--<include name="testParams"/>-->
        <!--</methods>-->
    <!--</class>-->
    <!--</classes>-->
    

      testng 目录结构 : suit -test->group->depenceies->group->classes ->class->method  

      testng 方法忽略测试 <method>节点底下加

    <exclude name="testUserLogin1"></exclude>

    testng 其他功能:
    参数化(通过xml文件参数化)
    1.抽象出公共测试方法
        @Test
        public void testUserLogin(String name1,String pwd1,String except1){
            Login login = new Login();
            String ac = login.userLogin(name1,pwd1);
            Assert.assertEquals(except1,ac);
        }

    2.在testng.xml文件中<method>节点增加

    parameter
             <methods>
                                   <parameter name="name" value="zhangsan"></parameter>
                                   <parameter name="pwd" value="123"></parameter>
                                   <parameter name="except" value="欢迎zhangsan"></parameter>
                                   <include name="testUserLogin"></include>
                               </methods>
    

    3.在test方法上增加

    Parameters注解
        @Parameters({"name","pwd","except"})
        @Test
        public void testUserLogin(String name1,String pwd1,String except1){
            Login login = new Login();
            String ac = login.userLogin(name1,pwd1);
            Assert.assertEquals(except1,ac);
        }  
    通过 DataProvide 进行参数化
    1.提供数据类
    package LoginData;
    
    import org.testng.annotations.DataProvider;
    
    public class Login1Params {
    
        @DataProvider
        public Object [][] getUsers(){
    
            return new Object[][]{
                    {"zhangsan","1234","欢迎zhangsan"},
                    {"lisi","","用户名或密码不能为空"},
                    {"admin","121","欢迎管理员"},
            };
        }
    }
    

      2.@test增加属性

    package TestXunit;
    
    import DemoXunit.Login;
    import LoginData.Login1Params;
    import LoginData.LoginParams;
    import org.testng.Assert;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Optional;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;
    
    /**
     * Created by duzhe on 2018/12/23.
     *
     * @Description:
     */
    public class LoginTest {
    //    @Parameters({"name","pwd","except"})
        @Test(dataProvider = "getUsers",dataProviderClass = Login1Params.class)
        public void testUserLogin(String name1,String pwd1,String except1){
            Login login = new Login();
            String ac = login.userLogin(name1,pwd1);
            Assert.assertEquals(except1,ac);
        }
    }
    参考链接:https://www.cnblogs.com/qiaoyeye/p/5038553.html
    maven pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.test</groupId>
        <artifactId>XunitDemo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>testng1.xml</suiteXmlFile>
                            <suiteXmlFile>testng2.xml</suiteXmlFile>
                        </suiteXmlFiles>
                        <!--<parallel>methods</parallel>-->
                        <!--<threadCount>2</threadCount>-->
                    </configuration>
                </plugin>
                <!--<plugin>-->
                    <!--<groupId>org.apache.maven.plugins</groupId>-->
                    <!--<artifactId>maven-compiler-plugin</artifactId>-->
                    <!--<configuration>-->
                        <!--<source>1.6</source>-->
                        <!--<target>1.6</target>-->
                    <!--</configuration>-->
                <!--</plugin>-->
            </plugins>
        </build>
    
    
        <dependencies>
    
            <!-- https://mvnrepository.com/artifact/org.testng/testng -->
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.14.3</version>
                <scope>test</scope>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.uncommons/reportng -->
            <dependency>
                <groupId>org.uncommons</groupId>
                <artifactId>reportng</artifactId>
                <version>1.1.4</version>
                <scope>test</scope>
            </dependency>
    
    
    
        </dependencies>
    
    </project>
    

      

     
  • 相关阅读:
    天梯赛练习2 补题
    QFNU 天梯赛练习 1 补题
    2019 山东省赛 B 题
    CCPC2020 网络赛 总结
    一个比较好看的 Typora 主题
    〔OS〕磁盘调度算法
    〔OS〕页面置换算法
    〔OS〕多线程模拟实现生产者和消费者
    〔OS〕银行家算法
    LCS and LIS
  • 原文地址:https://www.cnblogs.com/Neotester/p/14100674.html
Copyright © 2020-2023  润新知