在 testng 中使用 @BeforeTest 注释的问题 [英] Issue in using @BeforeTest annotation in testng

查看:26
本文介绍了在 testng 中使用 @BeforeTest 注释的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的移动网站编写自动化代码.

I am writing automation code for my mobile site.

Class1:-

public class Sample{

    @BeforeTest
    public void createUser(){
    // code to create user
    }

    @Test
    public void verifyUser(){
    // code to verify user
    }


    @AfterTest
    public void deleteUser(){
    // code to delete user
    }

}

Class1.我有不同的类,例如 Class2Class3Class4.

Like Class1. I have different classes such as Class2, Class3, Class4.

测试.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" verbose="1">
    <test name="Regression1" parallel="false" preserve-order="true">
        <classes>
            <class name="Class1"/>
            <class name="Class2"/>          
            <class name="Class3"/>
            <class name="Class4"/>
        </classes>
    </test>
</suite>

运行命令:-

mvn -Dtests=testing.xml test

当我运行上面的命令时,@BeforeTest 来自 Class1Class2Class3Class4 首先被调用.这意味着在运行任何测试之前首先创建 4 个用户.然后只有 Class1 测试正在运行,然后 Class2 等等.来自所有类的 Atlast @AfterTest 正在运行(删除所有用户 atlast).

when i run above command, @BeforeTest from Class1, Class2, Class3, Class4 are called first. It means 4 users are created firstly before running any test. Then only Class1 Test is running then Class2 and so on. Atlast @AfterTest from all classes are running(deleting all user atlast).

我不想要这种情况.

我需要以下方式来运行我的每个测试:-

I need the following way to run my each test:-

  1. 创建用户
  2. 验证用户
  3. 删除用户

我需要先完全执行我的 Class1 然后 Class2 等等.

I need my Class1 executed first fully then Class2 and so on.

testng 的注释有什么变化我必须做吗?

Is there any change in annotation for testng i have to do?

推荐答案

使用 BeforeMethod 而不是 BeforeTest.BeforeTest 方法将在 testng.xml 中的 标记之前运行,而不是在 @Test 之前运行方法.当然也可以使用 AfterMethod 而不是 AfterTest.

Use BeforeMethod instead of BeforeTest. The BeforeTest method will run before your <test> tag in testng.xml and not before your @Test method. And also use AfterMethod instead of AfterTest of course.

检查我对另一个类似问题的回答:BeforeClass 之间的差异和TestNG中的BeforeTest

Check my answer for another similar question: Difference between BeforeClass and BeforeTest in TestNG

这篇关于在 testng 中使用 @BeforeTest 注释的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆