1

As an example, I have:

@Test
public void login(*Code ommitted*){

That tests logging into a site.

Then I want to do other tests that start with logging in, so I've got:

@Test
public void sendUserInvite(){
login();
*Other code omitted*
}

Something is intuitively telling me this is really bad practise, but at the same time if the login test does what I need it to, so why not re-use it in this way? Can anyone clarify this. After a while I end up doing several tests at the start of another test because they are the pre-conditions in order to carry out a particular test.

TEH EMPRAH
  • 1,828
  • 16
  • 32
Sam
  • 273
  • 2
  • 5

3 Answers3

0

If you're using TestNG, you can use @BeforeClass, @BeforeSuite, @BeforeTest, @BeforeMethod etc. in order to launch your preconditions on some step before your @Test method

E.g. you have 2 tests in xml:

<suite name="Suite0" verbose="1" >
  <test name="name0" >
    <classes>
       <class name="Test0" />
    </classes>
  </test>

  <test name="name1">
    <classes>
      <class name="Test1"/>
    </classes>
  </test>
</suite>

Let's assume Test0 and Test1 both extend class BaseTest

Then in BaseTest:

public class BaseTest {

    @BeforeTest
    public void login() {
         // smth w/ login
    }

}

So, when the suite is launched, login method will be invoked. Just note that @BeforeTest will work for every test from a suite, not for every method with @Test, this sometimes confuses.

UPD

If you're using JUnit, you can use @Before, and method with it will be run before every @Test in a class. So to say, it is the same as @BeforeMethod in TestNG

@Before
public void pre() {
     // your login here
}

@Test
public void testA() {
     // prints A
}

@Test
public void testB() {
     // prints B
}

@After
public void end() {
     // logout
}

Order of execution:

login

A

logout

login

B

logout

TEH EMPRAH
  • 1,828
  • 16
  • 32
0

According to the following links, JUnit test cases are designed to be run in isolation and each test should be independent of one other. I believe you have to reconsider your design and go for test framework like TestNG which perfectly suites your requirements.

Choose order to execute JUnit tests

running a subset of JUnit @Test methods

How to run test methods in specific order in JUnit4?

Community
  • 1
  • 1
StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
0

If you feel the need to call one test method from another test method, that's a good sign that you need to extract a class.

I suggest moving login to a PageObject class:

public class HomePage {
  private final WebDriver driver;

  public HomePage(WebDriver driver) {
    this.driver = driver;
  }

  public WelcomePage login(String userName, String password) {
    signIn(userName, password);
    Assert.assertEquals("sign in failed", userName, getSignedInUser());

    return new WelcomePage(driver, userName);
  }

  private void signIn(String userName, String password) {
    // Add code to perform the sign in
  }

  public String getSignedInUser() {
    // Add code to check the current page to see who is reported
    // as the signed in user
  }
}

Then your test looks like this:

@Test
public void login() {
  HomePage page = new HomePage(driver);

  page.login(TEST_USER_NAME, TEST_PASSWORD);
}

@Test
public void sendUserInvite() {
  WelcomePage page = new HomePage(driver)
      .login(TEST_USER_NAME, TEST_PASSWORD);

  page.sendUserInvite(NON_USER_EMAIL_ADDRESS);
}

Of course, your page objects may also have some code duplication (for instance, getting the signed in user may be a common concern). When this happens, you can either extract a base class for all of our page objects or a helper class for common logic.

NamshubWriter
  • 23,549
  • 2
  • 41
  • 59