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
  Basic Inheritance, Why Use init udinjelek 5 542 Sep-29-2021, 06:03 PM
Last Post: deanhystad
Thumbs Up [SOLVED] Simplify condition test in if block? Winfried 2 389 Aug-25-2021, 09:42 PM
Last Post: Winfried
  write new function or change the old one to work "smartter? korenron 3 448 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 897 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Find factor to match test curve to golden curve SriRajesh 0 460 Jun-17-2021, 04:39 AM
Last Post: SriRajesh
  How do I split a dataset into test/train/validation according to a particular group? 69195Student 1 703 May-12-2021, 08:27 PM
Last Post: bowlofred
  How to make a test data file for the full length of definition? MDRI 6 1,261 Apr-16-2021, 01:47 AM
Last Post: MDRI
  How can I write a function with three parameters? MehmetAliKarabulut 1 861 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  Using Dictionary to Test Evenness of Distribution Generated by Randint Function new_coder_231013 6 1,115 Feb-23-2021, 01:29 PM
Last Post: new_coder_231013
  How to run a pytest test for each item in a list arielma 0 837 Jan-06-2021, 10:40 PM
Last Post: arielma

Forum Jump:

User Panel Messages

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