2

I have tests that are depend on each other, and I want to select there order.
The code very long and call them from one method is unrealistic and impossible.

Is there any way that the tests will be run, in order as the test methods appears in class file, or in the order that I want?

EDIT
What can I do if I want to test database connection class. I will need to test 'add' 'delete' and 'update', and also test some methods that use data from database?

nrofis
  • 8,975
  • 14
  • 58
  • 113

2 Answers2

3

Unit Tests should be independent of each other. It sounds like you are testing multiple methods which are highly dependent on each other. I suggest looking into Dependency Injection and mock objects to remove some of that dependency and allow you to test one method at a time.

Community
  • 1
  • 1
Rumpel
  • 56
  • 3
  • I've never done database testing personally. I would say you could either mock out the database calls and ensure the methods which insert/update/delete get called at the correct times, or create a separate test database. The method using database data should be unit tested independently. If these methods are using global data that is populated from the database calls then testing it will be difficult and it should be re-factored. – Rumpel Nov 16 '13 at 02:28
2

Test methods cannot depend on each other. Before each test starts methor protected void setUp() throws Exception{}. After each test starts method protected void tearDown() throws Exception {} You can use this methods for initialization/clearing common object fields or logic.

resource8218
  • 1,445
  • 1
  • 20
  • 33