How I Build Geoprocessing Tools: #12 Testing
We all like to get positive feedback for the work we do. I appreciate those of you who have told me that you have been enjoying this blog. I hope to continue to provide you with useful content in readable format. Negative feedback is equally useful but not as enjoyable. Getting praise and being alerted to my faults helps to direct and finish me. I hope to turn a corner on this series and finish the MOVE tool to distribute to you for your own use and customizing, but we've got a couple more steps...
I finished the first draft of my script, but I have no idea if it works?! I need to test it. I will test the script file within the PythonWin environment rather than launching the script tool inside ArcGIS that would in turn call the script file. I will use the tools within the PythonWin environment to help with any debugging.
As part of my regiment of testing I print out the key variable values within my script to the PythonWin interactive window to get feedback on how my tool is behaving. Later I use the GP.AddError method to write debugging messages to the geoprocessing message window when I'm testing the tool inside ArcGIS. Sometimes these feedback comments are temporary and I remove them before I distribute the sample, other times they are useful feedback to the user while the function is processing, especially for longer running tools it gives some sense of the progress.
Another handy function of the PythonWin interactive window is that once I've loaded the ESRI geoprocesser I can type geoprocessing commands directly into it, and get results. I can test out a single line, or just a couple lines of code by cutting and pasting them into the window.
I will hard-code the input variables directly to string and numeric values with equality statements in thbeginningng of my script and then comment-out the statements that are attempting to get the arguments from a calling function. Hard-coding the inputs for testing will make re-running the function easy since I won't have to enter any inputs. I can change and re-run the script and get feedback instantly. Once the tool is functioning for my test data I can try other data and/or test my script with the inputs coming from the geoprocessing tool, instead of just running the script code directly inside PythonWin.
During testing I will muddle through typographical, syntax and spelling errors... I will probably have a couple of logical errors before its all said and done. Barring any show-stopping design flaws I'll finish with the Simple Matter Of Programming (SMOP).
I will test this function with the original use case: to modify the coordinates of a Point feature class. I originally needed a tool to adjust the position of points to use for defining the insertion points for CAD TEXT entities created using the EXPORT TO CAD tool. I can create CAD TEXT using the optional functionality of the EXPORT TO CAD tool by including a CADType field in that point feature class set to a string value of "TEXT" and valid text values in a field called TxtValue. Since I want the CAD text to be located near, but not on the point features, my MOVE tool will create offset points for this purpose.
Once I've got it working I may add additional code to make it work with any type of features, POINTS, LINES, POLYGONS... I am already thinking how this tool can be repackaged into multiple tools using the same basic logic, but with different interfaces.