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
  Presenting random test questions oradba4u 2 313 Sep-27-2022, 04:23 PM
Last Post: deanhystad
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 386 Sep-19-2022, 02:32 AM
Last Post: Xeno
  How to sort .csv file test log which item first fail and paint color SamLiu 24 2,187 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
  Help with bleak - how to send test to BLE device? korenron 1 556 Aug-28-2022, 11:28 PM
Last Post: Larz60+
  Statsmodels, Dickey-Fuller test for stationarity Led_Zeppelin 1 345 Aug-17-2022, 06:36 PM
Last Post: Larz60+
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 585 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 548 Apr-28-2022, 07:13 AM
Last Post: Anldra12
  Init an indefinite number of class MathisDELAGE 9 1,168 Feb-18-2022, 07:49 PM
Last Post: deanhystad
  can someone test the code for me kucingkembar 4 865 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 642 Jan-16-2022, 09:07 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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