Pages

Monday, April 4, 2011

QTP Coding standards and guidelines.

Why guidelines & standards?
Any good automation code needs guidelines or coding standards to be followed to be readable, presentable and at the same time ensure it is executable. The coding standard brings in the good rules being followed across projects into the project and hence value adds to the project. It also decreases the maintenance cost, as it is well documented and readable.

Again, I've seen a lot of my subscibers asking what coding standard should we follow in QTP. I have put together these best practices and I believe it will be of help to you guys. More importantly, if you think I can add something here let me know by commenting, I will do the needful.

1. Indentation: This is one of the basic area we should focus upon. It should be used to clearly define and allow easy access to different sections of the scripts, loops, conditional statements and Functions/Sub Routines, etc.

2. Hard-coding: This again is a basic area and can make your code hard to be maintained. Hardcoding has to be avoided at all possible scenarios. Particularly hard-coded paths and machine names should be avoided in test scripts. This makes them very difficult to move from one machine or environment to another. Instead all those will be kept into a excel file and can be used in the scripts as variable. This would be very helpful when we have many environments where the script has to be run.

3. Suppress user messages: While coding we have a tendency to use message boxes and input boxes for the purpose of debugging. We should ensure that unnecessary user messages should be suppressed. – Delete or comment out all debug user messages, trial code routines. This will ease the batch runs for scripts and ensure proper execution of those.

4. App sync issues: While running the script synchronization problem may occur. This synchronization problem should be covered with code only, i.e. the delay for window synchronization should not be set under Settings-> General options. It should be done with the help of coding whenever needed. Usage of wait statements should again be avoided and try to use the .sync method where ever possible.

5. Script execution interruption: The script should complete its run even though verification/check fails. This has to be taken care through your code and ensure that the application crash or any unforeseen incidences are not breaking the script execution.Consider debugging scenarios while coding itself (Date, time stamps, Level of Pausing etc.)

8. QTP settings changes in scripts: If a script needs some QTP settings to be changed, it should be done programmatically so that other scripts are not affected.

9. Comments: There should be proper comments in the scripts. Comments for each line are not required. Instead, use comments for groups of closely related lines or whenever the code is confusing or may be difficult to understand.