Python Forum
Would you unit test __init__ method?
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Would you unit test __init__ method?
#2
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 ===========================
Reply


Messages In This Thread
Would you unit test __init__ method? - by kilthar - Oct-18-2017, 12:04 PM
RE: Would you unit test __init__ method? - by snippsat - Oct-18-2017, 05:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unit Testing Set Up and Use RockBlok 2 1,096 Jan-08-2024, 07:43 PM
Last Post: deanhystad
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 4,288 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Writing unit test results into a text file ateestructural 3 6,676 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 4,207 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 3,172 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  Problem with adding arbitrary parrameters to __init__ method sebastianvdn 1 2,505 Feb-03-2020, 09:30 PM
Last Post: micseydel
  Remove function and unit test ftg 5 5,016 Jan-07-2020, 03:10 PM
Last Post: ndc85430
  Odd Unit Test Behavior ichabod801 3 3,348 Jan-02-2020, 03:34 PM
Last Post: ichabod801
  Define unit of measure of a number doug2019 3 3,084 Oct-15-2019, 03:43 PM
Last Post: jefsummers
  Unit testing - AssertRaises kerzol81 3 8,136 Oct-07-2019, 10:35 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020