[Tutor] How to test a class in python?

Danny Yoo dyoo at hashcollision.org
Mon Mar 2 05:41:48 CET 2015


On Sun, Mar 1, 2015 at 11:41 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 01/03/15 16:19, Fatimah Taghdi wrote:
>>
>> Hello I was wondering how to test a class in python is it the same way as
>> testing a function ?


Depending on the design of the class, there might be a little bit more
set-up involved.  But the ideas are similar.


In a test case for a function, we have a few things in hand:

   1.  The function being tested.
   2.  The parameters we're going to pass as inputs to that function.
   3.  The expected output we want to see if the function is behaving properly.


In a test case for a class, we have a few more things in hand.

   1.  The class being tested.
   2.  The parameters we use to create the class instance.
   3.  The particular method of the class that we're testing.
   4.  The parameters we're going to pass as inputs to that method.
   5.  The expected output we want to see if the method is behaving properly.

So you can see that the core concept for testing is the same: we
express the things that are going to change, and we also say what the
expected result is supposed to be.  It just so happens that classes
can have a lot more state, and this can contribute to a few extra
steps to do the test set up.


Do you have a particular class in mind?


More information about the Tutor mailing list