A perfectly structured automation Testcase should consist of four parts. They are:
Why we should do like this?
- Setup or precondition
- main test
- Cleanup or post condition
- Optional code.
Precondition: All test data ot steps needs to be there in application or needs to be performed so that current test run/execute smoothly.
Main test: In this area the main objective of test case is being executed.
Cleanup: Here we delete all the testdata that is being created in the main test section.In other word we try to restore the out of box condition of the application. Why this is required?? Any accidental data pollution can be rectified.
Optional Area: All other user defined functions go here.
Class testcase1
{
precondition()
{
}
main()
{
functionx()
}
cleanup()
{
}
functionx()
{
}
}
{
precondition()
{
}
main()
{
functionx()
}
cleanup()
{
}
functionx()
{
}
}
Why we should do like this?
- Well, while developing or refactoring we must understand our test goal.Each testcase should address only one requirement. Multiple requirement coverage in a single automation test may lead to confusion. It will hamper the traceability matrix as well.
- By implementing the 3/4 folds we can reduce junk code creation. More encapsulation with less junk.
- By implementing such structure we are making testcase independent. There is no dependency such that testcaseX needs to be executed in order run current testcase.So pulling of any testcase and re execution is possible.
- Also, we must check if we are generating redundant code /duplicate code.The more we will reduce the duplicate code more our test will become mature.A premature optimization is the main problem area of the organization.