JKI Discussion Forums
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to use setUp and tearDown Rate Topic: -----

#1
User is offline   danielle 

  • Group: Members
  • Posts: 3
  • Joined: 13-September 10
Hi,

I'm new to unit testing and I'm a bit confused on how to use setUp and tearDown. It seems as though setUp is run before every test, and not once before all the tests. Therefore, it makes sense that there should be a way to choose the setUp according to the test being run. For example: If running a test called testCloseProg that closes a program, setUp should open the program, and for other tests not (or another example). I could find no way of doing this. In that case, why is setUp run before each test?

Thanks,
Danielle
0 kudos

#2
User is offline   Omar Mussa 

  • Group: JKI Team
  • Posts: 88
  • Joined: 12-October 06

View Postdanielle, on 26 December 2010 - 11:30 AM, said:

It seems as though setUp is run before every test, and not once before all the tests.


By Design:
TestCase.setUp runs before each test in the TestCase.
TestSuite.setUp runs once per TestSuite.

So, if you need to do something once before all of your tests run, add your TestCase to a TestSuite and write code in your TestSuite.setUp method.

View Postdanielle, on 26 December 2010 - 11:30 AM, said:

For example: If running a test called testCloseProg that closes a program, setUp should open the program, and for other tests not (or another example). I could find no way of doing this. In that case, why is setUp run before each test?


If your setUp opens the program, then your tearDown should close the program and then you can use your program for each test in an independent test harness. If you want to test the Close method of your program, you can either write another test that closes the program called 'testCloseProg' and ignore close errors in your tearDown or you can open a separate context for your program for the specific testCloseProg test method that is not used by other tests.
1 kudos

#3
User is offline   danielle 

  • Group: Members
  • Posts: 3
  • Joined: 13-September 10

View PostOmar Mussa, on 26 December 2010 - 05:40 PM, said:

By Design:
TestCase.setUp runs before each test in the TestCase.
TestSuite.setUp runs once per TestSuite.

So, if you need to do something once before all of your tests run, add your TestCase to a TestSuite and write code in your TestSuite.setUp method.



If your setUp opens the program, then your tearDown should close the program and then you can use your program for each test in an independent test harness. If you want to test the Close method of your program, you can either write another test that closes the program called 'testCloseProg' and ignore close errors in your tearDown or you can open a separate context for your program for the specific testCloseProg test method that is not used by other tests.


Thanks!

However, something is still unclear: Is there a way to have TestCase.setUp be different for each test?
Also, what do you mean by "open a separate context for your program for the specific testCloseProg test method that is not used by other tests"?

Thanks again,
Danielle
0 kudos

#4
User is offline   Jim Kring 

  • Group: JKI Team
  • Posts: 1,204
  • Joined: 15-March 06

View Postdanielle, on 29 December 2010 - 12:24 PM, said:

Thanks!

However, something is still unclear: Is there a way to have TestCase.setUp be different for each test?
Also, what do you mean by "open a separate context for your program for the specific testCloseProg test method that is not used by other tests"?

Thanks again,
Danielle



Hi Danielle,

You should make a separate TestCase class for every different set of setUp and tearDown methods you'll need and make your tests members of the TestCase class whose setUp and tearDown methods they need.

In general, there are a lot of strategies and patterns for organizing tests. For example: it is very common to create one TestCase class per feature of your software under test and to create one TestCase class for every class in your software under test.

Bottom line, don't be afraid to create more than one TestCase :) In fact, you could have one TestCase for every single test, but you'll often find that you'll want to reuse your setUp and tearDown methods, which is the reason to start grouping several test methods into the same TestCase.

Thanks,

-Jim
0 kudos

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic