Python 错误:";字典更新序列元素#0的长度为1;2是必需的“;单元测试

Python 错误:";字典更新序列元素#0的长度为1;2是必需的“;单元测试,python,python-unittest,pyyaml,Python,Python Unittest,Pyyaml,我有两个文件:database_config.py,用于通过DatabaseConfig.yml配置文件检查与数据库的连接。我需要为我的配置文件编写一些测试用例,包括几个场景: 如果数据库为空,则测试用例失败 如果数据库不是Sqlite或Postgre,测试用例将失败 我已经创建了一个测试文件,但当我尝试实现它时,我发现了一个错误 ValueError:字典更新序列元素#0的长度为1;2是必需的 谁能帮我一下吗 My DatabaseConfig.yml: database: dbopt:

我有两个文件:database_config.py,用于通过DatabaseConfig.yml配置文件检查与数据库的连接。我需要为我的配置文件编写一些测试用例,包括几个场景:

  • 如果数据库为空,则测试用例失败
  • 如果数据库不是Sqlite或Postgre,测试用例将失败
  • 我已经创建了一个测试文件,但当我尝试实现它时,我发现了一个错误

    ValueError:字典更新序列元素#0的长度为1;2是必需的

    谁能帮我一下吗

    My DatabaseConfig.yml:

    database: 
    
    dbopt:
       host: None
       port: 6313
       dbname: spam_eggs2
    
    query:
       select * from manufacturing_product
    
    
    我的数据库_config.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 f:
        data = yaml.full_load(f)
    
        for item, doc in data.items():
            print(item, ":", doc)
    
        database = DB(data)
    
    import yaml
    import unittest
    import database_config
    
    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):
            self.db_instance = database_config.DB('DatabaseConfig.yml')
    
        def test_missing_db(self):
            self.assertEqual(self.db_instance, None, "Connection returned None.")
    
        def test_db_type(self):
            with self.subTest():
                self.assertEqual(self.db_instance, 'postgres')
            with self.subTest():
                self.assertEqual(self.db_instance, 'sqlite')
    
    if __name__ == '__main__':
        unittest.main()
    
    
    数据库的测试文件test\u db\u type.py\u config.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 f:
        data = yaml.full_load(f)
    
        for item, doc in data.items():
            print(item, ":", doc)
    
        database = DB(data)
    
    import yaml
    import unittest
    import database_config
    
    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):
            self.db_instance = database_config.DB('DatabaseConfig.yml')
    
        def test_missing_db(self):
            self.assertEqual(self.db_instance, None, "Connection returned None.")
    
        def test_db_type(self):
            with self.subTest():
                self.assertEqual(self.db_instance, 'postgres')
            with self.subTest():
                self.assertEqual(self.db_instance, 'sqlite')
    
    if __name__ == '__main__':
        unittest.main()
    
    
    运行测试时发生错误回溯:

    (base) D:\Python>python -u "d:\Python\TestDataSource\test_db_type.py"
    database : postgres
    dbopt : {'host': 'None', 'port': 6313, 'dbname': 'spam_eggs2'}
    query : select * from manufacturing_product
    E
    ======================================================================
    ERROR: setUpClass (__main__.TestDBtype)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "d:\Python\TestDataSource\test_db_type.py", line 9, in setUpClass
        cls.init_db()
      File "d:\Python\TestDataSource\test_db_type.py", line 13, in init_db
        self.db_instance = database_config.DB('DatabaseConfig.yml')
      File "d:\Python\TestDataSource\database_config.py", line 8, in __init__
        self._dbconf = dict(dbconf)
    ValueError: dictionary update sequence element #0 has length 1; 2 is required
    
    ----------------------------------------------------------------------
    Ran 0 tests in 0.001s
    
    FAILED (errors=1)
    
    (base) D:\Python>C:/Users/duongnb/AppData/Local/Continuum/anaconda3/Scripts/activate
    
    (base) D:\Python>conda activate base
    
    (base) D:\Python>^A
    
    
    
    在这里:

    将单个字符串参数传递给
    DB
    构造函数;此参数在此处传递:

    class DB():
        def __init__(self, dbconf):
            self._dbconf = dict(dbconf)
    

    但是不能从单个参数创建dict-
    dict('DatabaseConfig.yml')
    引发错误。

    我应该怎么做?这是由于yaml文件的结构造成的吗?它发生在您甚至打开yaml文件之前。您可能应该重新考虑您的
    DB
    类。