It is usually not useful or even impossible to test class methods without instantiating the class (including starting __init__
). Typically, your class methods will refer to class attributes (e.g. self.a
). If you do not run __init__
, these attributes will not exist, so your methods will not work. (If your methods do not rely on the attributes of their instance, then why are they methods and not just stand-alone functions?) In your example, it looks like func1
and func2
are part of the initialization process, so they should be tested as part of this.
In theory, you can “quasi-instantiate” a class using __new__
, and then add only the elements you need, for example:
obj = A.__new__(args) obj.a = "test value" obj.func1()
However, this is probably not a good way to test. Firstly, this leads to duplication of code that supposedly already exists in the initialization code, which means that your tests are more likely to not synchronize with the real code. For another, you may need to duplicate many of the initialization calls this way, since you will have to manually repeat what would otherwise be done using any __init__
methods of the base class called from your class.
Regarding the design of the tests, you can look at the unittest module and / or the nose module . This gives you the basics for setting up tests. What you actually enter in the tests obviously depends on what your code should do.
Edit: The answer to your question 1 is "definitely yes, but not necessarily everyone." The answer to your question 2 is "maybe not." Even with the first link you give, there is a discussion about whether methods that are not part of the public API class should be tested at all. If your func1 and func2 are purely internal methods that are only part of the initialization, then there is probably no need to test them separately from the initialization.
This comes to your last question about the advisability of calling func1 and func2 from __init__
. As I have repeatedly stated in my comments, it depends on what these functions do. If func1 and func2 do part of the initialization (that is, they do some “tuning” of the work for the instance), then it makes sense to call them from __init__
; but in this case they should be tested as part of the initialization process, and there is no need to test them yourself. If func1 and func2 are not part of the initialization, then yes, you must test them yourself; but in this case, why are they in __init__
?
The methods that are an integral part of instantiating your class must be tested as part of testing your class instance. Methods that are not an integral part of instantiating your class should not be called from __init__
.
If func1 and func2 are “just I / O logic” and do not require access to the instance, then they should not be class methods at all; they can simply be standalone functions. If you want to store them in a class, you can mark them as staticmethods and then call them directly in the class without creating it. Here is an example:
>>> class Foo(object): ... def __init__(self, num): ... self.numSquared = self.square(num) ... ... @staticmethod ... def square(num): ... return num**2 >>> Foo.square(2)
You can just imagine that you might have a class of monsters that requires an extremely complex initialization process. In this case, you may need to check individual parts of this process individually. However, such a giant init sequence would itself be a warning about cumbersome construction.