In my pytest file test_foo.py
, I am trying to change the value of foo.path
to mock_path
using monkeypatch
. However, when I try the following, I get an error
ERROR test_foo.py::test_foo - AttributeError: 'foo' has no attribute 'path'
What should be the correct way to make this change so that foo
object in test_foo
will use mock_path
and pass that test?
test_foo.py:
import os
import pytest
class Foo:
def __init__(self):
self.path = os.path.join(os.getcwd(), "test.txt")
@pytest.fixture
def foo(monkeypatch):
monkeypatch.setattr(Foo, 'path', 'mock_path')
return Foo()
def test_foo(foo):
assert foo.path == "mock_path"