Swapnil Sharma
Swapnil Sharma

Reputation: 361

How to mock os.walk in python with a temporary filesystem using monkeypatching(PyTest)?

I have a function that uses os.walk and I want to test it using monkeypatching (PyTest). I don't know how to use monkeypatch.setattr.

Upvotes: 1

Views: 598

Answers (1)

Cédric Julien
Cédric Julien

Reputation: 80811

To achieve correct mock of the os.walk, you have to use

monkeypatch.setattr(os, "walk", function_that_will_simulate_os_walk_iterator)

The function_that_will_simulate_os_walk_iterator should return a list of tuples (root, dirs, files) for each simulated file.

Upvotes: 2

Related Questions