如何通过单元测试来测试 else 是否起作用?
How to test if else function by Unit test?
我有一个 YAML 配置文件用于检查与数据库的连接。我需要为我的配置文件编写一些测试用例,包括几个场景:
- 如果数据库字段为空则测试用例失败。
- 如果数据库不是 Sqlite 或 Postgre,测试用例失败。
我创建了一个测试class,但我不知道如何实施它。谁能给我建议?
data_source.yml:
database:
dbopt:
host: bvcbvcbvcb.com
port: 5432
dbname: db1
user: username
password: 1234
client_encoding: utf-8
connect_timeout: 60
sslmode: none
query:
select * from manufacturing_product
data_source.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('data_source.yml') as f:
data = yaml.full_load(f)
for item, doc in data.items():
print(item, ":", doc)
database = DB(data)
我的测试class:
import yaml
import unittest
import data_source
class TestDBtype(unittest.TestCase):
def test_missing_db(self):
# if database is None then failed
# What should I do?
pass
def test_db_type(self):
# if database is not Sqlite or Postgres then failed
# What should I do?
pass
if __name__ == '__main__':
unittest.main()
您可以像下面这样使用。想法是初始化数据库一次并在测试用例中使用连接实例。我没有测试过,但就是这个想法。
import data_source
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 = data_source.DB( config_file )
def test_missing_db(self):
self.assertEqual( db_instance, None, "Connection returned None.")
我有一个 YAML 配置文件用于检查与数据库的连接。我需要为我的配置文件编写一些测试用例,包括几个场景:
- 如果数据库字段为空则测试用例失败。
- 如果数据库不是 Sqlite 或 Postgre,测试用例失败。
我创建了一个测试class,但我不知道如何实施它。谁能给我建议?
data_source.yml:
database:
dbopt:
host: bvcbvcbvcb.com
port: 5432
dbname: db1
user: username
password: 1234
client_encoding: utf-8
connect_timeout: 60
sslmode: none
query:
select * from manufacturing_product
data_source.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('data_source.yml') as f:
data = yaml.full_load(f)
for item, doc in data.items():
print(item, ":", doc)
database = DB(data)
我的测试class:
import yaml
import unittest
import data_source
class TestDBtype(unittest.TestCase):
def test_missing_db(self):
# if database is None then failed
# What should I do?
pass
def test_db_type(self):
# if database is not Sqlite or Postgres then failed
# What should I do?
pass
if __name__ == '__main__':
unittest.main()
您可以像下面这样使用。想法是初始化数据库一次并在测试用例中使用连接实例。我没有测试过,但就是这个想法。
import data_source
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 = data_source.DB( config_file )
def test_missing_db(self):
self.assertEqual( db_instance, None, "Connection returned None.")