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.
-
What stops you from using beforemethod ?PDHide– PDHide2019年11月02日 14:01:25 +00:00Commented 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?lucasso– lucasso2019年11月02日 15:29:37 +00:00Commented Nov 2, 2019 at 15:29
1 Answer 1
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);
}
}
-
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.lucasso– lucasso2019年11月04日 14:44:36 +00:00Commented 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).João Farias– João Farias2019年11月04日 15:15:15 +00:00Commented 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.lucasso– lucasso2019年11月05日 22:03:40 +00:00Commented Nov 5, 2019 at 22:03