美文网首页
TestNg-01-HelloWorld

TestNg-01-HelloWorld

作者: 请叫我刚爷 | 来源:发表于2020-03-12 14:10 被阅读0次

1.环境准备

jdk1.8 , idea/myeclisp , maven

2、构建自己的maven工程

在pom.xml中引入需要的配置,如下

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

配置Java文件

package com.course.testng;
import org.testng.annotations.*;

public class TestNg0 {

    @Test
    public void HelloWorld(){
        System.out.println("HelloWorld");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("BeforeMethod这是在HelloWorld之前运行的");
    }

    @AfterMethod
    public void afterMethod(){
        System.out.println("AfterMethod这是在HelloWorld之后运行的");
    }

    @BeforeClass
    public void beforeClass(){
        System.out.println("beforeClass这是在类TestNg0运行之前运行的方法");
    }

    @AfterClass
    public void afterClass(){
        System.out.println("afterClass这是在类TestNg0运行之后运行的方法");
    }

    @BeforeSuite
    public void beforeSuite(){
        System.out.println("BeforeSuite测试套件");
    }

    @AfterSuite
    public void afterSuite(){
        System.out.println("AfterSuite测试套件");
    }
}

执行结果如下:

image.png

通过运行结果可以知道各个标签的运行大意,从大到小依次是BeforeSuite-->beforeClass-->BeforeMethod-->test-->AfterMethod-->afterClass-->AfterSuite。

我们将代码更改一下,如下:

package com.course.testng;
import org.testng.annotations.*;

public class TestNg0 {

    @Test
    public void HelloWorld(){
        System.out.println("HelloWorld");
    }

    @Test
    public void HelloWorld1(){
        System.out.println("HelloWorld1");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("BeforeMethod这是在HelloWorld之前运行的");
    }

    @AfterMethod
    public void afterMethod(){
        System.out.println("AfterMethod这是在HelloWorld之后运行的");
    }

    @BeforeClass
    public void beforeClass(){
        System.out.println("beforeClass这是在类TestNg0运行之前运行的方法");
    }

    @AfterClass
    public void afterClass(){
        System.out.println("afterClass这是在类TestNg0运行之后运行的方法");
    }

    @BeforeSuite
    public void beforeSuite(){
        System.out.println("BeforeSuite测试套件");
    }

    @AfterSuite
    public void afterSuite(){
        System.out.println("AfterSuite测试套件");
    }
}

执行结果如下图,得知除了BeforeMethod和AfterMethod其他的都执行多次,就是有多少个Test注解标注,就执行多少次,其他的只执行一次。相关测试注解的详解请参考<u>https://www.yiibai.com/testng/basic-annotations.html</u>

[图片上传失败...(image-d24621-1583992745508)]

忽略测试

如果不想让某个方法执行则标注@Test(enabled = false)即可如下

package com.course.testng;
import org.testng.annotations.*;

public class TestNg0 {

    @Test
    public void HelloWorld(){
        System.out.println("HelloWorld");
    }

    @Test(enabled = false)
    public void HelloWorld1(){
        System.out.println("HelloWorld1");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println("BeforeMethod这是在HelloWorld之前运行的");
    }

    @AfterMethod
    public void afterMethod(){
        System.out.println("AfterMethod这是在HelloWorld之后运行的");
    }

    @BeforeClass
    public void beforeClass(){
        System.out.println("beforeClass这是在类TestNg0运行之前运行的方法");
    }

    @AfterClass
    public void afterClass(){
        System.out.println("afterClass这是在类TestNg0运行之后运行的方法");
    }

    @BeforeSuite
    public void beforeSuite(){
        System.out.println("BeforeSuite测试套件");
    }

    @AfterSuite
    public void afterSuite(){
        System.out.println("AfterSuite测试套件");
    }
}
image.png

相关文章

  • TestNg-01-HelloWorld

    1.环境准备 jdk1.8 , idea/myeclisp , maven 2、构建自己的maven工程 在pom...

网友评论

      本文标题:TestNg-01-HelloWorld

      本文链接:https://www.haomeiwen.com/subject/gypujhtx.html