0

I am having problems trying to mock/patch a default parameter for a method, that is being called inside a method that is being unit tested with Pytest. In general the code looks like so:

class Repository:
  DEFAULT_VERSION = "0.1.10"
  ...

  @classmethod
  def _get_metadata(cls, id: str, version: str = DEFAULT_VERSION) -> Dict[str, str]:
    return ...

  def write(self, df: DataFrame, id: str) -> None:
    ...
    metadata = self._get_metadata(id)

class TestRepository:
  def test_write(self, ...):
    assert df.write(df=test_df, id="1").count() > 1

TEST_DEFAULT_VERSION = "0.2.20"

Now, I would like to mock the value of DEFAULT_VERSION parameter to be the value of TEST_DEFAULT_VERSION - how can I do that in Pytest?

Bartosz Gajda
  • 984
  • 6
  • 14

1 Answers1

0

You can do this by either modifying the class method to allow Monkey Patching of the constant, or Monkey Patching the class method directly. Have a look at this solution (basically the same question): pytest - monkeypatch keyword argument default

BraisLP
  • 76
  • 3