0

I am using Selenium and JUnit to automate tests. Lets say there are two tests and each has to authenticate with a different user and perform an action. Can it be done any smarter than to call an authentication method in each test method? Preferably with annotations so that the username for the test really stands out while skimming through the code and the login method being in @Before method or in TestBase class.

 public class AppTest extends TestBase {
 // this test must be run with userA
 @Test
 public void testA() {
 authenticateUser (userA);
 int count = retrieveNewEmailCount();
 assertEquals(NEW_EMAIL_COUNT, count);
 }
 // this test must be run with userB
 @Test
 public void testB() {
 authenticateUser (userB);
 String notificationText = retrieveNoNewEmailNotification();
 assertEquals(NO_EMAIL_NOTIFICATION, notificationText);
 }
}

Do not comment on business logic in the examples above as this is just simplified example. The real tests are not about emails at all. In reality there are hundreds of tests and almost a hundred users. Each user participates from one to hundreds tests. Each test is run once with one particular user only, ie no need to run the same test few times with different users.

asked Nov 1, 2019 at 20:33
2
  • What stops you from using beforemethod ? Commented Nov 2, 2019 at 14:01
  • @PDHide, nothing. How do I do that? How do I get a required userName in authenticateUser method if it is in Before and the userName variabble is declared in Test method? Commented Nov 2, 2019 at 15:29

1 Answer 1

0

You can use a bit of inheritance, which helps you to extend without dealing too much with data providers:

public abstract class AbstractTest extends TestBase{
 protected User user;
 protected AbstractTest(User user) { this.user = user; } 
 @Before
 public void setUp() {
 authenticateUser(this.user);
 }
}
public class UserWithEmailsTest extends AbstractTest {
 protected UserWithEmailsTest(User user) { super(userA); } 
 // this test must be run with userA
 @Test
 public void testA() { 
 int count = retrieveNewEmailCount();
 assertEquals(NEW_EMAIL_COUNT, count);
 }
}

public class UserWithNoEmailsTest extends AbstractTest {

 protected UserWithNoEmailsTest(User user) { super(userB); } 
 @Test
 public void testB() {
 String notificationText = retrieveNoNewEmailNotification();
 assertEquals(NO_EMAIL_NOTIFICATION, notificationText);
 }
}
answered Nov 2, 2019 at 17:40
3
  • Thanks for the answer, but in your example every test class has one test case, which is not suitable for our framework. As I said, there are hundreds of tests, they can not each go into separate class. Commented Nov 4, 2019 at 14:44
  • Not necessarily one per class: You just need to separate things that use different users. You can also inject this dependency using annotations (which is just syntactic sugar for the polymorphism above). Commented Nov 4, 2019 at 15:15
  • can you give an example where all tests are in one class? I can not separate tests into classes by user, we have another logic for grouping tests into classes, I can not group by user. Commented Nov 5, 2019 at 22:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.