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
JKI Discussion Forums
Page 1 of 1
How to use setUp and tearDown
#2
Posted 26 December 2010 - 05:40 PM
danielle, 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.
danielle, 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.
#3
Posted 29 December 2010 - 12:24 PM
Omar 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.
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
#4
Posted 01 January 2011 - 12:50 AM
danielle, 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
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
Thanks,
-Jim
Share this topic:
Page 1 of 1




Back to top








