如何跨具有 TestNG 测试的不同 类 访问 dependsOnMethod 或 dependsOnGroup?

How to access dependsOnMethod or dependsOnGroup across different classes having TestNG tests?

我们正在编写一个基于 TestNG 的回归套件。 很快,我们将在不同的 类 中进行测试,其中 Class A 中的测试应该 运行 只有在 Class 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>

请指教。提前致谢。

TestNG 希望您的文件在同一个包中。

尝试删除您的

<groups>
        <dependencies>
            <group depends-on="SignupGroup" name="LoginTestsGroup"/>
        </dependencies>
    </groups>

而是使用 运行 元素,包括您想要 运行 的组。

根据文档,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/