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

Improve OpenStudio's support for Python unitttest #4973

Merged
merged 1 commit into from
Sep 19, 2023

Conversation

kbenne
Copy link
Contributor

@kbenne kbenne commented 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.

close #4906

Pull request overview

  • Fixes #ISSUENUMBERHERE (IF THIS IS A DEFECT)

Pull Request Author

  • Model API Changes / Additions
  • Any new or modified fields have been implemented in the EnergyPlus ForwardTranslator (and ReverseTranslator as appropriate)
  • Model API methods are tested (in src/model/test)
  • EnergyPlus ForwardTranslator Tests (in src/energyplus/Test)
  • If a new object or method, added a test in NREL/OpenStudio-resources: Add Link
  • If needed, added VersionTranslation rules for the objects (src/osversion/VersionTranslator.cpp)
  • Verified that C# bindings built fine on Windows, partial classes used as needed, etc.
  • All new and existing tests passes
  • If methods have been deprecated, update rest of code to use the new methods

Labels:

  • If change to an IDD file, add the label IDDChange
  • If breaking existing API, add the label APIChange
  • If deemed ready, add label Pull Request - Ready for CI so that CI builds your PR

Review Checklist

This will not be exhaustively relevant to every PR.

  • Perform a Code Review on GitHub
  • Code Style, strip trailing whitespace, etc.
  • All related changes have been implemented: model changes, model tests, FT changes, FT tests, VersionTranslation, OS App
  • Labeling is ok
  • If defect, verify by running develop branch and reproducing defect, then running PR and reproducing fix
  • If feature, test running new feature, try creative ways to break it
  • CI status: all green or justified

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
@jmarrec
Copy link
Collaborator

jmarrec commented Sep 19, 2023

I don't want to sound like a broken record, but I would urge we standardize around pytest and not unittest. yes, I now this isn't part of the standard library, but I think we should just pip install it into the EnergyPlus/python_standard_lib directory.

module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a link to somewhere where they do this sys.modules["__main__"] = module or something to convince me this is the right way to do it? Don't want to go chasing info if you already have it.

I have a completely non-justified feeling this might cause trouble in some edge case we haven't foreseen, when this isn't a unittest file or something.

Copy link
Contributor Author

@kbenne kbenne Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could be right. Here is my justification though.

https://docs.python.org/3/library/__main__.html#import-main

Python inserts an empty __main__ module in [sys.modules](https://docs.python.org/3/library/sys.html#sys.modules) at interpreter startup, and populates it by running top-level code....

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmarrec jmarrec added Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge. component - Python bindings labels Sep 19, 2023
Copy link
Collaborator

@jmarrec jmarrec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming CI comes clean, this will merge

@jmarrec
Copy link
Collaborator

jmarrec commented Sep 19, 2023

Just the usual suspect OpenStudioCLI.Labs.test_logger_rb (Failed) on windows. Thanks @kbenne

@jmarrec jmarrec merged commit f1590e2 into develop Sep 19, 2023
4 of 6 checks passed
@jmarrec jmarrec deleted the 4906-python-unittest branch September 19, 2023 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component - Python bindings Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Testing of Python Measures with standard OpenStudio install
3 participants