snu-quiqcl / qiwis

QuIqcl Widget Integration Software
MIT License
5 stars 2 forks source link

`unittest.mock.patch` uses `importlib.import_module()` #164

Open kangz12345 opened 1 year ago

kangz12345 commented 1 year ago

Describe the bug

unittest.mock.patch does not work properly (the objects are not patched at all!) in SwiftTest.

Expected behavior

For example, if we do unittest.mock.patch("swift.dumps"), then calling swift.dumps() should be mocked.

To Reproduce

Steps to reproduce the behavior:

  1. Write a test in SwiftTest
  2. Use unittest.mock.patch
  3. See error

Expected causes and correction methods

I found that patching uses importlib.import_module(), by inspecting mock_calls of the mocked import_modules(). Therefore, we should be careful when patching importlib.import_module(). We should not replace importlib.import_module() permanently. If we have to, we may patch it temporarily.

Additional context

This issue is found while developing #163.