Unit Testing

4518
4
09-10-2013 01:56 PM
Matrix_Solutions_Inc_Geomatics
New Contributor III
Hi,

Our python script library is starting to get large and complex to the point that we are introducing unit testing to keep problems manageable.
Has anyone had any experience creating Unit Tests for their python. How did you mock ArcGIS objects?

Thanks
Tags (2)
0 Kudos
4 Replies
JamesCrandall
MVP Frequent Contributor
Hi,

Our python script library is starting to get large and complex to the point that we are introducing unit testing to keep problems manageable.
Has anyone had any experience creating Unit Tests for their python. How did you mock ArcGIS objects?

Thanks


Do you have a specific example in mind?  I'm interested in this topic myself, but since the number of arcpy developers in our org is very small, the code base and testing we conduct is very project-focused and simply not much need to unit test a bunch of related libraries. 

My limited unit testing is usually kept to small items and not related to GIS objects.  So things like string validation or variation tests might be done, but things such as database objects (cursors, rows of things, sql statements, etc...) are just not good candidates in my experience and I simply setup smaller functions/methods to perform a test (this allows me to clearly define connection and close operations to make sure things get killed properly).
0 Kudos
deleted-user-st4HsVxccxgs
New Contributor III

For arcpy functions, I use the mock library. Here is a basic example:

import scriptRef
import unittest
from mock import mock

class ScriptUnitTests(unittest.TestCase):

   @mock.patch('arcpy.da')

   def test_when_starting_session_should_start_edtior(self, mock_da):

         variables = " "

         scriptRef.scriptMethod(variables)

         mock_da.Editor.assert_called_with(variables)

I also recommend this article for helpful arcpy testing strategies: 

Testing with ArcPy: Isolation and Mocking· Notes from the Lifeboat 

0 Kudos
ClintonDow1
Occasional Contributor II

Thanks that's a great tutorial! I hadn't seen it before.

0 Kudos
ClintonDow1
Occasional Contributor II

In general its much easier to test scripts when they have been heavily modularized. The .py file that the toolbox points to, or the .pyt file for Python toolboxes can both be very 'thin' in that they only need to handle the importing of arcpy and the parameter i/o, then the rest of the logic split across several modules which are imported. This makes testing those modules easier, as they can be separated into those which require arcpy and those that don't.

For example, rather than having a section of the script which processes each row in a feature class within a loop, break that logic out of the main script into an arcpy-bound function which takes a path to a table and list of fields, then generates rows from the table via an arcpy.da.cursor. This function can easily be mocked to return tuples, because it can be called independently of the main script. This also allows you to easily debug this code in an IDE, since it can be run completely independently of the ArcGIS UI at that point. 

0 Kudos