impy-project / chromo

Hadronic Interaction Model interface in PYthon
Other
31 stars 7 forks source link

Uncaptured warnings in unit tests #179

Closed HDembinski closed 1 year ago

HDembinski commented 1 year ago

Several unit tests now emit uncaptured warnings. Tests should not emit those. If a warning is expected in a test, you should use

with pytest.warns(<type of warning>):
    # code which emits the warning goes here
tests/test_hepmc_io.py::test_hepmc_io[UrQMD34]
  /Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/chromo/common.py:443: RuntimeWarning: UrQMD-3.4: only final state particles available in HepMC3 event
    warnings.warn(

tests/test_hepmc_io.py::test_hepmc_io[DpmjetIII191]
tests/test_to_hepmc3.py::test_to_hepmc3[DpmjetIII191]
  /Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/chromo/common.py:443: RuntimeWarning: DPMJET-III-19.1: only final state particles available in HepMC3 event
    warnings.warn(

tests/test_hepmc_io.py::test_hepmc_io[DpmjetIII193]
tests/test_to_hepmc3.py::test_to_hepmc3[DpmjetIII193]
  /Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/chromo/common.py:443: RuntimeWarning: DPMJET-III-19.3: only final state particles available in HepMC3 event
    warnings.warn(

tests/test_hepmc_io.py::test_hepmc_io[DpmjetIII307]
tests/test_to_hepmc3.py::test_to_hepmc3[DpmjetIII307]
  /Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/chromo/common.py:443: RuntimeWarning: DPMJET-III-3.0-7: only final state particles available in HepMC3 event
    warnings.warn(

tests/test_hepmc_io.py::test_hepmc_io[Phojet112]
tests/test_to_hepmc3.py::test_to_hepmc3[Phojet112]
  /Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/chromo/common.py:443: RuntimeWarning: PhoJet-1.12-36: only final state particles available in HepMC3 event
    warnings.warn(

tests/test_hepmc_io.py::test_hepmc_io[Phojet191]
tests/test_to_hepmc3.py::test_to_hepmc3[Phojet191]
  /Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/chromo/common.py:443: RuntimeWarning: PhoJet-19.1: only final state particles available in HepMC3 event
    warnings.warn(

tests/test_hepmc_io.py::test_hepmc_io[Phojet193]
tests/test_to_hepmc3.py::test_to_hepmc3[Phojet193]
  /Users/runner/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/chromo/common.py:443: RuntimeWarning: PhoJet-19.3: only final state particles available in HepMC3 event
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
jncots commented 1 year ago

I tried to use with pytest.warns and find out that it assert that code raises a particular class of warning, i.e. it raise an exception if the warning isn't issued. This is a problem because some models produce warning and other don't.

It seems easier to disable warnings using addopts = "-p no:warnings" in pyproject.toml. @HDembinski, what do you think about it?

HDembinski commented 1 year ago

No, we don't want to suppress warnings. The warnings are useful and they are part of our UX design, it is good to assert that they are raised as we expect them to be raised.

I tried to use with pytest.warns and find out that it assert that code raises a particular class of warning, i.e. it raise an exception if the warning isn't issued. This is a problem because some models produce warning and other don't.

Then you check whether the conditions are fulfilled and only use the with statement in those cases.

jncots commented 1 year ago

Fixed in #127