Python Forum

Full Version: Would you unit test __init__ method?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am learning unit testing.
The principle I am trying to adopt is to unit test only incoming queries for returned value and incoming commands for change of value plus outgoing command checking if it was sent with correct parameters (as per Sandi Metz lecture).

This cuts off unnecessary testing of private methods and redundant tests of the same code.

I am at a loss though how to classify an __init__ method that sets up publicly available attributes of a class...

Example:

MyClass(object):

def __init__(self, public_attr_1, public_attr_2)

self.public_attr_1 = public_attr_1
self.public_attr_2 = public_attr_2

Test:

instance = MyClass(1, 2)
assert instance.public_attr_1 = 1
assert instance.public_attr_2 = 2

My hunch is that it should be unit tested to make sure the class is initialized with correct attributes (the public ones).

Would you unit test it?

(Sorry for no formatting for code - it disappeared)
Python doesn't have public or private attributes,so to talk about that is wrong
All attributes are accessible to all code.

All can be tested if it make sense to do so.
Example:
foo.py
class MyClass:
    def __init__(self, public_attr_1, public_attr_2):
        self.public_attr_1 = public_attr_1
        self.public_attr_2 = public_attr_2
test_foo.py
import pytest
from foo import MyClass

def test_initial_value():
    obj_1 = MyClass(1, 2)     
    assert obj_1.public_attr_1 == 4
    assert obj_1.public_attr_2 == 2

def test_no_value():
    with pytest.raises(Exception) as e_info:
        obj = MyClass(1, 2)
Test:
Error:
C:\1\py_test λ pytest ============================= test session starts ============================= platform win32 -- Python 3.6.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0 rootdir: C:\1\py_test, inifile: collected 2 items test_foo.py FF ================================== FAILURES =================================== _____________________________ test_initial_value ______________________________     def test_initial_value():         obj_1 = MyClass(1, 2) >       assert obj_1.public_attr_1 == 4 E       assert 1 == 4 E        +  where 1 = <foo.MyClass object at 0x03FE4950>.public_attr_1 test_foo.py:6: AssertionError ________________________________ test_no_value ________________________________     def test_no_value():         with pytest.raises(Exception) as e_info: >           obj = MyClass(1, 2) E           Failed: DID NOT RAISE <class 'Exception'> test_foo.py:11: Failed ========================== 2 failed in 0.17 seconds ===========================
Fix:
import pytest
from foo import MyClass

def test_initial_value():
    obj_1 = MyClass(1, 2)     
    assert obj_1.public_attr_1 == 1
    assert obj_1.public_attr_2 == 2

def test_no_value():
    with pytest.raises(Exception) as e_info:
        obj = MyClass()   
Test:
Output:
C:\1\py_test λ pytest ============================= test session starts ============================= platform win32 -- Python 3.6.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0 rootdir: C:\1\py_test, inifile: collected 2 items test_foo.py .. ========================== 2 passed in 0.06 seconds ===========================