Python Forum
How to use unittest module ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use unittest module ?
#1
I have a YAML config file and a Python ConfigManager file used to check connection to database. I need to write some test cases for my ConfigManager file :

1. If the database is none then test case fails.
2. If the database is Sqlite or Postgre, test case pass, otherwise it fails
I have created a test class but I don't know how to implement it. Can anyone suggest to me ?

DatabaseConfig.yml:
database:  #if it is empty or not sqlite or not postgre then it fails

dbopt:
   host: 
   port: 5432
   dbname: db1
   
ConfigManager.py:
import yaml

class InvalidConfigError(Exception):
    pass

class DB():
    def __init__(self, dbconf):
        self._dbconf = dict(dbconf)
        
        # checking for database type
        dbtype = self.get_db_type()
        if dbtype != 'sqlite' and dbtype != 'postgres':
            raise InvalidConfigError(
                'E01001', 'Invalid database type, should be sqlite or postgres.')
        else:
            self.dbtype = dbtype

    def get_db_type(self):
        return self._dbconf['database']

with open('DatabaseConfig.yml') as file:
    data = yaml.full_load(file)

    for item, doc in data.items():
        print(item, ":", doc)

    db = DB(data)
My test file test_database.py (help me with this) :
import unittest
from database_config import DB

class TestDatabase(unittest.TestCase):

    def test_missing_db(self):
             # test database is none then it fail
    def test_db_type_postgres(self):
             # test if database is postgres then it pass
    def test_db_type_sqlite(self):
             # test if database is sqlite then it pass
    def test_db_type_invalid(self):
             # test if database is not postgres or sqlite then it fail
if __name__ == '__main__':
    unittest.main()
Reply
#2
suggest you read: https://docs.python.org/3/library/unittest.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using unittest akbarza 2 258 Feb-25-2024, 12:51 PM
Last Post: deanhystad
Question Unwanted execution of unittest ThomasFab 9 1,944 Nov-15-2022, 05:33 PM
Last Post: snippsat
  unittest.mock for an api key silver 3 1,335 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  Ran 0 tests in 0.000s - unittest Peaches 8 4,924 Dec-31-2021, 08:58 AM
Last Post: Peaches
Sad Problem with Unittest mhanusek 1 3,679 Nov-12-2020, 04:58 PM
Last Post: Gribouillis
  Unittest et patch mad31 2 2,071 Aug-09-2020, 06:16 AM
Last Post: mad31
  Unusual things to do with unittest/HTMLTestRunner AndyHolyer 0 2,102 Jul-29-2020, 02:43 PM
Last Post: AndyHolyer
  Test a class function via "unittest " Penguin827 1 1,576 Jul-10-2020, 08:31 AM
Last Post: Gribouillis
  unittest mock and patch piscvau 1 2,070 Nov-08-2019, 03:23 PM
Last Post: ichabod801
  Optimize unittest loading Nazz 3 2,484 Mar-05-2019, 11:59 AM
Last Post: Nazz

Forum Jump:

User Panel Messages

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