如何跨具有TestNG测试的不同类访问dependsOnMethod或dependsOnGroup?
Posted
技术标签:
【中文标题】如何跨具有TestNG测试的不同类访问dependsOnMethod或dependsOnGroup?【英文标题】:How to access dependsOnMethod or dependsOnGroup across different classes having TestNG tests? 【发布时间】:2021-02-24 20:19:38 【问题描述】:我们正在编写基于 TestNG 的回归套件。 很快,我们将在不同的类中进行测试,其中 A 类中的测试应该在 B 类中的测试运行后才运行,而且我们需要一个工具来访问一些通用的方法和变量。
在尝试构建和访问分布在不同类中的依赖测试时,我遇到了空指针异常和其他一些问题。
public class BaseTest
final TestContext testContext = new TestContext();
TheUser newUser;
String newUserPhoneNumber;
String newUserVerificationCode;
public BaseTest()
newUser = testContext.getUserObjectManager().getTheUser();
newUserPhoneNumber = newUser.generateRandomPhoneNumber();
newUserVerificationCode = newUser.validVerificationCode();
@BeforeSuite
public void requestUserTokens(ITestContext iTestContext)
iTestContext.setAttribute("username", newUser.getUsername());
iTestContext.setAttribute("username", newUser.getPassword());
登录测试
public class LoginTests extends BaseTest
private POSTRequest postRequest;
private String password;
private String username;
@BeforeClass()
@BeforeEach
public void init()
newUser = testContext.getUserObjectManager().getTheUser();
postRequest = new POSTRequest();
postRequest.chooseEndPoint(OAUTH2_PARTIAL_PATH);
@Test(dependsOnGroups = "SignupGroup")
public void postRequestUserLogsInWithValidCredentials(ITestContext iTestContext)
JSONObject requestBody = new JSONObject();
requestBody.put("client_id", "**********");
requestBody.put("grant_type", "password");
username = iTestContext.getAttribute("username").toString();
password = iTestContext.getAttribute("password").toString();
System.out.println("TestContext username--> " + newUser.getUsername());
System.out.println("TestContext password--> " + newUser.getPassword());
System.out.println("iTestContext username--> " + username);
System.out.println("iTestContext password--> " + password);
.. ..
注册测试
public class SignUpTests
private final TheUser newUser = new TheUser();
String newUserPhoneNumber = newUser.generateRandomPhoneNumber();
String newUserVerificationCode = newUser.validVerificationCode();
private PUTRequest putrequest;
private POSTRequest postRequest;
@BeforeClass
@BeforeEach
public void init()
putrequest = new PUTRequest();
postRequest = new POSTRequest();
putrequest.chooseEndPoint(API_V1_PARTIAL_PATH);
postRequest.chooseEndPoint(API_V1_PARTIAL_PATH);
@Test(priority = 0, groups = "SignupGroup")
public void userEntersInvalidVerificationCode()
String invalidVerificationCode = Helper.randomNumber(6);
JSONObject requestBody = new JSONObject();
requestBody.put("sms_code", invalidVerificationCode);
String path = USERS_VERIFICATION_PHONE_NUMBER_PATH + newUser.getVerificationId() + "/";
Response response = putrequest.putRequest(requestBody, path);
assertThat(response.getStatusCode(), (anyOf(is(SC_UNPROCESSABLE_ENTITY), is(SC_FORBIDDEN))));
TestNG.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<test name="SignUp Test Suite" >
<parameter name="Test without Mock" value="Api test-method Three" />
<classes >
<class name="signupAndLogin.SignUpTests" />
</classes>
</test>
<test name="Login Test Suite">
<parameter name="Test without Mock" value="Api test-method Three" />
<groups>
<dependencies>
<group depends-on="SignupGroup" name="LoginTestsGroup"/>
</dependencies>
</groups>
<classes>
<class name="signupAndLogin.LoginTests" />
</classes>
</test>
请多多指教。提前致谢。
【问题讨论】:
【参考方案1】:TestNG 希望您的文件在同一个包中。
尝试删除您的
<groups>
<dependencies>
<group depends-on="SignupGroup" name="LoginTestsGroup"/>
</dependencies>
</groups>
而是通过包含您要运行的组来使用 run 元素。
根据文档,https://testng.org/doc/documentation-main.html#test-groups
您的遗嘱看起来类似于
<test name="SignUp Test Suite" >
<parameter name="Test without Mock" value="Api test-method Three" />
<classes >
<class name="signupAndLogin.SignUpTests" />
</classes>
</test>
<test name="Login Test Suite">
<parameter name="Test without Mock" value="Api test-method Three" />
<groups>
<run>
<include name="SignupGroup" />
</run>
</groups>
<classes>
<class name="signupAndLogin.LoginTests" />
</classes>
</test>
有用的例子:https://howtodoinjava.com/testng/testng-test-groups/
【讨论】:
如果我使用上面的 TestNG.xml 运行,则不会触发登录测试。但是如果我注释掉以上是关于如何跨具有TestNG测试的不同类访问dependsOnMethod或dependsOnGroup?的主要内容,如果未能解决你的问题,请参考以下文章