Python Forum
How to write test cases for a init function by Unit test in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write test cases for a init function by Unit test in python?
#1
I'm new in Python. I have a __init__ function with information about data sink config and I have to write some test cases fot it. But init is a function which doesn't return any result so it confuses me. Can you show me some ways to implement test case for it ? Thank you.

My function :
class DataSinkConfig():
    DEFAULT_ENTRIES_TABLE = 'entries'
    DEFAULT_OUTLIERS_TABLE = 'outliers'
    DEFAULT_OUTLIERCAUSES_TABLE = 'outlier_causes'

    # Note
    def __init__(self, dbconf):

        insopt = dbconf.get('insopt')
        if insopt is None:
            self.entries_table_name = self.DEFAULT_ENTRIES_TABLE
            self.outliers_table_name = self.DEFAULT_OUTLIERS_TABLE
            self.outlier_causes_table_name = self.DEFAULT_OUTLIERCAUSES_TABLE
        else:
            try:
                dict(insopt)
            except Exception as e:
                raise Exception(
                    "Invalid value {} for parameter 'insopt'.".format(insopt))

            self.entries_table_name = self.__complement_item(insopt, 'entry_tbl', self.DEFAULT_ENTRIES_TABLE)
            self.outliers_table_name = self.__complement_item(insopt, 'outlier_tbl', self.DEFAULT_OUTLIERS_TABLE)
            self.outlier_causes_table_name = self.__complement_item(insopt, 'cause_tbl', self.DEFAULT_OUTLIERCAUSES_TABLE)
My test is stuck :
import unittest
import DataSinkConfig, DataSourceConfig

class TestDataSink(unittest.TestCase):
    def setUp(self):
      #pass

if __name__ == "__main__":
    unittest.main()
Reply
#2
In a nutshell create an instance of the class and check that whatever should have been performed were in fact carried out. Check if properties were correctly set, do method return the correct values and so on.
There is no passion to be found playing small - in settling for a life that is less than the one you are capable of living.
Reply
#3
you have:
class DataSinkConfig():
    DEFAULT_ENTRIES_TABLE = 'entries'
    DEFAULT_OUTLIERS_TABLE = 'outliers'
    DEFAULT_OUTLIERCAUSES_TABLE = 'outlier_causes'
 
    # Note
    def __init__(self, dbconf):
 
        insopt = dbconf.get('insopt')
        if insopt is None:
            self.entries_table_name = self.DEFAULT_ENTRIES_TABLE
            self.outliers_table_name = self.DEFAULT_OUTLIERS_TABLE
            self.outlier_causes_table_name = self.DEFAULT_OUTLIERCAUSES_TABLE
why not just?:
class DataSinkConfig():
    def __init__(self, dbconf):
 
        insopt = dbconf.get('insopt')
        if insopt is None:
            self.entries_table_name = 'entries'
            self.outliers_table_name = 'outliers'
            self.outlier_causes_table_name = 'outlier_causes'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 118 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  ImportError: /home/ali/Documents/test/charm/core/crypto/AES.cpython-39-x86_64-linux-g Anldra12 0 237 Apr-28-2022, 07:13 AM
Last Post: Anldra12
  How do I get rid of "Chrome is being controlled by automated test software"? jetBlack 1 2,162 Feb-23-2022, 07:36 AM
Last Post: snippsat
  Init an indefinite number of class MathisDELAGE 9 725 Feb-18-2022, 07:49 PM
Last Post: deanhystad
  can someone test the code for me kucingkembar 4 583 Feb-17-2022, 10:22 AM
Last Post: kucingkembar
  how do I return Max Test result + corresponding student name from an excel dataset? sean1 3 449 Jan-16-2022, 09:07 PM
Last Post: snippsat
  Basic Inheritance, Why Use init udinjelek 5 1,140 Sep-29-2021, 06:03 PM
Last Post: deanhystad
Thumbs Up [SOLVED] Simplify condition test in if block? Winfried 2 814 Aug-25-2021, 09:42 PM
Last Post: Winfried
  write new function or change the old one to work "smartter? korenron 3 1,004 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 1,544 Jul-03-2021, 10:07 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

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