Search code examples
pythonpytestmonkeypatching

How test a function that doesn't have a return with monkeypatch


I am running some unit tests in my project, and I have one function that doesn't return anything. I started with the tests using monkeypatch, pytests, and Python 3.8. Bellow, it is available a part of this code that I am trying to perform the test.

def test_start_download(monkeypatch):
    def mock_get_file(self, x, y, z):
        assert z == function.get('foo')[0]


    monkeypatch.setattr(Bar, 'foo', lambda x: 'valid_token')
    monkeypatch.setattr(Foo, 'bar', lambda x, y, z: foo_bar)
    monkeypatch.setattr(FooBar, 'foo', mock_get_file)
    monkeypatch.setattr(BarFoo, 'foo', lambda x, y: 'file')

    function.call(1861, 'key', 'C:/User')

This function call doesn't return any result to me, it is just a function that download a file.


Solution

  • it is just a function that download a file.

    Then test that the file appears where it should:

    function.call(1861, 'key', 'C:/User')
    assert os.path.exists('C:/User/key')  # or whatnot
    

    You may wish to use the tmp_path fixture to avoid polluting the filesystem:

    def test_start_download(monkeypatch, tmp_path):
       # ...
       function.call(1861, 'key', str(tmp_path))
       assert (temp_path / 'key').exists()