Skip to content

Instantly share code, notes, and snippets.

@pypeach
Last active July 23, 2018 13:05
Show Gist options
  • Save pypeach/9d78297350eac7755c5d6f6205c61fec to your computer and use it in GitHub Desktop.
Save pypeach/9d78297350eac7755c5d6f6205c61fec to your computer and use it in GitHub Desktop.
テスト基底クラス
# coding:utf-8
import filecmp
import logging
import os
import unittest
from app.sql import sql_common
from app.util import app_config, file_access, message_access, string_helper, db_access
"""
テストクラスの基底クラスです。unittest.TestCaseを継承します
"""
__author__ = "t.ebinuma"
__version__ = "1.0"
__date__ = "28 April 2018"
class UnitTestBase(unittest.TestCase):
@staticmethod
def setup_app_config():
app_config.init()
@staticmethod
def cleanup_log_file():
"""
ログファイルをクリアする
"""
with open(app_config.get_log_file_path(), 'w', encoding='UTF-8') as f:
f.write("")
@staticmethod
def cleanup_table(table):
"""
テーブルをクリアする
"""
sql_common.delete_all(table)
@staticmethod
def check_text_in_log_file(text):
"""
ログファイルに指定された文字列が含まれるかをチェックする
"""
return file_access.is_exists_str(app_config.get_log_file_path(), text)
@staticmethod
def run_sql(sql_file):
"""
SQLファイルを実行してデータ登録を行う
"""
sql_path = os.path.join(app_config.get_value('test_sql_path'), sql_file + ".sql")
if os.path.exists(sql_path) is False:
logging.info(message_access.get_message('I903'), sql_path)
return
# SQL文を取得して実行する
with open(sql_path, 'r', encoding='UTF-8') as f:
sql_list = f.read().split(';')
for sql_item in sql_list:
sql = string_helper.skip_string_stream(sql_item.strip(), "#")
if len(sql) > 0:
db_access.dml_execute(sql)
@staticmethod
def get_data_file_path(file_name):
"""
テストで使用するデータファイルのフルパスを取得する
"""
return os.path.join(app_config.get_value('test_data_path'), file_name)
def assert_file_exists(self, file_path):
"""
ファイルが存在することをチェックする
"""
self.assertTrue(os.path.isfile(file_path))
def assert_file_not_exists(self, file_path):
"""
ファイルが存在しないことをチェックする
"""
self.assertFalse(os.path.isfile(file_path))
def assert_file_empty(self, file_path):
"""
ファイルが0バイトであることをチェックする
"""
self.assertEqual(os.path.getsize(file_path), 0)
def assert_file_not_empty(self, file_path):
"""
ファイルが0バイトでないことをチェックする
"""
self.assertGreater(os.path.getsize(file_path), 0)
def assert_file_equals(self, actual_file_path, expected_file_path):
"""
指定された2ファイルの内容が正しいかチェックする
"""
self.assertTrue(filecmp.cmp(actual_file_path, expected_file_path))
def assert_file_not_equals(self, actual_file_path, expected_file_path):
"""
指定された2ファイルの内容が異なるかチェックする
"""
self.assertFalse(filecmp.cmp(actual_file_path, expected_file_path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment