Source code for my presentation at the 2017 dev summit on unit tests for ArcToolbox Tools
This project provides a simple toolbox tool written as a PYT and a set of tests and corresponding test data used to demonstrate how to build tests for toolbox tools.
A simple tool that meets the following requirements:
- Input - Points with a "Type" field either "RED" or "BLUE"
- Input - Polygons
- Output - A copy of the Polygon input with "Red_Count" and "Blue_Count" fields that contain the number of "RED" points and the number of "BLUE" points in that polygon.
This is achieved by the following process:
- Filter the input points using a feature layer and where clause
- Use a spatial join to join the polygons and filtered points into a new temporary layer
- Alter the field name "Join_Count" (provided by SpatialJoin) to have the correct name either "Red_Count" or "Blue_Count"
arcpy.MakeFeatureLayer_management(points.Value, "red_points", "Type='{}'".format("RED"))
polygon_red_count = join("%scratchGDB%", "red_count")
arcpy.SpatialJoin_analysis(polygons.Value,
"red_points",
polygon_red_count,
"JOIN_ONE_TO_ONE",
"KEEP_ALL",
None,
"INTERSECT")
arcpy.AlterField_management(polygon_red_count, "Join_Count", "Red_Count", "Red Count")
The process above provides a simple process to test, as a result the tests can be simple as well. The tests and categories provided in this file are:
- Edge Cases
- All Blue Points
- All Red Points
- No Points
- No Points, but there are points of other "types"
- General Data - A random set of points in a grid. A substitute for building tests on real data.
- Exceptions
- Passing polygons into the point input
- Passing points into the polygon input
- No Type field