如何通过单元测试为数据库配置文件编写测试用例?

时间:2020-02-18 07:54:01

标签: python-3.x python-unittest pyyaml

我有一个包含数据库配置信息的yaml文件和一个用于检查通过该配置文件连接到数据库的python文件。然后,我在python文件中编写了一些测试用例。 有一个功能

def test_db_type_postgres(self):
    database = DB({'database': 'postgres'});
    self.assertEqual(database.get_db_type(), 'postgres')

我希望上面的功能是,如果数据库是Postgres,它将通过,但是失败。我检查了测试日志。似乎受方法

影响
#checking db option
        dbopt = self.__get_dbopt()
        if dbopt is None:
            raise InvalidConfigError(
                'E01002', 'Invalid database options.')
        else:
            self.dbopt = dbopt

我在哪里错了?请给我看。任何帮助表示赞赏。 enter image description here

我的python文件(包括测试用例):

import yaml
import unittest

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()
        else:
            self.dbtype = dbtype

        #checking db option
        dbopt = self.__get_dbopt()
        if dbopt is None:
            raise InvalidConfigError(
                'E01002', 'Invalid database options.')
        else:
            self.dbopt = dbopt



    def get_db_type(self):
        return self._dbconf.get('database')

    def __get_dbopt(self):
        return self._dbconf.get('dbopt')

class TestDBtype(unittest.TestCase):
    #setUpClass gets calls only once where as setUp gets called before every test
    @classmethod
    def setUpClass(cls):
        cls.init_db()

    @classmethod
    def init_db(cls):

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

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

            cls.database = DB(data)

    def test_missing_db(self):
        database = DB({'database': None});
        self.assertRaises(InvalidConfigError)

    def test_db_type_postgres(self):
        database = DB({'database': 'postgres'});
        self.assertEqual(database.get_db_type(), 'postgres')

    def test_db_type_invalid(self):
        database = DB({'database': 'mysql'});
        self.assertRaises(InvalidConfigError)

class TestOpt(unittest.TestCase):
    #setUpClass gets calls only once where as setUp gets called before every test
    @classmethod
    def setUpClass(cls):
        cls.init_db()

    @classmethod
    def init_db(cls):

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

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

            cls.database = DB(data)

    def test_db_opt(self):
        database = DB({'dbopt': None});
        self.assertRaises(InvalidConfigError)

if __name__ == '__main__':
    unittest.main()

测试日志输出:

database : postgres
dbopt : {'host': 'localhost', 'port': 6311, 'dbname': 'spam_eggs', 'user': 'hamburger', 'password': 'example_password', 'client_encoding': 'utf-8', 'connect_timeout': 60, 'sslmode': 'none'}
query : select * from manufacturing_product
test_db_type_invalid (test_db_type.TestDBtype) ... ERROR
NoneType: None
test_db_type_postgres (test_db_type.TestDBtype) ... ERROR
NoneType: None
test_missing_db (test_db_type.TestDBtype) ... ERROR
NoneType: None

======================================================================
ERROR: test_db_type_invalid (test_db_type.TestDBtype)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\Python\TestDataSource\test_db_type.py", line 60, in test_db_type_invalid
    database = DB({'database': 'mysql'});
  File "d:\Python\TestDataSource\test_db_type.py", line 14, in __init__
    raise InvalidConfigError()
test_db_type.InvalidConfigError

======================================================================
ERROR: test_db_type_postgres (test_db_type.TestDBtype)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\Python\TestDataSource\test_db_type.py", line 56, in test_db_type_postgres
    database = DB({'database': 'postgres'});
  File "d:\Python\TestDataSource\test_db_type.py", line 22, in __init__
    'E01002', 'Invalid database options.')
test_db_type.InvalidConfigError: ('E01002', 'Invalid database options.')

======================================================================
ERROR: test_missing_db (test_db_type.TestDBtype)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\Python\TestDataSource\test_db_type.py", line 52, in test_missing_db
    database = DB({'database': None});
  File "d:\Python\TestDataSource\test_db_type.py", line 14, in __init__
    raise InvalidConfigError()
test_db_type.InvalidConfigError

----------------------------------------------------------------------
Ran 3 tests in 0.003s

FAILED (errors=3)

0 个答案:

没有答案