2

For example, let's say I have these tests:

test1() {
    signIn();

    // Do some testing

    signOut();
}

test2() {
    signIn();

    // Do some testing

    signOut();
}

Would it be better to have signOut() be done in a common cleanup method like the following?

test1() {
    signIn();

    // Do some testing
}

test2() {
    signIn();

    // Do some testing
}

@AfterMethod
testCleanup() {
    signOut();
}
  • 1
    Either/or . . . junit-style frameworks do a setup/teardown before and after each test. The real answer to this depends on what you're trying to test, and there's no one size fits all . . . – ernie Jan 15 '15 at 16:24
2

You can go either way.

You might include these steps in each test, because they're actions that an actual user will take. Meaning that they can potentially fail in specific circumstances, and thus you'll want them linked to that specific test case.

However, signIn() and signOut() look like actions every tester will perform manually as well for every non-automated test. So if they're not likely to fail, and these tests are always run in conjunction with manual tests, you could extract them from individual test cases.

Personally I'd go with the first option.

1

Unless you have a really good reason to repeat yourself, don't. Putting in an explicit login and logout step in each test is needless repetition if every test must perform a login and logout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.