What are the benefits of "setup" and "teardown"

Ask general questions here.
Posts: 6
Joined: Tue Mar 14, 2017 2:50 pm

What are the benefits of "setup" and "teardown"

Post by Parapluie » Fri Mar 31, 2017 1:58 pm

Hey All,

Let's assume the following scenario. I have test suite in which I want to have only one test case. The test case should be data-driven test case. I have added a setup and teardown. When I execute the test suite, it is executed once and than it goes for a second execution(not only for the data-driven case but also for the setup). I want the setup and the tear down to be executed only one time. I have resolved the issue using a test case in the main test case. But I have been wondering if is it possible to solve the issue without using the inner test case. Is this not the main purpose of "setup" and "teardown" ?

1) The setup is executed for each iteration
scenario3.png (7.61 KiB) Viewed 2657 times
2) The setup is executed only once
scenario1.png (11.61 KiB) Viewed 2657 times
Thanks in advance!

Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: What are the benefits of "setup" and "teardown"

Post by krstcs » Fri Mar 31, 2017 3:38 pm

The setup section is mainly for grouping modules that should be at the start of a testcase. You don't have to use it as it gives no specific benefit in testcases. For the suite, the setup allows you to place modules at the suite level without having a test case there.

Teardown, however, is very important. It ALWAYS runs, regardless of test status. So, even if your test case fails, the teardown will run. This is handy for cleaning up after a run, or for doing things that you need to do even if the test case doesn't pass.

If you understand code, think of it as a try...catch...finally block in code. The try is the case, the catch is if there is a failure, and the finally always runs, even if there's an exception.

In your situation, you should use the nested test cases like your second example. That is the correct way to do it.
Shortcuts usually aren't...