Odd quirk of running nupic tests (steps to reproduce included)

I’m running into yet another inexplicable quirk, this time with tests. Consider the following simple test file (let’s call it network_api_test.py):

from nupic.engine import Network, Dimensions



def testSimpleTwoRegionNetwork():
  network = Network()

  region1 = network.addRegion("region1", "TestNode", "")
  region2 = network.addRegion("region2", "TestNode", "")

  network.link("region1", "region2", "TestFanIn2", "")

  r1dims = Dimensions([6, 4])
  region1.setDimensions(r1dims)

  network.initialize()

  r2dims = region2.dimensions
  print r2dims



if __name__ == "__main__":
  testSimpleTwoRegionNetwork()

When I run it directly, it get the following (expected) output:

$ python tests/unit/nupic/engine/network_api_test.py 
[3 2]

And, when invoked w/ py.test, I get passing results:

$ py.test tests/unit/nupic/engine/network_api_test.py 
======================================================================================================================================================================= test session starts ========================================================================================================================================================================
platform darwin -- Python 2.7.10 -- pytest-2.5.1
plugins: xdist, cov
collected 1 items 

tests/unit/nupic/engine/network_api_test.py .

===================================================================================================================================================================== 1 passed in 0.03 seconds =====================================================================================================================================================================

However, when I run the same test invoking test discovery, I get an unexpected failure originating from the swig binding:

$ py.test tests/unit/nupic/engine -k testSimpleTwoRegionNetwork
======================================================================================================================================================================= test session starts ========================================================================================================================================================================
platform darwin -- Python 2.7.10 -- pytest-2.5.1
plugins: xdist, cov
collected 18 items / 1 skipped 

tests/unit/nupic/engine/network_api_test.py F

============================================================================================================================================================================= FAILURES =============================================================================================================================================================================
____________________________________________________________________________________________________________________________________________________________________ testSimpleTwoRegionNetwork ____________________________________________________________________________________________________________________________________________________________________

    def testSimpleTwoRegionNetwork():
      network = Network()
    
      region1 = network.addRegion("region1", "TestNode", "")
      region2 = network.addRegion("region2", "TestNode", "")
    
      network.link("region1", "region2", "TestFanIn2", "")
    
      r1dims = Dimensions([6, 4])
      region1.setDimensions(r1dims)
    
      network.initialize()
    
>     r2dims = region2.dimensions

tests/unit/nupic/engine/network_api_test.py:18: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <nupic.engine.Region object at 0x111388ed0>

    def _getDimensions(self):
      """Dimensions of the region"""
>     return Dimensions(tuple(self._region.getDimensions()))

src/nupic/engine/__init__.py:498: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <nupic.bindings.math.SwigPyIterator; proxy of <Swig Object of type 'swig::SwigPyIterator *' at 0x11256a8d0> >

    def next(self):
        """next(self) -> PyObject *"""
>       return _math.SwigPyIterator_next(self)
E       RuntimeError: Unknown error from C++ library

../nupic.core/bindings/py/nupic/bindings/math.py:182: RuntimeError
====================================================================================================================================================== 17 tests deselected by '-ktestSimpleTwoRegionNetwork' =======================================================================================================================================================
======================================================================================================================================================== 1 failed, 1 skipped, 17 deselected in 0.32 seconds ========================================================================================================================================================

Meanwhile, I get passing results if I start at a higher directory:

$ py.test tests/unit/nupic -k testSimpleTwoRegionNetwork
================================================================================== test session starts ==================================================================================
platform darwin -- Python 2.7.10 -- pytest-2.5.1
plugins: xdist, cov
collected 763 items / 5 skipped 

tests/unit/nupic/engine/network_api_test.py .

================================================================ 762 tests deselected by '-ktestSimpleTwoRegionNetwork' =================================================================
================================================================== 1 passed, 5 skipped, 762 deselected in 0.91 seconds ==================================================================

It’s as if there’s something about how py.test runs that somehow interferes with the ability to access dimensions on a region. I get a similar failure when I run py.test tests/unit/nupic/engine/network_test.py which resolves itself if I run py.test tests/unit/nupic.

I’m guessing it has to do with how py.test sets up the Python import paths. It seems likely that whatever path is added causes the wrong thing to be imported at some point (since it will be used for all imports, not just those in the test file). You could print sys.path to see what it looks like (before the offending line in your test file).