2bndy5 / CircuitPython-mocks

A library of mock data structures for soft testing CircuitPython-based projects
https://circuitpython-mocks.rtfd.io/
MIT License
1 stars 0 forks source link

reconsider how to assert that expectations are used #8

Open 2bndy5 opened 1 month ago

2bndy5 commented 1 month ago

Currently, the expectations are checked that they are all used upon destruction of the mocked object. However, pytest will ignore any exception thrown by a destructor. This leaves a traceback in the pytest output about "Ignored exception" when some exceptions were not used.

Possible solutions

  1. let unused expectations go unchecked

    pros cons
    The done() method can be called from the test code This may cause a deviation from expected behavior
  2. assert that done() is called upon destruction

    pros cons
    This serves as reminder to update the test code This seems to have poor behavior with the monkey_patch_sys_paths fixture because it is set to autouse=True (see also #9 )

Additional context

Because pytest manages setup and tear-down as an internal implementation detail, this is destructor approach is rather undesirable.

2bndy5 commented 1 month ago

I'm inclined to go with option 1 because tests could expectedly fail (pytest.marks.xfail). Relying on a mock's destructor would force users' test code to overcompensate for a mock's behavior that asserts all expectations are used. It would be better to let a mock's done() be optionally invoked when appropriate.

As this library is largely influenced by embedded-hal-mock (for rust), going with option 1 is a serious deviation from that lib's design.