Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Testing of Python Measures with standard OpenStudio install #4906

Closed
DavidGoldwasser opened this issue Jun 2, 2023 · 3 comments · Fixed by #4973
Closed

Support Testing of Python Measures with standard OpenStudio install #4906

DavidGoldwasser opened this issue Jun 2, 2023 · 3 comments · Fixed by #4973
Assignees
Labels
Enhancement Request Triage Issue needs to be assessed and labeled, further information on reported might be needed

Comments

@DavidGoldwasser
Copy link
Collaborator

DavidGoldwasser commented Jun 2, 2023

Enhancement Request

example Python measures included with OpenStudio use pytest for the measure tests. pytest isn't included in the EnergyPlus Python that is used by the OpenStudio CLI when openstudio labs my_test.py is run.

Detailed Description

While a clean OpenStudio installer can run OSW file with labs that runs fine, there isn't way currently to run measure tests. We want to make it easy for novice measure writes to write and use tests when developing or altering Python measures, as we have done for Ruby measures. The goal here is to provide update the example measure tests, and make any changes necessary so the example Python measure, and other Python measures that do not use external packages can be tested with out of the box OpenStudio installer.

Possible Implementation

Preferred solution is to change to use unittest and fix issues that exist. When I try to import unittest on non-developer mac OpenStudio I get the following error. I have not tested on Windows.

  File "/Users/dgoldwas/Documents/github/nrel/openstudio-common-measures-gem/lib/measures/IncreaseRoofRValuePython/tests/test_measure.py", line 2, in <module>
    import unittest
  File "/Applications/OpenStudio-3.6.0/EnergyPlus/python_standard_lib/unittest/__init__.py", line 60, in <module>
    from .async_case import IsolatedAsyncioTestCase
  File "/Applications/OpenStudio-3.6.0/EnergyPlus/python_standard_lib/unittest/async_case.py", line 1, in <module>
    import asyncio
  File "/Applications/OpenStudio-3.6.0/EnergyPlus/python_standard_lib/asyncio/__init__.py", line 8, in <module>
    from .base_events import *
  File "/Applications/OpenStudio-3.6.0/EnergyPlus/python_standard_lib/asyncio/base_events.py", line 23, in <module>
    import socket
  File "/Applications/OpenStudio-3.6.0/EnergyPlus/python_standard_lib/socket.py", line 49, in <module>
    import _socket
ModuleNotFoundError: No module named '_socket'

If we can't get unittest to work, it would be fine as an alternative to add pytest to OpenStudio's Python, so that the existing example Python Measure tests work as written.

@jmarrec
Copy link
Collaborator

jmarrec commented Jul 4, 2023

I'd ideally like to actually bundle some packages into the CLI (install them in the energyplus standard_lib folder), some of which are native. I'm thinking pytest, jinja2 (templating engine similar to ERB, would help @joseph-robertson with #4858 too), maybe numpy and pandas.
cf #4868 as a premise, and the folllowup branch I plan on making a PR when #4868 is merged: 38eed72

@kbenne
Copy link
Contributor

kbenne commented Jul 5, 2023

Yeah putting them into the EnergyPlus library folder makes sense to me. The "how" we do this seems more unknown to me. The Python developers on our team seem to be using Poetry a lot. Do we start a poetry project for the things we want to bundle with OS?

@jmarrec
Copy link
Collaborator

jmarrec commented Jul 5, 2023

Just pip install --target=${ENERGYPLUS_DIR}/python_standard_lib --upgrade jinja2 numpy pandas pip. We won't suffer from the same issue as ruby with the MSVC v Mingw, so it should work as-is (I tested on linux, mac, windows)

People can always use the --python_path CLI variable to load anything else they might need.

kbenne added a commit that referenced this issue Sep 19, 2023
In the Python unittest library, unittest.main() was not working as
expected, because tests defined in the __main__ scope where not being
detected. This meant that `openstudio labs execute_python_script
foo_measure_test.rb` did not work as expected.

The solution here is to register the __main__ module properly within
Python.

* An example Measure test is located at NREL/openstudio-common-measures-gem@0c73cdb
* A Measure test can be invoked with `openstudio labs --python_path=. execute_python_script tests/test_measure.py`

close #4906
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Request Triage Issue needs to be assessed and labeled, further information on reported might be needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants