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?
#1
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)
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unit Testing Set Up and Use RockBlok 2 519 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 3,288 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Writing unit test results into a text file ateestructural 3 4,914 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 3,203 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,620 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  Problem with adding arbitrary parrameters to __init__ method sebastianvdn 1 2,037 Feb-03-2020, 09:30 PM
Last Post: micseydel
  Remove function and unit test ftg 5 3,688 Jan-07-2020, 03:10 PM
Last Post: ndc85430
  Odd Unit Test Behavior ichabod801 3 2,685 Jan-02-2020, 03:34 PM
Last Post: ichabod801
  Define unit of measure of a number doug2019 3 2,470 Oct-15-2019, 03:43 PM
Last Post: jefsummers
  Unit testing - AssertRaises kerzol81 3 4,713 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