1

I need to write a test for a function that writes into a file as follows:

with open(name=ccf.status_dir_path + '/' + script_normalized, mode='w') as outfile:
    outfile.write(str(datetime.datetime.now()))

Of course I would like to avoid the file to be created, so I need to monkeypatch "open", but since "open" doesn't belong to any class or module I do not know what to give as first parameter to

monkeypatch.setattr(??, "open", patched_open)
mstuebner
  • 386
  • 1
  • 14
  • 1
    `unittest.mock` has something specifically for this... – jonrsharpe Sep 09 '16 at 06:25
  • But we moved away from unittest. Anything similar for pytest? – mstuebner Sep 09 '16 at 06:26
  • You can just use the mock part, it's separate to `TestCase` etc. and works perfectly well with pytest. – jonrsharpe Sep 09 '16 at 06:28
  • 1
    @jonrshape: Would be brilliant to point out to which question my question should be a duplicate! I didn't find any duplicate during a 30 minute search. So it is difficult to explain the difference to something unknown. – mstuebner Sep 09 '16 at 06:59
  • ...it's literally listed at the top of the question. Also I've told you where to find it, so you could just look in the docs. – jonrsharpe Sep 09 '16 at 07:00
  • @jonrshape: 1. Thanx, I have no idea how you found that since there is only the word "open" in common, +1 2. I just saw your comment and didn't look at the top of the page, because I didn't expect another related information at a different place. Sry. – mstuebner Sep 09 '16 at 07:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122993/discussion-between-mstuebner-and-jonrsharpe). – mstuebner Sep 09 '16 at 12:24

0 Answers0