1

I have the following class A to be tested.

aaa.py

def get_c():
    print('call external modules to get c from some expensive I/O')
    return 'something'

@dataclass
class A:
    c: ClassVar[C] = get_c()  # get_c() shouldn't be run in the test code

In a test code, I assign A.c to some constants. However, get_c() was still called.

from aaa import A

@pytest.fixture
def sut()
    A.c = {'a':0}
    yield A()

def test_1(sut):
    ''''''

How to prevent get_c() from being called?

ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • 7
    Can't reproduce, assertion works for me. Are you sure that's the full code? – Bharel Jan 30 '22 at 04:54
  • 4
    Same, I can't reproduce this. – Kraigolas Jan 30 '22 at 04:55
  • This seems a very strange test, you're not actually *mocking* anything, just setting a new field on a `Mock` object and then transferring it to another. I suspect it would be better if you posted your *actual* code. – paxdiablo Jan 30 '22 at 05:12

0 Answers0