Selenium junit tests - how do I run tests within a test in sequential order?

You can't guarantee the order of execution of test methods in JUnit.

You can't guarantee the order of execution of test methods in JUnit. The order of execution of test classes within a suite is guaranteed (if you're using Suite), but the order of execution if the test classes are found by reflection isn't (for instance, if you're running a package in Eclipse, or a set of tests from maven or ant). This may be definable by ant or maven, but it isn't defined by JUnit.In general, JUnit executes the test methods in the order in which they are defined in the source file, but not all JVMs guarantee this (particulary with JVM 7).

If some of the methods are inherited from an abstract base test class, then this may not hold either. (This sounds like your case, but I can't tell from your description). For more information on this, see my answer to Has JUnit4 begun supporting ordering of test?

Is it intentional?. So what can you do to fix your problem? There are two solutions.

In your original example, you've actually only got one test (verify), but you've got 4 methods, two setup (createUser, joinUserToRoom) and one teardown (deleteUser). So your first option is to better define your test cases, using a TestRule, in particular ExternalResource. ExternalResource allows you to define before/after behaviour for a test, similar to @Before/@After.

However, the advantage of ExternalResource is that you can factor this out of your test. So, you would create/delete the user in your external resource: public class UsesExternalResource { @Rule public ExternalResource resource= new ExternalResource() { @Override protected void before() throws Throwable { // create user }; @Override protected void after() { // destroy user }; }; @Test public void testJoinUserToRoom() { // join user to room // verify all ok } } For me, this is simpler and easier to understand, and you get independent tests, which is a good thing. This is what I would do, but you will need to refactor your tests a bit.

You can also stack these Rules using RuleChain. Your second option, if you really want to introduce dependencies between your test methods, is to look at TestNG, in which you can define dependencies from one test to another.

Thanks Matthew, makes sense, I'll give this a go. – Pritum Patel Dec 15 '11 at 13:17.

Using @SuiteClasses annotation it respects test's order. Import org.junit.runner. RunWith; import org.junit.runners.

Suite; import org.junit.runners.Suite. SuiteClasses; @RunWith(Suite. Class) @SuiteClasses(value={ testA.

Class, testB. Class}) public class MyTestSuite{ }.

Thanks Shagaan, at the moment I'm just clicking on the package name and running all the test within it. Thats where I'm having the issue with the tests within the test not running in the correct order. I guess I'm not actually running a suite then?

– Pritum Patel Dec 15 '11 at 10:35 Exactly. Just create a testSuite and run it. – Shagaan Dec 15 '11 at 10:40.

If they have a 'correct' order, then they are not multiple tests, but a single test that you have incorrectly annotated as being multiple independent tests. Best practise would to rewrite them in approved junit style (setup - act - verify), supported by @Before or @BeforeClass methods that did any required common setup. Quick workaround would be to have a single @Test-annotated method that called the other test methods in sequence.

That becomes something like the preferred alternative if you are using Junit not to do strict unit testing, but something more like scenario-driven systems testing. It's not necessarily the best tool for such use, but it does work perfectly well in some cases. Then, what you would have so far is have a single test: @Test public void testUserNominalLifeCycle(... which could then, if you are feeling virtuous, be supplemented by extra new tests like @Test public void testUserWhoNeverJoinsARoom(...

Thanks Soru, test's within the package can be run in any order. But the tests within those tests need to be run in the correct order. So it must login as admin create a user and join that user to a room.

Then login as that user and run the test. I'm not sure how its possible to run this kind of test without any dependencies...? – Pritum Patel Dec 15 '11 at 10:41 For unit-style testing, you can have an @Before (or @BeforeClass) that creates required users, and a corresponding after-annotated function that cleans them up. – soru Dec 15 '11 at 12:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions