简体   繁体   English

在testng中使用@BeforeTest批注的问题

[英]Issue in using @BeforeTest annotation in testng

I am writing automation code for my mobile site. 我正在为我的移动网站编写自动化代码。

Class1:- 第1类:-

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
    }

}

Like Class1 . Class1一样。 I have different classes such as Class2 , Class3 , Class4 . 我有不同的类,例如Class2Class3Class4

testing.xml testing.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>

Command to run:- 运行命令:

mvn -Dtests=testing.xml test

when i run above command, @BeforeTest from Class1 , Class2 , Class3 , Class4 are called first. 当我运行上述命令时, @BeforeTest Class1Class2Class3Class4中的@BeforeTest It means 4 users are created firstly before running any test. 这意味着在运行任何测试之前,首先要创建4个用户。 Then only Class1 Test is running then Class2 and so on. 然后,只有Class1 Test在运行,然后是Class2 ,依此类推。 Atlast @AfterTest from all classes are running(deleting all user atlast). 所有类的@AfterTest正在运行(删除所有用户atlast)。

I don't want this scenario. 我不希望出现这种情况。

I need the following way to run my each test:- 我需要以下方式来运行每个测试:

  1. create of user 创建用户
  2. verify of user 用户验证
  3. delete of user 删除用户

I need my Class1 executed first fully then Class2 and so on. 我需要先完全执行Class1然后再执行Class2 ,依此类推。

Is there any change in annotation for testng i have to do? 我必须对testng的注释进行任何更改吗?

Use BeforeMethod instead of BeforeTest . 使用BeforeTest代替BeforeMethod The BeforeTest method will run before your <test> tag in testng.xml and not before your @Test method. BeforeTest方法将在testng.xml <test>标记之前运行,而不是在@Test方法之前运行。 And also use AfterMethod instead of AfterTest of course. 当然也可以使用AfterMethod代替AfterTest

Check my answer for another similar question: Difference between BeforeClass and BeforeTest in TestNG 检查我的回答是否有另一个类似的问题: TestNG中的BeforeClass和BeforeTest之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM