在 testng 中使用 @BeforeTest 注释的问题 [英] Issue in using @BeforeTest annotation in testng
问题描述
我正在为我的移动网站编写自动化代码.
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
.我有不同的类,例如 Class2
、Class3
、Class4
.
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
来自 Class1
、Class2
、Class3
、Class4
首先被调用.这意味着在运行任何测试之前首先创建 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:-
- 创建用户
- 验证用户
- 删除用户
我需要先完全执行我的 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屋!