We're writing a TestNG based regression suite. Soon, we'll have tests across different classes, wherein the Test in Class A should run only after tests in Class B has run, also we'll need a facility to access some methods, variables which will be common across.
I am getting nullpointer exception & few other issues when tried to build & access dependent tests spread across different classes.
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());
}
}
Login Test
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);
.. ..}
SignUP Test
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>
Kindly advise. Thanks in advance.
TestNG wants your files to be in same package.
Try to remove your
<groups>
<dependencies>
<group depends-on="SignupGroup" name="LoginTestsGroup"/>
</dependencies>
</groups>
And instead use the run element by including the group you want to run.
According to the documentation, https://testng.org/doc/documentation-main.html#test-groups
Your will look similar to
<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>
Helpful Example: https://howtodoinjava.com/testng/testng-test-groups/