[Tutor] How to test a class in python?

Mark Lawrence breamoreboy at yahoo.co.uk
Tue Mar 3 17:05:57 CET 2015


On 03/03/2015 06:25, Marcos Almeida Azevedo wrote:
> On Mon, Mar 2, 2015 at 12:41 PM, Danny Yoo <dyoo at hashcollision.org> wrote:
>
>> 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..
>>
>
> I suggest PyUnit for easier testing.
> Here is a very good guide: http://pyunit.sourceforge.net/pyunit.html
>

Quoting from the link "PyUnit forms a part of the Python Standard 
Library as of Python version 2.1." so for the latest version see 
https://docs.python.org/3/library/unittest.html

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list