Select to view content in your preferred language

Problem with AGP Python Toolbox executing Unittest

623
3
Jump to solution
10-24-2023 01:35 AM
BernhardEhrminger
New Contributor III

In Module FileCount.py I have a simple Python Unittest, which tests that the number of files in current directory is greater than zero, so the test should never fail.

 

 

import unittest
import pathlib


class Test(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_file_count(self):
        self.assertGreater(
            len(list(pathlib.Path().cwd().glob('*'))), 0)
        pass

 

 

 

The Python Tool-Class referencing the Unittest is like:

 

 

class DemoUnittest(object):

    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Demo_Unittest"
        self.description = "Execute a Unittest"
        self.canRunInBackground = True

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal validation is performed.  This method is called whenever a parameter has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        try:
            self.result = unittest.TextTestRunner().run(
                unittest.TestLoader().loadTestsFromModule(
                    module = FileCount))
            arcpy.AddMessage(self.result)
        except Exception as e:
            arcpy.AddMessage(str(e))
        return True

    def postExecute(self, parameters):
        arcpy.AddMessage(self.result)
        """This method takes place after outputs are processed and added to the display."""
        return

 

 

When running the Python Tool in ArcGIS Pro I get the following result:

2023-10-24 10_21_32-Clipboard.png

So from the screen dump above, I conclude the Unittest performed as expected (OK).

But why is ArcGIS Pro telling me erroneously  "Failed to execute (DemoUnittest)"?

Thank You for your attention.

0 Kudos
1 Solution

Accepted Solutions
BernhardEhrminger
New Contributor III

the class constructor unittest.TextTestRunner has an argument stream, which can be initialized to sys.stdout. In this case the problem with failing tools does not exist.

View solution in original post

0 Kudos
3 Replies
Luke_Pinner
MVP Regular Contributor

The default output stream for unittest.TextTestRunner is sys.stderr and I don't know and haven't tested, but  perhaps ArcGIS notices this and assumes there's been an error?

BernhardEhrminger
New Contributor III

the class constructor unittest.TextTestRunner has an argument stream, which can be initialized to sys.stdout. In this case the problem with failing tools does not exist.

0 Kudos
BernhardEhrminger
New Contributor III

thank you Luke, the situation is like you pointed out.

0 Kudos