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

Remove test file runnability #5809

Open
trexfeathers opened this issue Mar 11, 2024 · 2 comments
Open

Remove test file runnability #5809

trexfeathers opened this issue Mar 11, 2024 · 2 comments

Comments

@trexfeathers
Copy link
Contributor

Historically our test files have been runnable as independent Python scripts e.g. python test_pp_cf.py. We even customise the help message e.g. python test_pp_cf.py --help.

This relies on the block below, plus the inclusion of if __name__ == "__main__": tests.main() in every test file.

if "--data-files-used" in sys.argv:
sys.argv.remove("--data-files-used")
fname = "/var/tmp/all_iris_test_resource_paths.txt"
print("saving list of files used by tests to %s" % fname)
_EXPORT_DATAPATHS_FILE = open(fname, "w")
else:
_EXPORT_DATAPATHS_FILE = None
if "--create-missing" in sys.argv:
sys.argv.remove("--create-missing")
print("Allowing creation of missing test results.")
os.environ["IRIS_TEST_CREATE_MISSING"] = "true"
def main():
"""A wrapper for unittest.main() which adds iris.test specific options to the help (-h) output."""
if "-h" in sys.argv or "--help" in sys.argv:
stdout = sys.stdout
buff = io.StringIO()
# NB. unittest.main() raises an exception after it's shown the help text
try:
sys.stdout = buff
unittest.main()
finally:
sys.stdout = stdout
lines = buff.getvalue().split("\n")
lines.insert(9, "Iris-specific options:")
lines.insert(
10,
" -d Display matplotlib figures (uses tkagg).",
)
lines.insert(
11,
" NOTE: To compare results of failing tests, ",
)
lines.insert(12, " use idiff.py instead")
lines.insert(
13,
" --data-files-used Save a list of files used to a temporary file",
)
lines.insert(14, " -m Create missing test results")
print("\n".join(lines))
else:
unittest.main()

Now we are using PyTest I often do not import the tests module, and I have not bothered making my files runnable since they are intended to be run via PyTest e.g. pytest test_pp_cf.py.

I think we should remove the runnability from our tests, and if those extra command line options are important then we should explore how they can be offered via PyTest configurability instead. Happy to discuss.

@rcomer
Copy link
Member

rcomer commented Mar 11, 2024

This would be consistent with the decision in cf-units to remove the ifmains: SciTools/cf-units#245

@pp-mo
Copy link
Member

pp-mo commented Mar 11, 2024

It makes sense to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

3 participants