Jump to content
danielle

How to use setUp and tearDown

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

 

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.

  • Upvote 1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.