0

I have a class which has a side effect at import time: it populates a field with a value from a deployed infrastructure. It basically boils down to this:

class MyModel:
    class Meta:
        table_name = get_param("table_name")

I want to use instances of MyModel in offline tests, so I need to make sure that boto3 doesn't actually run the production get_param code. I've done it by mocking out get_param in these tests:

with patch("….get_param"):
    from … import MyModel

    def test_…

Clunky, but it sort of works. The problem is that if I run pytest with this test file first and then an acceptance test file (which does need to use the real get_param), the acceptance test ends up with a mocked version of get_param (reversing the file sequence makes both tests pass). The acceptance test runs fine on its own, but I'd rather not pull apart the test suite based on such a weird premise. Is there some way to avoid this namespace pollution?

l0b0
  • 55,365
  • 30
  • 138
  • 223

1 Answers1

0

Looks like I don't understand how Python imports work.

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • you should also be mocking `prod.randint` instead of `random.randint` in order to not have to make the import happen inside that block – gold_cy Mar 24 '21 at 02:41