Preconditions and Teardowns are a nice way of organizing WET tests in such a way that it mimics a real test case. You may define one or more preconditions for a test. Each one of these preconditions is executed before the transactions are run. Even if one of the preconditions fails, then the test stops without executing. Not only does this mechanism allow testers to reuse common tests as preconditions, but it also clearly demarkates between a failed test and one that cannot be run - For example,
You have a test to move messages across folders. This test requires that the user must be logged in.
If the login action fails, It does not amount to the 'move message across folders' test failing! In such a case, WET prints out a message stating that the precondition to login failed and therefore the test was not executed.
How to create Precondition tests?
Any Library test (reusable test) can become a candidate for a Precondition. Nothing more! Nothing less! To understand how Library tests work see the article Library Tests
How to call Preconditions?
Preconditions are called by defining them within the test definition. Recall the snippet of the test definition which defines preconditions:
[Precondition1] name = P1 path = /lib/reusable_tests/login param1 = pre_param1 param2 = pre_param2 [Precondition2] name = P2 path = /lib/reusable_tests/nav_to_home param1 = pre_param1 param2 = pre_param2 param3 = pre_param3
You can have zero or more such preconditions in any test definition. For each of these definition, the following is the significance of each parameters:
- name = A short name describing what this precondition is meant for
- path = The path to the reusable test which is to be used as the precondition
- param1 = The first parameter to be passed to the precondition library test
- param2 = The second parameter to be passed
- (you can define as many parameters as required by the reusable script)
To understand how Parameterization works see the article on Parameterizing Tests
How do I use the WET UI to setup preconditions?
In the WET UI, by default one precondition is created. This precondition is a 'dummy' precondition - that is, it doesn't point to any Reusable test. To use this as the first precondition:
- Open the Test Definition Window
- Click on the First Precondition Treeview node on the left hand side
- Change the treenode's label to the required name of the precondition
- On the Right hand side, in the Description text area, give a brief description about the precondition
- Change the path to point to the test definition that represents the 'reusable test' for this precondition. Make sure that you have created a 'reusable test' by following the guide at Library Tests
- Set the parameters to be passed to the preconditions in parameters datagrid view.
- That's it!
Teardowns
Teardowns are exactly similar to preconditions. The only difference is that the teardowns are run after all the transactions as opposed to preconditions which are run before the test starts. In short, Preconditions make sure that all the steps required before running a test are executed before starting the test, while teardowns bring the Application Under Test back to the state where it started.
