sixty-north / cosmic-ray

Mutation testing for Python
MIT License
565 stars 57 forks source link

Problem with exception replacement #423

Closed abingham closed 5 years ago

abingham commented 5 years ago

We need to rethink our exception-replacement operator. We used to be able to rely on being able to inject a new exception type into the testing environment. Now we can't. See the following cr-html results as an example.

63 : job ID 9528a51d8b4b4a269e8a0a19def2a9ad

worker outcome: normal

test outcome: killed

adam/adam_2.py, start pos: (42, 11), end pos: (42, 18)

operator: core/ExceptionReplacer, occurrence: 0

--- mutation diff ---
--- aadam/adam_2.py
+++ badam/adam_2.py
@@ -39,7 +39,7 @@
     result = None
     try:
         raise IOError
-    except IOError:
+    except CosmicRayTestingException:
         result = True

     return result

.............E.......
======================================================================
ERROR: test_handle_exception (test_adam.Tests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/private/var/folders/28/tck2nhpj59zc5x9m_cbp3zkc0000gp/T/tmpjwzvn0yv/repo/adam/adam_2.py", line 41, in handle_exception
    raise IOError
OSError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/private/var/folders/28/tck2nhpj59zc5x9m_cbp3zkc0000gp/T/tmpjwzvn0yv/repo/tests/test_adam.py", line 68, in test_handle_exception
    self.assertTrue(adam.adam_2.handle_exception())
  File "/private/var/folders/28/tck2nhpj59zc5x9m_cbp3zkc0000gp/T/tmpjwzvn0yv/repo/adam/adam_2.py", line 42, in handle_exception
    except CosmicRayTestingException:
NameError: name 'CosmicRayTestingException' is not defined

----------------------------------------------------------------------
Ran 21 tests in 0.436s

FAILED (errors=1)
abingham commented 5 years ago

The point being that CosmicRayTestingException is not something we can rely on now. Interesting...

abingham commented 5 years ago

I've disabled this operator for now until we can find a way to do it correctly.

abingham commented 5 years ago

One approach would be to use sitecustomize.py. It could modify __builtins__ to add CosmicRayTestingException.

Would this potentially conflict with the user's sitecustomize? Not with the current cloning technologies; we don't do anything that could put a sitecustomize in place. So this might work cleanly.

abingham commented 5 years ago

Fixed in 7bee55663608fa893a406bfa648e22521287fa49.

We use sitecustomize.py for the actually test execution. We use a standard module for working with our custom exception type everywhere else.