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
  Class test : good way to split methods into several files paul18fr 4 403 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Unit Testing Set Up and Use RockBlok 2 380 Jan-08-2024, 07:43 PM
Last Post: deanhystad
  How to automate loop test check on Network device jpc230 1 545 Oct-09-2023, 09:54 PM
Last Post: Larz60+
  How to test the high-score in my Python-game Pluviometer 2 547 Oct-02-2023, 06:55 PM
Last Post: deanhystad
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 918 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Better way to write this function SephMon 1 789 Feb-08-2023, 10:05 PM
Last Post: Gribouillis
  Presenting random test questions oradba4u 2 855 Sep-27-2022, 04:23 PM
Last Post: deanhystad
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 990 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 4,704 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
  Help with bleak - how to send test to BLE device? korenron 1 1,650 Aug-28-2022, 11:28 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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