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

Java 如何跨具有TestNG测试的不同类访问dependsOnMethod或dependsOnGroup?,java,testng,testng.xml,testng-annotation-test,Java,Testng,Testng.xml,Testng Annotation Test,我们正在编写一个基于TestNG的回归套件。 很快,我们将有跨不同类的测试,其中类A中的测试应该只在类B中的测试运行之后运行,我们还需要一个工具来访问一些方法,这些方法和变量将在不同的类中通用 我在尝试构建和访问跨不同类的依赖测试时遇到了nullpointer异常和其他一些问题。 } 登录测试 public class LoginTests extends BaseTest { private POSTRequest postRequest; private String password;

我们正在编写一个基于TestNG的回归套件。 很快,我们将有跨不同类的测试,其中类A中的测试应该只在类B中的测试运行之后运行,我们还需要一个工具来访问一些方法,这些方法和变量将在不同的类中通用

我在尝试构建和访问跨不同类的依赖测试时遇到了nullpointer异常和其他一些问题。

}

登录测试

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))));
}
。。 ..}

注册测试

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" >



请告知。提前感谢。

TestNG希望您的文件位于同一个包中

试着去掉你的头发

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

而是通过包含要运行的组来使用run元素

根据文件,

你的头发看起来很像

<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>



有用的示例:

如果使用上述TestNG.xml运行,则不会触发登录测试。但是如果我注释掉block,那么这些测试就会被触发,但是很明显,对于dependency error,请查看以下内容是否对您有帮助您是否有suite元素@user2451016