Home>

Test code is written using Python unittest (https://docs.python.jp/3/library/unittest.mock-examples.html).
I'm not sure how to use Mock and path.
I think that path is something that makes it possible to use the method called by the method I want to test, but I think it's probably not so wrong, but I don't understand Mock's functionality ...

  • Answer # 1

    Mock and patch.

    mock is English for imitation.
    A mock imitates an object of a certain class.
    Since each class is supposed to behave when it is designed, it is there to prepare exactly what it will do when viewed from the outside.
    When testing, it has a role to make sure that the actual class need not be implemented.

    If you use mock for the

    method, you can use the mock log function. You can control how many times it has been called and how many times it cannot be called.

    patch is a destination cloth.
    With patch, you can change the behavior of some of the implemented classes.
    For example, if there is a method that calls the Web API, and it hits a large amount, it will be difficult to test. In that case, you can get the return value of communication once locally and use the local one for testing.

    If you don't know it, you might be able to better understand it by studying stubs and drivers.