Pytest fails with access violation?

I’m building a project in python with the Orekit python wrapper, and want to use pytest for unit testing. When setting up fixtures that initialize the orekit/java vm, pytest fails reporting an “access violation” (on windows). Has anyone had success with using pytest in a project that includes Orekit?

The test fixture code is:

from arcsim.models import Scenario
def new_scenario():
   sce = Scenario("Test Scenario")
   return sce

The unit test code is:

class TestScenario:
    def test_repr(self, new_scenario):
        out = new_scenario.__repr__()
        assert "arcsim.models.Scenario object" in out

And the resulting pytest output is:

(arcsim) G:\My Drive\Satellite Architecture\Python\ArcSim>pytest -v
========================== test session starts ======================
platform win32 -- Python 3.7.7, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
cachedir: .pytest_cache
rootdir: G:\My Drive\Satellite Architecture\Python\ArcSim
plugins: cov-2.10.0
collected 1 item

test/test_models.py::TestScenario::test_repr Windows fatal exception: access violation
Current thread 0x00002e10 (most recent call first):
File "G:\My Drive\Satellite Architecture\Python\ArcSim\arcsim\models.py", line 55 in __init__
File "G:\My Drive\Satellite Architecture\Python\ArcSim\test\conftest.py", line 55 in new_scenario
File "C:\Users\EmRa\.conda\envs\arcsim\lib\site-packages\_pytest\fixtures.py", line 792 in call_fixture_func
[ ... and so on ... ]

Line 55 in the models.py file is the first time an Orekit function is called to build the Earth model. The source is below (I took out comment lines to make it shorter):

# orekit init
import orekit
from orekit.pyhelpers import setup_orekit_curdir
vm = orekit.initVM()
setup_orekit_curdir('data/orekit-data.zip')

# space dynamics libraries
# https://www.orekit.org/site-orekit-development/apidocs/index.html
from org.orekit.frames import FramesFactory, TopocentricFrame
from org.orekit.utils import Constants, IERSConventions
from org.orekit.time import TimeScalesFactory, AbsoluteDate
from org.orekit.bodies import OneAxisEllipsoid, GeodeticPoint

class Scenario():
    def __init__(self, name):
    self.name = name

    ITRF = FramesFactory.getITRF(IERSConventions.IERS_2010, True)
    self.Earth = OneAxisEllipsoid(Constants.WGS84_EARTH_EQUATORIAL_RADIUS,
                                  Constants.WGS84_EARTH_FLATTENING, ITRF)

Is there a way to use calls to the orekit vm in pytest without causing access violations? Thanks!

Hi Sydra,

There has been some issues with testing in PyCharm as dicsussed in issue #427. I don’t think I’ve seen this outside pycharm (and in PyCharm it is possible to practically ignore the error as the tests themselves are executed, which has led me to think it is more related to PyCharm).

But you are running this as a standalone command it looks?

Hi Petrus, yes I’m running pytest directly from the command line. You are correct though that the test cases continue to run following the “access violation” error. I haven’t validated the returned data yet, but at least all code executes without further errors. I’ve attached my environment file in case that’s helpful
environment.yml (4.9 KB)

After looking at the issue ticket you referenced, I’ve also tried specifying the location of the Orekit data file using the os.path constructor, but unfortunately that didn’t resolve the problem.

Hi Sydra,

Good to know this is happening also in pytests, this limits the sources of error with one piece…

I have a very unconfirmed feeling that there is something in the less commonly used parts of the wrapping that does not work as expected, something related to inspection with variables, as we see something similar when running pycharm debugger in asynchronous monitoring of variables. In case it is in the wrapping it is something that is rarely used by Python & IDE’s as we have not seen this in jupyter/ipython/spyder etc. Would be good to track down this bug, but one needs to do it in a less complex wrapping than orekit and isolate it.

If you have the time, could you please create a small failing test case and file an issue at
the issue tracker?

Regards
/Petrus

Sorry it took me a while to put this together; I’ve created an issue here: