Class mock without methods

Asked

Viewed 33 times

0

I need to mock a class that has no methods and returns nothing, how could I do that in Python? I’m mocking this class because it acts as a typing converter.

My class is very similar to the one below:

class ClasseExemplo(OutraClasseExemplo):
    item_1: str
    item_2: datetime
    item_3: int
    item_4: datetime
    item_5: str

And I’ve tried some alternatives that didn’t work:

@patch.object(ClasseExemplo)
@patch('caminho.caminho.arquivo_onde_a_classe_se_encontra')

1 answer

1

Without you giving more details, all that is possible to advance is that you probably don’t need mock such a class.

Simply let the code that will use this class use the original class.

Why there are 4 things your code and testing may require from Mock:

(1) want the class to exist, so that the code works: the code will work if it is the original class and not a mock

(2) want to write attributes and call methods in the class, and in the test, you want to check if attributes are written correctly: since there are no methods, you do not need the mock functionality to verify which calls were made and with which parameters. Already written attributes will work as well with the class as it is, as with a mock, so mock is not necessary

(3) Target code will read attributes and annotations in the class and use this internally: again, not only will this work perfectly with the original class, but it would take some work to recreate the class and annotations using Python mocks. Not only is mock not necessary but it would be very laborious.

(4) the code to be tested will call methods in the code mocado that will cause side effects of input and output (file recording/sending or receiving information on the network -; creation of objects in the database) - and the use of Mock makes it possible to exercise that code without triggering such side effects. Again: if the class has no code, it also has no dangerous side effects.


I can imagine that you want to pass a class with annotations different from the original class to the code to be tested - in this case, yes to the call unittest.mock.patch, but with one argument new specific, not for a mock.

In this case, you first declare the substitute class (it can be within the code of the test function, or in the test module itself - whatever), and use, as a string, the full path of the class you want to fuck, including the packets, subpackages, module (file) and the name of the class, be stopped by point:


class ExemploSubstituto:
    item_1: str


with unitest.mock.patch(
    "meuprojeto.arquivo_de_classe.ClasseExemplo",
     new=ExemploSubstituto):
   # código que chama as classes que usam o "ClasseExemplo"

Another thing, if the file that uses the "Classeexemplo" matters the same doing so:

archiv_que_usa.py

from arquivo_de_classe import ClasseExemplo
...

Another thing: no use changing the Classeemplo in "arquivo_de_class" - the Classeemplo in "arquivo_que_usa" will continue pointing to the previous object.

In such cases, the most recommended is that the code be this way:

import arquivo_de_classe 

...
def alguma_funcao():
    ...
    instancia = arquivo_de_classe.ClasseExemplo()
    ...

But you can also patch right into "archiv_que_usa" - too will work - but if the code there, call code in a third file who also access the class, each will see a different class - so it is not recommended. In that specific case, the best is to see if you really will need to mock these classes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.