|
Re: Tests not executing in specified order [message #553422 is a reply to message #553313] |
Tue, 17 August 2010 13:27 |
Pascal G Messages: 157 Registered: July 2009 |
Senior Member |
|
|
On 10-08-17 03:03 AM, Anitha wrote:
> i have couple of test cases which are dependant on each other. However
> the tests mentioned inside the same class are not getting executed in
> the order specified in the class.
> For example, i have a class say
> TestPerspective {
> @Test
> public void testMenu1() {}
> public void testMenu2() {}
> public void testMenu3() {}
> }
>
> Now i would expect the test cases to run in the below order:
> testMenu1()
> testMenu2()
> testMenu3()
>
> However they run as testMenu3()
> testMenu1()
> testMenu2()
>
> How can i make them run in the order specified?
Unfortunately, you can't... this is just impossible with JUnit. But, in
my experience, if you have tests that depends on each other, it is a
sign the your test suite have a problem. It is generally a bad pattern
to make tests depends on each other, since it can result on some weird
behavior sometimes. I might not be the best person to explain the
concept of test independence and all that is related, but as a rule of
thumb I try to avoid this kind of thing.
I also know that if testMenu1() does some general things, such as
setting some fields or creating some resources, you might want to use a
@Before method: it will be executed before every test you run. You would
also need an @After method to do some clean-up, because such a method
gets executed after each test you run. There is also @BeforeClass and
@AfterClass, which gets executed once for the whole test class. So let's
say you have:
TestPerspective {
@Test
public void testMenu1() {}
@Test
public void testMenu2() {}
@Test
public void testMenu3() {}
@Before
public void before() {}
@After
public void after() {}
@BeforeClass
public static void createResources() {}
@AfterClass
public static void deleteResources() {}
}
you'll get the execution:
createResources()
before()
one of the test method
after()
before()
another test method
after()
.... snip ...
before()
last test method
after()
deleteResources()
Look for the javadoc of those annotations for more in-depth explanation.
Hope this helps.
--
Pascal Gélinas | Software Developer
*Nu Echo Inc.*
http://www.nuecho.com/ | http://blog.nuecho.com/
*Because performance matters.*
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02282 seconds